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”

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.

 

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.

 

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

 

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

 

 

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.

 

 

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

 

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.

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.

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.

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’.

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.

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

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

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”

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

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.


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

 

 

 

  1. Rohan Reply

    Hey,

    I need to know how to concatenate 2 strings in Talend, based on a condition.
    And if we can use tmap for it, or some additional component.
    Im stuck bigtime with this.
    Please help 🙂

    thanks

    Rohan

  2. admin Reply

    Hi Rohan, you can concatenate 2 strings in the tMap by utilizing the “+” operator in the tMap output table. Could you please provide some more detail regarding how you want to apply this based upon a condition? What are the details of your condition?

  3. Abbasi Reply

    Exception in component tMap_1
    java.lang.StringIndexOutOfBoundsException: String index out of range: -8
    at java.lang.String.substring(Unknown Source)

  4. Rahul Desale Reply

    Hi

    Can you tell please difference and important of different Talend versions like 5.4,5.5, and 5.5?

  5. Ashutosh Kumar Reply

    Hello Admin, I want to add a special variable after first syllable of a string. Eg: If the input is “How are you” Them i want to add “~” after “How” in the string but i don’t want to do this for second or other words in the string. Kindly let me know if you have any solution for that.

    Thanks and Regards,
    Ashutosh Kumar

Leave a Reply

*

captcha *