
Creating tMap expressions
In the previous example, we demonstrated how to manipulate the schemas and basic mapping of input columns to output columns. This recipe will show how to add much more complex rules to tMap
and how to use the Talend supplied functions.
Expressions are limited to a single line of Java code, but can contain any of the following:
- Constants
- Input variables
- The
globalMap
andcontext
variables - The
tMap
variables (see next recipe) - Talend supplied functions
- User supplied code routines
- Standard Custom java functions
- Included methods from JAR files
Getting ready
Open the job jo_cook_ch04_0020_usingExpressions
How to do it...
- Open
tMap
. - Click the left mouse button on the output
transactionDateTime
column. - You will notice that the expression button looks like the following:
- Click on the … button to open the Expression Builder dialogue.
Adding a Talend function
- The bottom-left panel lists the Categories of Talend functions. Scroll down to the TalendDate category and click the left mouse button.
- You should now see a list of available functions. Scroll down to the getCurrentDate function, and double-click the left mouse button.
- The function has now been added to the Expression panel, as shown in thefollowing screenshot:
- Exit Expression builder by clicking on Ok, and you will now see the function present in the expression column for
transactionDateTime
.Transforming input columns
- Select the output
cleanName
column and open Expression builder. - Select Category of StringHandling and the Function of UPCASE and double-click it to add it to the expression. Delete the text
"Hello"
. - In the middle-top panel, you will see the input columns available to add to the expression. From this panel, drag
customer.firstname
into the brackets and add+"
"+
. - Double-click
customer.middleName
, add+"
"+
then double-clickcustomer.lastName
. Your expression should now look like the following: - Exit Expression builder and run the job.
How it works...
The expression editor allows drag-and-drop creation of complex transformation rules. It also includes panels for accessing Talend supplied and user created functions to make building rules much easier.
There's more…
The expression builder also has test functionality, making it a powerful Talend feature, but it isn't the only way to create expressions.
The expression builder will also allow an expression to be tested by filling in values in the Value column and clicking the Test! button, as shown:

Although the expression builder is useful, it can also be time consuming to open and close the editor continuously. More seasoned Talend developers will often not use the expression builder, preferring instead to edit the line directly in the main tMap
window expression column or in the expanded expression window, which is an alternative tab of the schema panel, shown in the following screenshot:

See Also
Using the tMap
variables and ternary expressions are handled later in this chapter. The use of code routines is handled in Chapter 10, Debugging, Logging, and Testing.