tJavaRow Component
The tJavaRow component is part of the Custom Code family of components. This tutorial discusses why you may want to use tJavaRow in certain situations, as opposed to using a tMap component and entering mapping expressions.
tMap Expressions
The Mapping Editor of the component tMap usually provides all of the mapping capability that you may need. It is, however, limited in the construct of the code that you can write.
The following is a simple example of an expression.
row1.PostalCode.toUpperCase()
Of course, it's good practice to perform a null-test before calling a Method, so Talend also allows you to enter the following construct.
row1.PostalCode == null ? null : row1.PostalCode.toUpperCase()
You can also perform some nesting, extending your expression as follows.
row1.PostalCode == null ? null : row1.PostalCode.length() == 0 ? null : row1.PostalCode.toUpperCase()
Beyond tMap Expressions
Sometimes, you'll want to perform some mapping that is more complex that the capability of a tMap expression. You may want to call an external library, or you may want to write your own complex Java code.
First of all, consider if this piece of logic is something that you will want to reuse. If it is, then consider writing aRoutine.
As an alternative to writing a Routine, you can use the tJavaRow component, to make your mapping expression as complex as you need.
Using tJavaRow
When you first add tJavaRow to your Job, and connect its input and output, it will perform no mapping. Select the component and then take a look at the Basic settings tab.
The following screenshot shows comments that are automatically added by Talend. These show some simple example usage.
Default Code
Hit the Generate code button, to create the default mapping expressions for this component.
Caution Once you have written your own code, do not use Generate code as this will overwrite your custom code.
The following screenshot shows an example of some generated code.
Rolling Your Own Code
Now that you've generated the default code, you can modify your mappings as you require. You have the full power of Java at your disposal.
For an example of an advanced mapping, read our tutorial Talend tLibraryLoad Component Tutorial. In this tutorial,Google's Phone Number Handling Library is used to perform transformation of telephone numbers.
Conclusion
tJavaRow is a powerful Component. That said, I usually look at it as a component as last resort.
Whenever possible, restrict your transformation to tMap mapping expressions. If you need to write your own complex code, then see if this is a suitable candidate for a reusable Routine.
One of the goals of using an ETL tools is that complex business logic is not buried in 3GL code. Metadata and the ability to perform both Impact Analysis and Data Lineage are important aspects to your Talend development.