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

Announcements
Join us in Bucharest on Sept 18th for Qlik's AI Reality Tour! Register Now
cancel
Showing results for 
Search instead for 
Did you mean: 
Anonymous
Not applicable

[resolved] Use tJavaRow to get fieldname and fieldvalue

Hi guys,
I want to dynamically pick the field names and values of input flow so that I can output them as a single string.
This is because I have no control over the incoming messages but they all go through a tMap component. 
So, assuming we have incoming data in the format:

CustomerID|CustomerName
123|John Doe
456|Jane Doe

I want to loop through all the columns and give the output as column_name=column_value,column_name=column_value; ...
I.e. CustomerID=123,CustomerName=John Doe;CustomerID=456,CustomerName=Jane Doe;...

Using the advice from  https://community.talend.com/t5/Design-and-Development/resolved-how-to-get-the-column-names-in-a-dat... I was able to get the field names by looping through the tMap output columns. 
Now I want to know how to get the field values. I know I can use the code 
output_row.OutString = input_row.CustomerID; 

to get the field value but for this I have to give the explicit column name (CustomerID) and that's what I'm trying to avoid.
Labels (2)
1 Solution

Accepted Solutions
Anonymous
Not applicable
Author

Thanks  archenroot for all the assistance. 
I got the output I was looking for using the code below:
String columnName ="";
String fieldType = "";
String outString = "";
for (java.lang.reflect.Field field: tMapOut.getClass().getDeclaredFields()) { 
/*
tMapOut is the name connector from tMap to tJavaRow. Change to your map name
getDeclaredFields - to get the tmap output fields
*/
columnName = field.getName();
Class type = field.getType();
String fieldValue = "";
Object  objType = field.get(tMapOut); 
switch (type.getSimpleName())
{
case "String": // Write as many cases as there are data types in your job. Convert each output to string using the respective methods
fieldValue = (String)objType;
break;
case "Integer":
fieldValue = String.valueOf((int)objType);
break;
case "BigDecimal":
fieldValue = ((BigDecimal)objType).toPlainString();
}
outString += columnName + "=" + fieldValue + ",";
}
System.out.println(outString);

View solution in original post

7 Replies
Anonymous
Not applicable
Author

CustomerID|CustomerName

This line is the column name or the first row of incoming message? Which components do you use in the job? How do you define the schema? Just want to find out your needs, so that I can give your accurate answer.
Regards
Shong
Anonymous
Not applicable
Author

This line is the column name or the first row of incoming message? 

Yes, this is the column name.
Which components do you use in the job?

xmlFileInput -> tMap -> tJavaRow -> tLogRow
The xml files are simple enough for tMap to be sufficient. I'm not using the tXmlMap.
How do you define the schema?

I've defined the metadata in the repository for the input xmls.
Just want to find out your needs, so that I can give your accurate answer.

Much appreciated. Let me know if the responses are sufficient
Anonymous
Not applicable
Author

Hello 
The schema has been defined and they are fixed. I don't understand why you need to get the column name dynamically. To me, you can use a tJavaFlex component to loop each row and concatenate each row to a string, for example:
tFileInputXML--main--tJavaFlex
in the begin part of tJavaFlex, define a empty string. 
String result="";
in the main part:
Result=result+"CustomerID="+row1.CustomerID+","+"CustomerName="+row1.CustomerName+";";
in the end part, print the result to console and put it to a global variable for used later.
System.out.println(result);
globalMap.put("key",result);
Let me know if it fixes your need.
Regards
Shong
Anonymous
Not applicable
Author

Thanks Shong. But I knew of this option already. Even tJavaRow can do the same
I want to have a code I can re-use in all of my jobs. At the end of the day I'm going to have over 50 jobs with different input fields and field numbers. So the hardcoding of fieldnames in the javaflex code is not an ideal option for me.
The thing is there is a way to dynamically get the field names. But how do you dynamically get the field values in the same breath?
Anonymous
Not applicable
Author

Well in Enterprise version of Talend there is available Dynamic type schema, which is an object, whick you can query and it gives you dynamically any input structure (name of the fields) and you could iterate trough those and based on names obtain for each row the values.
In case your input message even with different columns is still the same (CSV file), it is not hard to write such java lib and forget about Talend tFileInputDelimited etc if you have only Open Source edition, you could use it as orchestrator only.
When this component obtain a key-value I suggest appending this into this structure - MultiMap ( https://google.github.io/guava/releases/19.0/api/docs/com/google/common/collect/Multimap.html
Then you can overide method .toString() or implement new one to print the data in the way you like.
If the insertion order is important, then go ahead with  https://google.github.io/guava/releases/19.0/api/docs/com/google/common/collect/LinkedListMultimap.h...
and that's it. I hope I understood your requirement correctly.
In general Talend won't help you in this case unless you have Enterprise edition and access to Dynamic schema, so do it by hand I suggest.
Anonymous
Not applicable
Author

Ok, based on communication we could do:
take output of tFileInputRead - tMap -> tJavaRow where you do:
iterate ({field}row.getDeclaredFields) {
field.getName()
switch field.getType()
case String: store String
case BigDecimal: convert and store to String
etc.
}
and here you go... 🙂
Anonymous
Not applicable
Author

Thanks  archenroot for all the assistance. 
I got the output I was looking for using the code below:
String columnName ="";
String fieldType = "";
String outString = "";
for (java.lang.reflect.Field field: tMapOut.getClass().getDeclaredFields()) { 
/*
tMapOut is the name connector from tMap to tJavaRow. Change to your map name
getDeclaredFields - to get the tmap output fields
*/
columnName = field.getName();
Class type = field.getType();
String fieldValue = "";
Object  objType = field.get(tMapOut); 
switch (type.getSimpleName())
{
case "String": // Write as many cases as there are data types in your job. Convert each output to string using the respective methods
fieldValue = (String)objType;
break;
case "Integer":
fieldValue = String.valueOf((int)objType);
break;
case "BigDecimal":
fieldValue = ((BigDecimal)objType).toPlainString();
}
outString += columnName + "=" + fieldValue + ",";
}
System.out.println(outString);