tFileOutputDelimited with text enclosure only for type "String"

One Star ODN
One Star

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

Re: tFileOutputDelimited with text enclosure only for type "String"

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.
One Star

Re: tFileOutputDelimited with text enclosure only for type "String"

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));
}
}
}
One Star

Re: tFileOutputDelimited with text enclosure only for type "String"

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.
One Star

Re: tFileOutputDelimited with text enclosure only for type "String"

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.
One Star

Re: tFileOutputDelimited with text enclosure only for type "String"

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
Six Stars

Re: tFileOutputDelimited with text enclosure only for type "String"

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
One Star ODN
One Star

Re: tFileOutputDelimited with text enclosure only for type "String"

Hello
I tried with the component "tFileOutputDeleimited "with the option "text enclosure"
with Integer as I get "123".
One Star

Re: tFileOutputDelimited with text enclosure only for type "String"

Told you so. ;-)
Rick - aka tchd - why are you saying that it has to be tJavaRow? ODN did not specify that...
One Star

Re: tFileOutputDelimited with text enclosure only for type "String"

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
One Star

Re: tFileOutputDelimited with text enclosure only for type "String"

Did anybody get to the bottom of this?
Experiencing the same issue still. (TOS 6.1.1)
Ten Stars

Re: tFileOutputDelimited with text enclosure only for type "String"

There are some old Jira tickets that raise this issue.  They were closed as "not a bug".  
If the implementation doesn't match users' expectations, perhaps the component's field name should be changed from "Text enclosure" to "Enclose all with".
One Star

Re: tFileOutputDelimited with text enclosure only for type "String"

This is totally a bug, and I boggle that it's been 6 years and is not resolved.
Five Stars

Re: tFileOutputDelimited with text enclosure only for type "String"

Could we reopen the ticket, and then all of us can post at once on it?