Skip to main content Skip to complementary content

Writing code using the Expression Builder

Some Jobs require pieces of code to be written in order to provide components with parameters. In the Component view of some components, an Expression Builder interface can help you write such pieces of code (in Java), known as expressions.

Using the Expression Builder of tMap, you can edit the expression for an input column, an output column, or a variable, or change the expressions for multiple output columns at the same time.

Editing individual expressions

About this task

The following example shows how to use the Expression Builder to edit two individual expressions.

Example of a Job using the tMap, tMysqlInput, tFileInputDelimited, and tLogRow components.

In this example, two input flows are connected to the tMap component.

  • From the DB input, comes a list of names made of a first name and a last name separated by a space char.

  • From the File input, comes a list of US states, in lower case.

In the tMap, use the expression builder to: First, replace the blank char separating the first and last names with an underscore char, and second, change the states from lower case to upper case.

Procedure

  1. In the tMap, set the relevant inner join to set the reference mapping.
    For more information regarding tMap, see tMap operation and Mapping data flows.
  2. From the main (row1) input, drop the Names column to the output area, and the State column from the lookup (row2) input towards the same output area.
  3. Click in the first Expression field (row1.Name), and then click the ... button that appears next to the expression.
    Location of the '…' button.

    The Expression Builder dialog box opens up.

    Overview of the Expression Builder dialog box.
  4. In the Category area, select the relevant action you want to perform. In this example, select StringHandling and select the EREPLACE function.
  5. In the Expression area, paste row1.Name in place of the text expression, in order to get: StringHandling.EREPLACE(row1.Name," ","_"). This expression will replace the separating space char with an underscore char in the char string given.
    Note that the CHANGE and EREPLACE functions in the StringHandling category are used to substitute all substrings that match the given regular expression in the given old string with the given replacement and returns a new string. Their three parameters are:
    • oldStr: the old string
    • newStr: the regular expression to match
    • replacement: the string to be substituted for every match
  6. Now check that the output is correct, by typing in the relevant Value field of the Test area, a placeholder value, e.g: Chuck Norris and clicking Test!. The correct change should be carried out, for example, Chuck_Norris.
  7. Click OK to validate the changes, and then proceed with the same operation for the second column (State).
  8. In the tMap output, select the row2.State Expression and click the [...] button to open the Expression builder again.
    Overview of the Expression Builder dialog box.

    This time, the StringHandling function to be used is UPCASE. The complete expression says: StringHandling.UPCASE(row2.State).

  9. Once again, check that the expression syntax is correct using a placeholder Value in the Test area, for example indiana. The Test! result should display INDIANA for this example. Then, click OK to validate the changes.

    Both expressions are now displayed in the tMap Expression field.

    Map editor.

Results

These changes will be carried out along the flow processing. The output of this example is as shown below.

Example of some results.

Setting expressions for multiple output columns simultaneously

About this task

tMap allows you to define the transformation behavior for multiple output columns at the same time.

Using a simple transformation Job, the following example shows how to define expressions on multiple columns in a batch manner in tMap.

Example of a Job using the tFileInputDelimited, tMap, and tLogRow components.

Here is the content of the input CSV file used in this example:

id;firstname;lastname;city;state
               1; Andrew;Adams;Madison; Rhode Island
               2;Andrew; Garfield; Saint Paul;Colorado
               3; Woodrow;   Eisenhower    ; Juneau; New Hampshire
               4;Woodrow; Jackson;Denver;Maine
               5; Lyndon;Buchanan; Pierre; Kentucky
               6;  Bill;Tyler; Helena; New York
               7;George;Adams;Oklahoma City    ;Alaska
               8;Ulysses; Garfield;Santa Fe;Massachusetts
               9; Thomas;Coolidge  ;Charleston;   Mississippi
               10;John;Polk;   Carson City;  Louisiana

In this example, all the output columns of type String will be trimmed to remove preceding and training whitespace and the last names and state names will be transformed to upper case.

Procedure

  1. In the Map Editor, complete the input-output mappings.
    Map editor.
  2. Select the columns of type String in the output table, namely firstname, lastname, city, and state in this example, and right-click the selection so that the Apply Routine button shows up.
    Apply Routine button from the Map editor.
  3. Click the Apply Routine button to open the Expression Builder dialog box.
    Overview of the Expression Builder dialog box.
  4. Select StringHandling in the Categories area, and then double-click the TRIM function in the Functions area to get StringHandling.TRIM(${0}) in the Expression field.
    Expression field in the Expression Builder dialog box.
  5. Click OK to close the Expression Builder dialog box.
  6. Select the lastname and state columns in the output table of the Map Editor, right-click the selection, and then click the Apply Routine button to open the Expression Builder dialog box.
  7. Select StringHandling in the Categories area, and then double-click the UPPERCASE function in the Functions area to get StringHandling(${0}) in the Expression field.
    Expression field in the Expression Builder dialog box.
  8. Click OK to close the Expression Builder dialog box.

Results

Now the expressions on those output columns look like below:

Output section from the Map editor.
The functions will be carried out along the flow processing. The output of this example is as shown below.
Example of some results.

Did this page help you?

If you find any issues with this page or its content – a typo, a missing step, or a technical error – please let us know!