One Star

[resolved] Iterating over a ArrayList<HashMap> to produce multiple output rows

Hi I need to iterate over my ArrayList<HashMap<String, String>> and produce multiple output rows
I Stuck the below in a tJavaRow but have got to a dead end as obviously on the last vlaues extracted from the HashMap are outputted in a single row.
"ArrayList<HashMap> profiled_attributes =(ArrayList<HashMap>)input_row.profiled_attributes;
for (HashMap<String, String> attribute : profiled_attributes){
output_row.name = attribute.get("name");
output_row.text_value = attribute.get("text_value");
output_row.source = attribute.get("source");
}"

Is there anyway to make the tJava.... components output multiple rows?
I also took a brief look at tLoop and tForeach but these don't support ArrayLists is there a way I could use these?
Or I'm I over complicating this and is there a much simpler option?
Cheers Andy
1 ACCEPTED SOLUTION

Accepted Solutions
One Star

Re: [resolved] Iterating over a ArrayList<HashMap> to produce multiple output rows

Solved but I feel there should be a better way i.e. the ability to loop/iterate something and produce multiple output rows, core components do it so im sure it possible...
Basically generate a JSON string (or any other flat format that can later be turned into rows );
Pass this thought to were you need to loop/iterate and then extract your rows.
Cheers Andy
6 REPLIES
Four Stars

Re: [resolved] Iterating over a ArrayList<HashMap> to produce multiple output rows

Hi Andy,
Can you try using
if (HashMap is not null)
{
your code
here
}
Vaibhav
One Star

Re: [resolved] Iterating over a ArrayList<HashMap> to produce multiple output rows

Vaibhav,
Thanks for the replay, however I'm not entirely sure I understand your recommendation as the is no issue with null HashMaps.
I've attached some images to illustrate.
Basically attribute_array rows contain the column profiled_attributes which is an ArrayList<HashMap<String, String>>, right now this is test data and contains 2 attributes i.e;
ArrayListHashMap("name"="Subscribed to","text_value"="Publication E","source"="Source 35" );
ArrayListHashMap("name=Lives In","text_value"="A Town","source"="Source 47");
I need the tJavaRow (or any other component) to iterate over this array and output a row per Array entry.
In my current example 1256 single_attribute rows need to be gendered for the 628 attribute arrays passed in.
Hope this explains what I need.
P.S. I have thought about generating a row per profiled_attribute, wrapping them up in an object seamed like a good idea and i'm sure there must be a way to do it.
Cheers Andy
Four Stars

Re: [resolved] Iterating over a ArrayList<HashMap> to produce multiple output rows

Pl check if this helps..., application area is different, but they have used HashMap in the way you are looking for...
http://spring-webservice-2-step-by-step.blogspot.in/2014/01/talend-elasticsearch-indexing-tutorial.h...
Vaibhav
One Star

Re: [resolved] Iterating over a ArrayList<HashMap> to produce multiple output rows

Thanks for that was kind of going this way anyway.
Now using a JSON String instead of a HashMap this can be feed to tExtractJSONFields and outputted as the rows I need.
I'll put up some screens later as an example.
P.S. That Mustache component looks really good (though I've already done the JSON encoding using base Java)
Cheers Andy
One Star

Re: [resolved] Iterating over a ArrayList<HashMap> to produce multiple output rows

Solved but I feel there should be a better way i.e. the ability to loop/iterate something and produce multiple output rows, core components do it so im sure it possible...
Basically generate a JSON string (or any other flat format that can later be turned into rows );
Pass this thought to were you need to loop/iterate and then extract your rows.
Cheers Andy
Four Stars

Re: [resolved] Iterating over a ArrayList<HashMap> to produce multiple output rows

Hay...
Good that you have achieved the objective in the different approach and it was useful to you. Also thanks for providing the screenshot of solution.
You may get an idea after looking at different scenario which could be implemented using different approach.
Vaibhav