Talend Open Studio Cookbook
上QQ阅读APP看书,第一时间看更新

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 and context 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...

  1. Open tMap.
  2. Click the left mouse button on the output transactionDateTime column.
  3. You will notice that the expression button looks like the following:
    How to do it...
  4. Click on the button to open the Expression Builder dialogue.

    Adding a Talend function

  5. The bottom-left panel lists the Categories of Talend functions. Scroll down to the TalendDate category and click the left mouse button.
  6. You should now see a list of available functions. Scroll down to the getCurrentDate function, and double-click the left mouse button.
  7. The function has now been added to the Expression panel, as shown in thefollowing screenshot:
    How to do it...
  8. Exit Expression builder by clicking on Ok, and you will now see the function present in the expression column for transactionDateTime.

    Transforming input columns

  9. Select the output cleanName column and open Expression builder.
  10. Select Category of StringHandling and the Function of UPCASE and double-click it to add it to the expression. Delete the text "Hello".
  11. 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 +" "+.
  12. Double-click customer.middleName, add +" "+ then double-click customer.lastName. Your expression should now look like the following:
    How to do it...
  13. 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.

Testing 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:

Testing expressions

Expression editor

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:

Expression editor

Getting around the 'one line' limitation

Although tMap expressions are limited to a single line of code, the use of the tMap variables, ternary expressions, and code routines do allow very complex mappings to be handled within a single expression.

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.