Talend tLoop

Talend tLoop is the same as For Loop or While loop. Use this Talend tLoop component to execute another component or job for a given number of times. This section covers multiple examples of the Talend tLoop to help you understand it completely.

Talend tLoop Example

Drag and drop the Talend tLoop component to the job designer window.

Add Talend tLoop to workflow to work with For and While loop

As you can see from the tLoop below, it has For loop and While loop options. Please choose as per your requirements, and for now, we kept it as For loop. Next, it has From, To, and Steps options with default values. It is same as for(i = 1; i <= 10; i++) or for(i = 1; i <= 10; i+1)

Within Talend tLoop to Component tab choose For or While loop

To display the iteration values, we used the tJava component. Within the Talend tJava, we add the Java println statements to print the current loop value in the iteration.

System.out.print(((Integer)globalMap.get("tLoop_1_CURRENT_VALUE")));
System.out.print("\t Ieration No = " + ((Integer)globalMap.get("tLoop_1_CURRENT_VALUE")));
System.out.println();
Write the Java Code

Let me run this Talend tLoop job and see the output. As you can see, iteration started at one and ended at 10 with an increment of 1.

Run the Talend tLoop Job with For and While loop

This time, we used the tRowGenerator to generate random rows for each iteration. For this, first, we changed the For loop maximum or value to 5.

Talend tLoop configuration of For loop

Within the tRowGenerator editor, we added four different columns. Next, we changed the number of rows to 2 which will generate two random rows.

Add Columns in the Schema

Next, we added the tLogRow to display the result. To do this, add the tLogRow to the workflow and connect the tRowGenerator main row to it. Next, within the tLogRow component tab, choose the table option.

In general, the tRowGenerator has to generate two rows and four columns. However, we used the tLoop to iterate the tRowGenerator. It means for each loop iteration, tRowGenerator has to generate two random rows. So, the total records will be five iterations * 2 random rows = 10. You can see the tLoop output.

Run the Talend tLoop Job  While loop

To make this interesting, we added one more row whose value is the tLoop current value. Use Control + Space to get the suggestions.

Add Current Value

Next, we changed the to value to 3 to accommodate all the results in the single screenshot.

Change Form, To, and Step values of the for loop

Now, you can see two randomly generated rows for each iteration.

Talend tLoop Job with For and While loop result