Do not input private or sensitive data. View Qlik Privacy & Cookie Policy.
Skip to main content

Announcements
Qlik Open Lakehouse is Now Generally Available! Discover the key highlights and partner resources here.
cancel
Showing results for 
Search instead for 
Did you mean: 
Anonymous
Not applicable

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

Labels (3)
1 Solution

Accepted Solutions
Anonymous
Not applicable
Author

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.

View solution in original post

7 Replies
Anonymous
Not applicable
Author

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.

Jesperrekuh
Specialist
Specialist

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.

 

 

 

Anonymous
Not applicable
Author

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.

 

 

Anonymous
Not applicable
Author

Thank You so much for your respponse @rhall

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

Anonymous
Not applicable
Author

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.

Anonymous
Not applicable
Author

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

Anonymous
Not applicable
Author

Thanks for the response and solution regarding this problem @rhall

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

Regards 

Meet Mahajan