How to add a rownumber to your Talend output

Do you want to add a rowNumber to your output then you really have to read this “how to” and learn how to do it! I needed it to add an order line number to my XML file.

1. Create a new job

  • Name your new job “rowNumber”

2. Add the tPrejob

The first component that you have to add is the tPrejob component. This component is very handy to set your database connections for example. In this case you are going to use it to create a global variable called “_rowNumber”.

RowCounter tPreJob

3. Add the tSetGlobalVar with variable “_rowNumber”

  • Add the tSetGlobalVar to temporarily store your row count. You don’t have to use double quotes in this case because you are going to use it as an integer.

RowCounter tSetGlobalVar

4. Move the tFixedFlowInput compoment to your canvas

  • Edit the schema
  • Add the city column as a String
  • Add the value column as a String

Split csv files schema

  • Connect the tPrejob to the tFixedFlowInput component (OnComponentOk)

RowCounter tPreJob tSetGlobalVar

 

  • Set the row and field separator
  • Add the content

Split csv files tfixedflowinput

Content: Los Angeles;1 Amsterdam;1 Rome;1 Barcelona;1 London;1 Berlin;1 Detroit;1 Amsterdam;2 Amsterdam;3 Los Angeles;2

5. Add the tJavaRow component to your canvas

  • Add the tJavaRow component
  • Connect the tFixedFlowInput component to you tJavaRow component (Row–> Main)
  • Add the code below.

RowCounter tJavaRow

Code:

//For passing the data through
output_row.city = row1.city;
output_row.value = row1.value;

//To add 1 to counter
Integer counter_i = (Integer)globalMap.get(“_rowNumber”);
globalMap.put(“_rowNumber”, new Integer(counter_i.intValue() +1));

6. Add a tMap

  • Add the tMap component to catch the output of the tJavaRow
  • Connect the tJavaRow to the tMap component (Row –> Main)
  • Configure the tMap component
    • Click the + button to add the outputflow
    • Add the columns (rowNumber – city – value)
    • Drag the columns from the input to the output
    • Add the global variable as a rowNumber

RowCounter tMap

7. Add a tLogRow to see your output

  • Add the tLogRow component next to your tMap component
  • Connect the tMap component to your tlogRow component (Row –> out1)

8. Run your job

Your result should look like this:

RowCounter Output

 

Your job should look like this:

RowCounter job _rowNumber

Leave a Reply