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[] |
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 dow --> 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
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