Six Stars

Pushing a Java list of POJOs from global variables to a file

I have a requirement wherein I had to do some complex processing by writing custom code, which, at the end of the processing generates a Java List of POJOs(u can assume a list of strings if it simplifies understanding). This list is pushed onto the Global map by the code. Now, I need to access this list from the Global map and push it onto a text file. 

 

Can anyone please suggest a way to achieve this? I am not sure how to pull the list from globalMap and push it to a file. Any help would be greatly appreciated.

 

Thanks.

1 ACCEPTED SOLUTION

Accepted Solutions
Fifteen Stars

Re: Pushing a Java list of POJOs from global variables to a file

Ah, do you want to iterate over that list to create "rows"? If so you can use a tJavaFlex. I do something similar here...https://www.rilhia.com/tutorials/talend-connect-example

Go to the heading '1) "Convert Input array to a datarow" (tJavaFlex)'

 

All you will need to do that is different is retrieve the ArrayList from the globalMap first, then you can essentially crib from what I have done.

Rilhia Solutions
6 REPLIES
Fifteen Stars

Re: Pushing a Java list of POJOs from global variables to a file

How exactly do you want this information saved to file? Are you serialising your objects (to be used again in another job?) or is this data that will be represented in a format like CSV? If it is the first, then you will need to add a method to serialise and output that to a String which can then be written to a file using a tFileOutputRaw. If it is the second, you will need your routine to have methods which will retrieve your data from your POJOs and output it as Strings (or any other Talend handled type. You would do this in a tMap and use a tFileOutputDelimited.

 

Have I understood your requirement correctly?

Rilhia Solutions
Six Stars

Re: Pushing a Java list of POJOs from global variables to a file

Thanks Rhall for you response. 

 

Serializing the POJO is not a problem. As you mentioned , I can get it done with the toString() or have them directly create Strings and put it in the global list. I can workout a way to get this done. My trouble is with reading this list in the global map and pushing it onto a file/database. To simplify, you can ignore POJOs and just assume that I am working with a list of String which are already present in globalMap.

 

Hope this is clear now.

Fifteen Stars

Re: Pushing a Java list of POJOs from global variables to a file

Ah, I think I understand.

 

Lets assume your POJO is an instantiation of a Class called MyPOJO. Lets also assume this class has methods for returning name and age (getName() and getAge()). Lets also assume that your POJO instantiation is called myPojo and has been added to the globalMap using the below code....

 

globalMap.put("myPojo", myPojo);

 

In your tMap (or other component where Java can be used), you can use the following code to retrieve the values from the POJO's methods.....

 

getName()

((MyPOJO)globalMap.get("myPojo")).getName()

getAge()

((MyPOJO)globalMap.get("myPojo")).getAge()
Rilhia Solutions
Six Stars

Re: Pushing a Java list of POJOs from global variables to a file

Alas..This would be perfect if only I had a single POJO. But, I will have a list of POJOs in my globalMap like below

 

List<MyPOJO> myPojoList = new ArrayList<MyPOJO>();
myPojoList.add(myPojo1);
myPojoList.add(myPojo2);
myPojoList.add(myPojo3);
myPojoList.add(myPojo4);
...
myPojoList.add(myPojoN);
globalMap.put("myPojoList", myPojoList);

 

Now, I need to access this list and push these values out into a file. 

Fifteen Stars

Re: Pushing a Java list of POJOs from global variables to a file

Ah, do you want to iterate over that list to create "rows"? If so you can use a tJavaFlex. I do something similar here...https://www.rilhia.com/tutorials/talend-connect-example

Go to the heading '1) "Convert Input array to a datarow" (tJavaFlex)'

 

All you will need to do that is different is retrieve the ArrayList from the globalMap first, then you can essentially crib from what I have done.

Rilhia Solutions
Six Stars

Re: Pushing a Java list of POJOs from global variables to a file

Awesome.

I was not aware that I can put a 'row' as an output from tJavaFlex.

I will try this solution out but I see no reason why this should not work. 

 

Thanks a lot Rhall Smiley Happy.