Tuesday, June 18, 2013

Using Expressions and Variables in Talend Maps


The Map -- tMap in Talend Open Studio -- is the workhorse of data conversion and ETL processing.  Occasionally, you'll need to work with a variable or expression.  There are several places in Talend where you can put an expression.

This article is available as a PDF here.

Maps are a productive way to transfer the fields of a source to a target using a graphical tool.  When there's a one-to-one correspondence between source and target fields, the map can be created with a simple drag-and-drop.  However, sometimes the map may require some logic or special processing in certain fields.

Examples of this special processing include

Null handling 
(row1.State==null)?"":row1.State 

Specifying a default value
(row2.New_Region==null)?row1.Region:row2.New_Region  

In tMap, you can put these variables in three places:

  1. In the target field Expression,
  2. As a Var, or
  3. In a tSetGlobalVar
The target field and Var panel locate the variable within the tMap component.  tSetGlobalVar locates the variable and expression out of the tMap component.

IN THE TARGET FIELD

Writing expressions in the target field is a concise way to process a field.  The target field Expression features an Expression Builder.

Expression Defined in Target field


AS A VAR

If your expression needs to be used throughout the map's fields, you can create a variable in the same window.


Notice that the variable is preceded with 'Var'.

USING TSETGLOBALVAR

This step involves adding another component to the canvas.  In addition to tMap, add a tSetGlobalVar component.  Rather than connecting the input Excel connection to the tMap, put the tSetGlobalVar in between.  This will require re-mapping the source and target fields; use the row of the tSetGlobalVar rather than the Excel file.

Double-click on the tSetGlobalVar and add a variable.  Make sure the variable is surrounded with quotes.  My example uses "mystate" and it is set with an expression that handles the null.  In the tMap, you'll use the following syntax in the target field.  The variable is stored in a Java map. Don't forget the cast.


(String)globalMap.get("mystate")



There is a lot of flexibility in setting variables in Talend Open Studio.  Pick the most concise syntax that gives you access to the variable where you need it.

No comments: