Talend Tutorials

Using Variables In The Tmap Component

Using Variables In The Tmap Component

In this post we are going to walk you through the basic steps required to get started on using variables in the tMap component. The use of variables within the tMap enables special logic to be programmed into the output mapping. Below we will demonstrate an example using variables to separate a multiword string into separate strings in the output mapping.

Step 1: Open Talend

Open Talend and create or open an existing project

Step 2: Create a new job

• Right click on Job Designs in the Repository window and select “Create job” • Name the job “tmap_variables”

image 1

Step 3: Design your job

In this step we are going to design our job to use variables in the tMap component In the palette window on the right hand side type “tFixedFlowInput” into the search box, then drag and drop the component “tFixedFlowInput” into the job window in the center of the screen.

image 2

Select the tFixedFlowInput Component in the job window and then select the “Component” tab near the bottom middle of the screen. Then click the button with three dots that appears, to add the schema by click on plus button. Name the columns and select the data types, then click on OK to apply the changes.

image 3

Set the value for “number of rows” to generate how many input rows we require for this example.

image 4

Provide the values to the schema. Provide the Name column a value with at least five words separated by spaces.

image 5

Select a tMap component from the palette on the right hand side of the screen and drag it into the job design window Right click on the tFixedFlowInput component, select Row->Main and connect a row to the tMap component.

image 6

Select a tLogRow component from the Palette and drag it over to the job design window.

image 7

Click on the tLogRow component to select it, and then navigate to the Component tab in the lower middle of the screen Under Mode, select “table (print values in cells of table)” radio button to display result in table format.

image 8

Next right click the tMap component, Select “Row” then “New Output”. Connect this new output to the tLogRow component Name the output row- “output1″. Now your tMap is connected to the tLogRow component.

image 9

Double click the tMap component to open up the tMap editor. Create a schema for the output1 in tMap by clicking on bottom plus symbol in the tMap output and set the respective data types.

image 10

You can see variable section in middle of the tMap. Click on the plus symbol to add a new variable and set the type to ‘int’.

image 11

Click and drag the name field from the “row1″ panel to the left of the screen into the expression in the variable panel. Write an expression to find the index of space in the name field in variable Expression. code: row1.name.indexOf(” “) Now the index of the first empty space is assigned to variable var1.

image 12

Add another variable by click on plus symbol in variable section. -This variable used to find second empty space in our input name field.

Find the second empty space using row1.name.indexOf(” “,Var.var1+1) :

This function finds the index of the space character starting from next character of the first empty space. i.e index of second empty space in the name field

image 13

Create another variable by repeating the above step to find the index of third empty space. row1.name.indexOf(” “,Var.var2+1) : This function finds the index of space character starting from next character of the second empty space. i.e index of third empty space in name field

image 14

Map the output columns : Click and drag the id, name data fields from the “row1″ panel to the left of the screen to the corresponding “output1″ data fields. This tMap editor enables you to map to fields from the input to the correct output fields in tLogRow. Map the substring_1 column with first word of the name field by using substring function row1.name.substring(0,Var.var1) gives the substring in the name file from “starting character” to “first empty space “

Map the substring_2 column with second word of the name field by using substring function row1.name.substring(Var.var1+1,Var.var2) gives the substring in the name field from “next character of the first empty space” to “second empty space”

Map the substring_3 column with third word of the name field by using substring function row1.name.substring(Var.var2+1,Var.var3) gives the substring in the name field from “next character of the second empty space” to “third empty space”

image 15

Click “Apply” then “OK” to save the changes and return to the Job Design window Now your job design is complete and we just need to run the job to see the results.

Step 4: Run your Job

In the group of tabs in the lower middle of the screen, select the “Run” tab

image 16

Under Execution, click the “Run” button Your job will be executed and you will see how many rows were processed in the job design window, You will be able to view the output data in the run window.

image 17

Congratulations! You have now learnt how to spilt a multi-word string into separate substrings using variables in the tMap component.

Looking For More Talend Resources and Help?
Ask An Expert