Hi all,
I was wondering if there is a more efficient way to convert a multiple fieldset from one source row into multiple rows for an output.
Example:
I have a row with the structure
key, value-a-1, value-a-2, value-a-3, value-b-1, value-b-2, value-b-3
and like to have an row (flow) like
key, value-a-1, value-b-1
key, value-a-2, value-b-2
key, value-a-3, value-b-3
My idea is at the moment to use a tMap to split the multiple fieldsets into three different flows for the output. As this is replicating quite some code I was wondering if there is a more efficient or elegant way.
Thx for your suggestions
Alex
Use a lookup table for your tMap containing one column and three rows with the values "1", "2" and "3" but don't link the main input table to the lookup table. This will give you three output rows for each input row and you can map the relevant field value depending on the lookup value e.g. lookupFlow.columnName.equals("1")?mainFlow.valueA1:lookupFlow.columnName.equals("2")?mainFlow.valueA2:mainFlow.valueA3.
See
https://community.talend.com/t5/Design-and-Development/resolved-Mapping-a-single-row-to-multiple-row... for a similar example.
Dear Alevy,
Thanks this is a helpful solution and works.
Do you have a smart idea how to avoid having an if statement for more than 3 different fields?
My idea is to use Java reflection like this to call the fields dynamic. However I'm not sure how to integrate this in the tMap. It doesn't look very simple.
Regards
Dear Alevy,
First off - your first solution is perfect!
It works and does exactly what it is supposed to do.
I think the tJavaRow & switch is quite similar what we can achieve with the if statement. However the tJavaRow can organize a reflection style approach what I was looking for.
The reflection in Java allows you to dynamically access attributes of a class or methods. It is a bit similar like using PHP with
$fieldName ="value-a-" + lookupRow->code;
$value = $row->$fieldName;
In Java you need to do a work around using a method and then invoke the method instance. Its a bit more code but will work quite similar.
Thank's again for your help - your solution worked for me
Alex
From v4.2.0, the tSplitRow component handles this very well. And joined outputs in tMap can be even better since you can drag and drop. tSplitRow is a pain to use.