Hitachi Vantara Pentaho Community Wiki
Child pages
  • User Defined Java Expression
Skip to end of metadata
Go to start of metadata

Description

This step allows you to enter User Defined Java Expressions as a basis for the calculation of new values.

If you have a Java expression like :

C=A+B

Then you can simply enter the right side of the expression in the dialog:

A+B

  The values are exposed to the expressions as the Java objects they are :

Data type

Java Class

String

java.lang.String

Integer

java.lang.Long

Number

java.lang.Double

Date

java.util.Date

BigNumber

BigDecimal

Binary

byte[]

Options

Options

Description

New Field

The new field in the data stream. If you want to overwrite an existing field, you need to define the field here and in the "Replace value" option.

Java Expression

The Java Expression, see examples below

Value Type

Type

Length

Length

Precision

Precision

Replace value

Select this identical to the "New field" name when you want to replace the

Metadata Injection Support

All fields of this step support metadata injection. You can use this step with ETL Metadata Injection to pass metadata to your transformation at runtime.

Examples

Add 2 integers, A and B

A+B

Concatenate 2 Strings : firstname and name and put a space in between

firstname+" "+name

or if you really care about performance, this might be faster:

new StringBuffer(firstname).append(" ").append(name).toString()

Set the first character of each word in upper-case: john doe --> John Doe

org.pentaho.di.core.Const.initCap(name)

Use native Java and API functions

System.getProperty("os.name")

When the used method throws an exception, you need at minimum PDI 3.2.1, see PDI-2611

Business rules (If / Then / Else)

When a<c then return true else return false

a<c?true:false

This can be more complicated

a<c?(a==1?1:2):3

even with OR and AND and other operators and functions

Using Constants

If you use a constant, you may need to define the right type in some expressions otherwise it could throw:

Incompatible expression types "int" and "java.lang.Long"

To solve this, use:

test == null ? new Long(0) : test

In this case, it checks if test is null and replaces with zero. If it is not null, it will return test.

If the return value is a BigNumber (BigDecimal), you need at minimum PDI 3.2.3, see PDI-2910

Cut a string from end and test for null and minimal length

Imagine you have input strings with

  • Orlando FL
  • New York NY

and you want to separate the state and city, you could use the following expressions:

For state (get the last 2 characters):

location != null && location.length()>2 ? location.substring(location.length()-2, location.length()) : null

For city (get the beginning without the last 2 characters and trim):

location != null && location.length()>2 ? location.substring(0, location.length()-2).trim() : location

Within the code there is a check for null and a minimal length of 2, the complete example is attached to an enhancement request PDI-6915.

Functionality of a LIKE operator (contains string)  and replacing values

The following example returns 1 when abc is within the source string, otherwise 2. It returns also 2 when the source string is null. The return values could be of value type Integer.

samplestr !=null && samplestr.indexOf("abc")>-1 ? 1 : 2

The complete example is attached to an enhancement request PDI-1241.

  • No labels

1 Comment

  1. You can see the performance advantage of using java expression under certain conditions in this article:

    http://rpbouman.blogspot.com/2009/11/pentaho-data-integration-javascript.html