Insert data generated by ArrayList and HashMap with manual code into DATABASE

Seven Stars

Insert data generated by ArrayList and HashMap with manual code into DATABASE

I have created the "ArrayList" in tJava which explored below briefly 

 

List<String> headers = new ArrayList<String>();
headers.add("Name");
headers.add("Age");
headers.add("Package");

List<HashMap<String, String>> rows = new ArrayList<HashMap<String,String>>();

HashMap<String, String> row1 = new HashMap<String, String>();
row1.put("Name","Meet");
row1.put("Age","10");
row1.put("Package", "10.25");


HashMap<String, String> row2 = new HashMap<String, String>();
row2.put("Name","Dhaval");
row2.put("Age","20");
row2.put("Package", "45.23");

HashMap<String, String> row3 = new HashMap<String, String>();
row3.put("Name","Hemali");
row3.put("Age","15");
row3.put("Package", "25.2");


rows.add(row1);
rows.add(row2);
rows.add(row3);

this.globalMap.put("rows", rows);

 

 

 

 In tJava component this code has been written and I wanted to insert these 3 data into the database. 

 

Name, Age, Package 

Meet, 10, 10.25

Dhaval, 20, 45.23

Hemali, 20, 25.2

 

How can I achieve this with scenario minimum use of components and with an efficient way?
Please suggest asap.

 


Thanks 

Meet Mahajan


Accepted Solutions
Sixteen Stars

Re: Insert data generated by ArrayList and HashMap with manual code into DATABASE

Put your code into the Start Code section of a tJavaFlex component. At the end of your code, crate an iterator for your ArrayList and open a while loop to iterate over the ArrayList. DO NOT CLOSE IT HERE.

 

Now create an output schema matching your data set for your tJavaFlexin. In the Main Code section (consider this section as a looping section in the middle of a while or for loop), use the iterator created in the Start Code to get values for your output columns. For example.....

 

HashMap row = it.next();
row1.Name = row.get("Name");
row1.Age = row.get("Age");
row1.Package = row.get("Package");

Then in the End Code section just close your loop.

 

You will arguably want to do more here, but the key is learning how the tJavaFlex works. Just consider it as a loop with the Main Code being the body of the loop, the Start Code being the code fired ONCE before the loop and the End Code fired ONCE afterwards.

Sixteen Stars

Re: Insert data generated by ArrayList and HashMap with manual code into DATABASE

You don't need to pass the ArrayList to get the data into a database. Just do as I suggested and your data will be sent to the database row by row. However, if you want to pass an ArrayList via a row in Talend, simply pass it as an Object and cast it at the other end.


All Replies
Sixteen Stars

Re: Insert data generated by ArrayList and HashMap with manual code into DATABASE

Put your code into the Start Code section of a tJavaFlex component. At the end of your code, crate an iterator for your ArrayList and open a while loop to iterate over the ArrayList. DO NOT CLOSE IT HERE.

 

Now create an output schema matching your data set for your tJavaFlexin. In the Main Code section (consider this section as a looping section in the middle of a while or for loop), use the iterator created in the Start Code to get values for your output columns. For example.....

 

HashMap row = it.next();
row1.Name = row.get("Name");
row1.Age = row.get("Age");
row1.Package = row.get("Package");

Then in the End Code section just close your loop.

 

You will arguably want to do more here, but the key is learning how the tJavaFlex works. Just consider it as a loop with the Main Code being the body of the loop, the Start Code being the code fired ONCE before the loop and the End Code fired ONCE afterwards.

Ten Stars

Re: Insert data generated by ArrayList and HashMap with manual code into DATABASE

First of all... why the : asap !? It's kinda rude! Every question on this forum is preferably answered as soon as possible... dont be a doucebag, you are as equally important as anybody else here.

Could you elaborate please ... I dont understand why java code is involved, its static not dynamic?
Simply iterate over a tForEach item or something component -> tIterateToFlow -> tDBoutput.
Within the tForEach put a String arrays

 

( String[] { "Meet" , 2, "CCC", "DDD", "EEE"})
( String[] { "Daval" , 12 , "X" , "Morty", "Rick"})

From the tForEach you get the current value and assign it to Name :

((String[])globalMap.get("tForeach_1_CURRENT_VALUE"))[0]
-> return : "Meet"
and the [1] will return : 2

Next iteration will give you the next array and its values.

Suggest using this flow/process design.

 

 

 

Seven Stars

Re: Insert data generated by ArrayList and HashMap with manual code into DATABASE

Sorry @Dijke if you find this rude, but i was badly stucked in this Scenario and that's why i asked for quick possible response.

 

About Flow design 

I fetch the headers from database which is dynamic can be vary from 10-20 and data should get inserted everytime when this code runs.

 

 

Seven Stars

Re: Insert data generated by ArrayList and HashMap with manual code into DATABASE

Thank You so much for your respponse @rhall_2_0

After  Creation of this HashMap how can i pass this arrayList to a component ?
using a context or global variable?

Sixteen Stars

Re: Insert data generated by ArrayList and HashMap with manual code into DATABASE

You don't need to pass the ArrayList to get the data into a database. Just do as I suggested and your data will be sent to the database row by row. However, if you want to pass an ArrayList via a row in Talend, simply pass it as an Object and cast it at the other end.

Sixteen Stars

Re: Insert data generated by ArrayList and HashMap with manual code into DATABASE

Has this solved your problem? If so, can you mark the solution as accepted please

Seven Stars

Re: Insert data generated by ArrayList and HashMap with manual code into DATABASE

Thanks for the response and solution regarding this problem @rhall_2_0

Execution is in Process. When it is done definitely, I'll do that.

Regards 

Meet Mahajan