One Star

[resolved] output_row dynamic field access

Hello
I have this input schema (simple example) :
Identifier ; Col1 ; Col2 ; Col3
A ; 1 ; 10 ; 2.3
B ; 11 ; 152
C ; 4.5 ; 1 ; 12 ; 1.8
I want this output :
A1 ; A2 ; A3 ; B1 ; B2 ; C1 ; C2 ; C3 ; C4
1 ; 10 ; 2.3 ; 11 ; 152 ; 4.5 ; 1 ; 12 ; 1.8
I don't know how to do this with tMap or other component, so I try to use tJavaRow. But the output field row depends of the input Identifier column value, and I don't know how to access the output field row dynamically. Something like this ... :
output_row = input_row.col1
output_row = input_row.col2
...
Someone can help me ?
Thx
1 ACCEPTED SOLUTION

Accepted Solutions
One Star

Re: [resolved] output_row dynamic field access

Hi
No reply, but I found a solution to my problem, so I post it, maybe it will be usefull for another guy ...
I user a tJavaRow, with my input shema and my output schema (output shema is different from input) :
For my example, it give that :
String s;
String value;
for(int i=1;i<=3;i++)
{
s = String.valueOf(i);
value = (String)input_row.getClass().getDeclaredField("Column"+i).get(input_row);
if (value != null && !value.equals(""))
{
java.lang.reflect.Field f = output_row.getClass().getDeclaredField(input_row.Identifier+s);
if (f.getType() == BigDecimal.class)
{
f.set(output_row, new BigDecimal(value));
}
else
{
f.set(output_row, value);
}
}
}

It was more a Java problem than a Talend problem.
This code generate multiple lines, but only the last one is complete. So I use another Field in which I put an increment, then I use a tSortRow (sort desc on my field) and a tSampleRow to keep only the first row. If someone has a best solution to do this ....
Bye
5 REPLIES
One Star

Re: [resolved] output_row dynamic field access

Hi
No reply, but I found a solution to my problem, so I post it, maybe it will be usefull for another guy ...
I user a tJavaRow, with my input shema and my output schema (output shema is different from input) :
For my example, it give that :
String s;
String value;
for(int i=1;i<=3;i++)
{
s = String.valueOf(i);
value = (String)input_row.getClass().getDeclaredField("Column"+i).get(input_row);
if (value != null && !value.equals(""))
{
java.lang.reflect.Field f = output_row.getClass().getDeclaredField(input_row.Identifier+s);
if (f.getType() == BigDecimal.class)
{
f.set(output_row, new BigDecimal(value));
}
else
{
f.set(output_row, value);
}
}
}

It was more a Java problem than a Talend problem.
This code generate multiple lines, but only the last one is complete. So I use another Field in which I put an increment, then I use a tSortRow (sort desc on my field) and a tSampleRow to keep only the first row. If someone has a best solution to do this ....
Bye
One Star

Re: [resolved] output_row dynamic field access

Hi,
thanks for posting your solution (even if no one could help you out). I read your post and thought about reflection too. But didn't got the time to think more about this (or even wrote an example). In Perl it is much easier, because the values are stored in a hash. I think this is something where some optimization in Talend are possible: Allow (additional to the actual fields) to set values by there name as an parameter: setColumn(String name, Object value)
Bye
Volker
One Star

Re: [resolved] output_row dynamic field access

Yes, the solution setColumn(String name, Object value) could be a great help ....
One Star

Re: [resolved] output_row dynamic field access

I added a feature request:
8407
One Star

Re: [resolved] output_row dynamic field access

Hi,
My issue is somewhat related to this topic.
At design time, I do not know the column names of a CSV file, but they are present in the first row as header. I just need to create an XML file containing the corresponding columns and the data.
Say for ex, below is my data in CSV,
ID,Name,Age
1,qwerty,12
2,asdf,11
3,zxcvb,10
I have to generate an XML file like this:
<?xml version="1.0" encoding="UTF-8"?>
<entities>
<entity>
<field name="ID" value="1"/>
<field name="Name" value="qwerty"/>
<field name="Age" value="12"/>
</entity>
<entity>
<field name="ID" value="2"/>
<field name="Name" value="asdf"/>
<field name="Age" value="11"/>
</entity>
<entity>
<field name="ID" value="3"/>
<field name="Name" value="zxcvb"/>
<field name="Age" value="10"/>
</entity>
</entities>
If anybody can help me out with the Java code to be written for this, it would be very helpful.
Thanks in advance,
Rashmitha