One Star

How to create SWITCH statement in talend?

How to create SWITCH statement in talend?
for ex: Var==1
switch(var)
{
case 1: value; result
case 2:
case 3:
}
is it possible to create in talend components?
6 REPLIES
One Star

Re: How to create SWITCH statement in talend?

I think you're looking for the Run If trigger. Right-click on a component and select "Run If" from the Triggers menu. Drag one of these to each target component (the cases). Click on each Run If and specify an expression in the Component View.
This video shows how to use Run If in the section on environment-specific calls: http://bekwam.blogspot.com/2011/12/video-coded-to-last-with-talend-open.html.
One Star

Re: How to create SWITCH statement in talend?

no i m not looking for run if
i m asking how can we create swith case statement (i dont want to use if else) and in that switch case passing string variable
ex:
case---value
JAN----1st month
FEB----2nd month
MAR---3rd month......

i want to pass variable value JAN then i need to out 1st month
One Star

Re: How to create SWITCH statement in talend?

This seems like a lookup table. Are you able to use a CSV or spreadsheet with the key as the first column (JAN) and the value as the second column ("1st month")? Then, you'd use a tMap to join the CSV or spreadsheet with your input.
Another option is to write a Talend routine that accepts a String. Java prior to version 7 doesn't accepts Strings in its switches. You can use something like this pasted into a Talend Routine.
public static String monthLookup(String in) {
HashMap<String, String> map = new LinkedHashMap<String, String>();
map.put("JAN", "1st month");
map.put("FEB", "2nd month");
map.put("MAR", "3rd month");
// ... other months
return (map.get(in)!=null)?map.get(in):"";
}
One Star

Re: How to create SWITCH statement in talend?

thanks
i ll try it and come back
One Star

Re: How to create SWITCH statement in talend?

Hi,
You could also use this syntax if you have to do your switch statement in a tmap for example: row1.field.equals(value1)?something:row1.field.equals(value2)?something2:somethingelse
the value between the "?" and ":" is the value that will be put if the statemement before the "?" is verified, if not it ll put the one after the ":". You can make several statements on after an other to make a switch like condition (as shown).
Hope it helps you.
One Star

Re: How to create SWITCH statement in talend?

Hi,
for complex switch you can also write a user routine with your switch statement