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: 
ODN
Contributor
Contributor

tFileOutputDelimited with text enclosure only for type "String"

Hello
I want to make a CSV file with a tFileOutputDelimited with text enclosure only for type "String" (not for number).
exemple:
12;"text2";"text3";15
45;"text4";"text25";12
how to do this?
Thanks
Eric
Labels (2)
14 Replies
Anonymous
Not applicable

in a tJavaRow you can use java.lang.reflect to retrieve the column types, enclose strings in quotes and then write them out to a file delimited with no text enclosure.
Anonymous
Not applicable

Hi ODN,
I hope you don't mind me hijacking the thread a little, but I really liked John's idea of using java.lang.reflect, so gave it a go myself (see code below). I'm fairly new to java, so I'm not sure if this would be considered robust enough for say production use.
John, if you would do it differently or have any tips ideas regarding robustness/error checking etc then I'd love to capture your thoughts.
Regards,
Rick
Class cls = Class.forName(input_row.getClass().getName());
Class clsOut = Class.forName(output_row.getClass().getName());
Field fieldlist[] = cls.getDeclaredFields();
Field fieldlistOut[] = clsOut.getDeclaredFields();
for (int i = 0; i < fieldlist.length; i++) {
Field fld = fieldlist ;
Field fldOut = fieldlistOut;
int mod = fld.getModifiers();
if ( Modifier.toString(mod).equals("public")) {
if ( fld.getType().getName().equals("java.lang.String") ) {
fldOut.set(output_row, "\"" + fld.get(input_row) + "\"");
} else {
fldOut.set(output_row, fld.get(input_row));
}
}
}
Anonymous
Not applicable

This is all good, but the actual question is that the component should not even place "s around non string columns...
I would consider the behavior not correct, since an integer is non-text.
janhess
Creator II
Creator II

You could do it manually in a map rule by adding " to the start and end of string fields and not setting the csv options in the output file.
OK if there are only a few fields.
Anonymous
Not applicable

This is all good, but the actual question is that the component should not even place "s around non string columns...
I would consider the behavior not correct, since an integer is non-text.

Hi CaptainRoo,
The code does only enclose strings, however I did neglect to state that:
- It should be in a tJavaRow
- You will need to include java.lang.reflection.*
- You will need to ensure that the tOutputFileDelimited has csv options turned off, so that no automatic enclosure is performed.

janhess: you are absolutely correct. This method would add an overhead in processing due to the need to loop through the fields for every row, whereas adding a rule for each field wouldn't have that overhead.
Another option would be to create a code routine to add the "s and then apply it to each string field.

Regards,
Rick
Anonymous
Not applicable

Well I edited this topic, because for the first time I was little bit wrong :-)) hehe
I checked tFileInputDelimited and you are able to use "Text enclosure" feature if you check the CSV option on component. So your functionality is already there.
This is also available by creating metadata for delimited file, select type CSV and there already is that option related to "Text-enclousure" as """, then the schema will be based on reading that file in a way:
"alfa" will be String
245 will be Integer
So Talend is already ready for your scenario.
Best regards,
archenroot
ODN
Contributor
Contributor
Author

Hello
I tried with the component "tFileOutputDeleimited "with the option "text enclosure"
with Integer as I get "123".
Anonymous
Not applicable

Told you so. 😉
Rick - aka tchd - why are you saying that it has to be tJavaRow? ODN did not specify that...
Anonymous
Not applicable

Hi Gabor,
It was a comment on the java code that I wrote that uses java.lang.reflection to test the field types and then enclose the strings. i.e. it has to be in the tJavaRow component to work.
Regards,
Rick