One Star

[resolved] how to get the column names in a data flow?

Hi Smiley Happy ,
I'd like to know how to get the column names of a table in a flow of data: excel, delimited or postgresql.
I have to compare them to the ones I should use. It could be via a tMap component.
I must follow a strict naming plan for files and their fields...
Would you know how to get that?
Thanks,
Mathieu
1 ACCEPTED SOLUTION

Accepted Solutions
Four Stars

Re: [resolved] how to get the column names in a data flow?

Hi Feroz,
Following code you can use in your tJavaRow...to get the column names to the context variable.
for (java.lang.reflect.Field field: row1.getClass().getDeclaredFields()) {
context.columnName = field.getName();
      System.out.println("Field name is " + context.columnName );      
           }
You can extend this code for your design...
Vaibhav
12 REPLIES
Community Manager

Re: [resolved] how to get the column names in a data flow?

Hello Mathieu
Try the code
String columnName=null;
for (java.lang.reflect.Field field: row1.getClass().getDeclaredFields()) {
columnName=field.getName().toLowerCase();
System.out.println(columnName);
}

Best regards
shong
----------------------------------------------------------
Talend | Data Agility for Modern Business
One Star

Re: [resolved] how to get the column names in a data flow?

thanks shong,
It works well, I get the column names.
They're printed out in the system.
But how to export them in a delimited file?
or make a column name a data flow so that it can be compared to a reference table via a tMap component?
Thanks again Smiley Happy,
Mathieu
Community Manager

Re: [resolved] how to get the column names in a data flow?

Hello
But how to export them in a delimited file?
or make a column name a data flow so that it can be compared to a reference table via a tMap component?

Define context variables to store the column name.
Best regards

shong
----------------------------------------------------------
Talend | Data Agility for Modern Business
One Star

Re: [resolved] how to get the column names in a data flow?

OK thanks Smiley Happy
One Star

Re: [resolved] how to get the column names in a data flow?

Hi,
It works correctly but I would to have access to the value or at least I would like to know if the value is null/blank. It can be a problem because I will have different type. Do you have an answer?
Thanks.
Seven Stars

Re: [resolved] how to get the column names in a data flow?

Use tJavaRow instead of tJava, then:
Object objValue = field.get(input_row);
String fldType = field.getType().getName();
Once you know the field type you can convert the value to the appropriate type for comparisons.
One Star

Re: [resolved] how to get the column names in a data flow?

plz can u tell me how to assign column names to context variable
thanks and regards
syed
One Star

Re: [resolved] how to get the column names in a data flow?

plz can u tell me how to assign column names to context variable
thanks and regards
syed
One Star

Re: [resolved] how to get the column names in a data flow?

plz can u tell me how to assign column names to context variable
thanks and regards
syed

Plz Give me a quick reply
Four Stars

Re: [resolved] how to get the column names in a data flow?

Hi Feroz,
Following code you can use in your tJavaRow...to get the column names to the context variable.
for (java.lang.reflect.Field field: row1.getClass().getDeclaredFields()) {
context.columnName = field.getName();
      System.out.println("Field name is " + context.columnName );      
           }
You can extend this code for your design...
Vaibhav
One Star

Re: [resolved] how to get the column names in a data flow?

Hello, 
I have the same problem: My job consists on doing the mapping of a XML file to a Csv file.But I want to have a second Csv file which contains the columns' names and types of the first.
Do you have an answer please??
Thanks
Moderator

Re: [resolved] how to get the column names in a data flow?

Hi rloukil,
But I want to have a second Csv file which contains the columns' names and types of the first.

Could you please elaborate your case with an example with input and expected output values?


Best regards
Sabrina
--
Don't forget to give kudos when a reply is helpful and click Accept the solution when you think you're good with it.