If-Then-Else statement in Talend

If-Then-Else statement in Talend
4.7 (94.04%) 94 votes

If-Then-Else is the common requirement of Talend implementation to use conditional execution of job, component or data, during various part of Talend job development.

Following are the possible ways of implementing If-Then-Else cases.

Using tMap: If Then Else with tMap

AS you know tMap is versatile component which allows us developing lookups, rejects, conditional formatting, transformation, data filter, and many more operations, therefore If-Then-Else is frequently used in tMap.

If-Then-Else is ternary operator when it comes to use in tMap because tMap has java expressions where we can add If-Then-Else but we should be writing it in a single line statement that is why traditional If-Then-Else will not work inside tMap.

To achieve this Talend has provided  ternary operator see in below statement.

result= 1==1?true:false;

If you notice we are evaluating 1==1 and if 1 equals to 1 then result is true else false. We can use same statement in tMap with actual data lets see with actual scenario.

Lets design job for demonstration.

  • Add tFixedFlowInput component and do following settings.
    • In schema editor add column “ID” with integer type,
    • Add column City and State as string
    • Number of rows=10
    • Select Mode=”Use Single Table”

  • Add tMap after tFixedFlowInput and connect with main.
    • Create output in tMap and map all the source columns to output
  • Add tLogRow to see the result after tMap and connect.

Now our sample job is ready we will start adding various scenarios.

Scenario 1 : Process only five rows.

Type 1: Open tMap editor and click on source side expression filter and following code inside it.

row12.ID>0 && row12.ID<=5

rwo12 is my input connection name which is come from tFixedFlowInput component. This code will restrict 5 rows to be processed.

Type 2: At output filter expression area you can add same condition and achieve same result.

See the screen for implementation.

If-Then-Else in Talend
If-Then-Else in Talend

Filters are nothing but a conditions to restrict data being processed, passed or transferred for further processing.

You got how to write if at filters section which has no else part.

Scenario 2: Check the number is even or not.

Type 1: Using tMap variable

Create variable in tMap, “ChechEven” as boolean type. and write down below statement inside it.

row12.ID%2==0?true:false

This will check whether incoming number is even or not.

Type 2: Inside output columns expression section.

Create another two columns at output side “VEvenCheck” and “CEvenChack” as  boolean type and assign respective values to each column as follows

Below screen shows tMap setting with filters and if conditions.

If-Then-Else in Talend with filters
If-Then-Else in Talend with filters

 

 

 

 

 

Scenario 3: Multiple If-Then-Else statements.

Now we have single If else statement but now we are going to use multiple if else statement inside tMap. for that we will use below sample data.

Step 1: Check test score and assign grade.

  • Modify tFixedFlowInput component as follows.
    • Mode=”Use Inline content(Delimited file)”
    • Keep row separator default and field separator =”|”
    • Add below records in inside content area.

We are implementing below java if then else statement in tMap

go to the tMap and add one extra column at output as “Grade” with string type and then type below statement in it.

Above statement in tMap with result.

multiple if-then-else Statement in Talend with Result
multiple if-then-else Statement in Talend with Result

You can use same statement in tMap variables section in same way. benefit of using variable is you can use same variable in multiple output columns, to avoid replicating same calculations.

Below List of components who supports normal If-Then-Else statements.

  • tJava
  • tJavaRow
  • tJavaFlex
  • Talend Routines

If you want to redirected flow or execute another job/component based on certain condition then see my previous post on connect-component-with-triggers-and-rows. In this article I shown if condition at component level instead data or flow level.

About dwetl

3 comments on “If-Then-Else statement in Talend

  1. Great post! Tx…

    Have to admit though, for some reason this does not work in my tMap and I get just a blank field for where the “Grade” should be. I have also tried that for validating units of measure and to replace the values. Although the code tests OK in the expression builder, it still yields blank results…any tips?

    1. provide hard-code values and see whether it gives result or not if yes then check with source data. for better understanding put some dummy example here.

  2. Hi dwetl

    Can we get a blog about String Functions and Operators combine together and applying some logic etc
    I hope this will be more helpful for us if we got that mean

Leave a Reply to dwetl Cancel reply

Your email address will not be published. Required fields are marked *