write header "by hand"

One Star

write header "by hand"

Hi,
tFileOutputDelimited allows to include the header. Instead of using this function I'd like to do it by hand, this means I want to enrich the header with some additions.
Since I have already defined the column names in the schema, I'd like to reuse these definitions. Is this possible?
Example in tJavaRow:
if ( first == true ) {
output_row = "my additions\n" + globalMap.get("column names") + input_row;
first = false;
} else {
output_row = input_row;
}

Thanks
Guenter
One Star

Re: write header "by hand"

I tried to access the header definition with (String)globalMap.get("__INCLUDEHEADER__") but this gives a null-result.
Seventeen Stars

Re: write header "by hand"

hi,
try to read file with header = 0 to cach header like a "normal data"
regards
laurent
One Star

Re: write header "by hand"

Hi guenniac,
Are you looking to change the wording of the header or add a header with more or less fields?
If it is the first case then you need to turn off the heading row in tFileOutputDelimited and generate a new heading yourself.
I would generate a single record using tFixedFlowInput with the same schema, but all set to string and set the values to the new column names that you want.
You can then then merge this record into the main flow using tUnite.
Make sure that the header is the first row connected to tUnite, so that it is the first record in the file on output.
Note: if your input data contains non-string fields then you'll need to add tConvertType to the flow to convert the flow to all strings (autocast makes this easy). I also needed to add a null tMap into the flow after tConvertType since not having it caused all the data to be written as blanks.
If it is the second case where you want a different number of fields then you should use the tFileOutputMSDelimited component that enables multiple types of records to be written to the output file.
Hope this helps,
Regards,
Rick
One Star

Re: write header "by hand"

What I want is:
I'd like to add one or more rows before the column header, which would look like this:
first row (free text)
second row (free text)
ColA ColB ColC (this is the standard header with the column names, I have set this in the input schema)
data rows
....
Since the tFileOutputDelimited cannot append a header to files I must write the header myself (see example above). I'd like to avoid this in order not to define the column names twice (once in the input schema and once afterwards when generating the header). So my question is: can I reuse the column names from the input schema for writing the header line myself?
@kzone: the original header is useless, I have set it in the input schema
When I move the mouse over the "inclued header" check box in the tFileOutputDelimited, the text __INCLUDEHEADER__. Is this name of a variable which I can use in the java-code?
Regards,
Günter
Six Stars

Re: write header "by hand"

Hi guenniac,
If I understand your issue in right way, following could solve it for you.
This is my test file input:
"DATE"
"DATE"
"12/29/2007"
"12/30/2007"
In the end this is the test file output:
custom header row 1
custom header row 2
"DATE"
"12/29/2007"
"12/30/2007"
I use tFileInputDelimited, read also the header row as standard data, so the header is set to 0, I also set up one context variable named context.headerRed of type boolean (default value false) which will tell me if the header was already red from the input file.
Second component is tJavaRow where is following code:
if (context.headerRed == false){
// this will process only for the first row from input file, so the header so I will tell to myself that I read header
context.headerRed = true;
// here i enhance header with custom rows
output_row.data = "custom header row 1\ncustom header row 2 \n" + input_row.data;
}
else{
// process standard output
output_row.data = input_row.data;
}
Finally I use standard tFileOuputDelimited and that's it.
Is that kind of behavior what you expected?
Best regards,
archenroot
One Star

Re: write header "by hand"

This is a SIMPLE example:
input file:
Col1;Col2;Col3
1;2;3
4;5;6
what I get as output is:
Col1 Col2 Col3
1 2 3
4 5 6
What I want as output is:
header line1
header line2
...
Col1 Col2 Col3
1 2 3
4 5 6
All my articles before where approaches to get this result.
I know that I can produce this by using the approach of archenroot, but this is error-prone, since I have to maintain the header completely manually. By changing the input schema I have to adapt my code in tJavaRow.
One Star

Re: write header "by hand"

Hi guenniac,
My first instinct was to suggest tFileOuptutMSDelimited, however after spending a little time with the component it doesn't seem to behave very well.
Next I thought that you could use tUnite, however this requires both new header lines and original files to have the same schema, which doesn't sound like what you are trying to achieve.
In the end I went for a simple solution:
1. Process your new header rows. I read mine from a text file using tFileInputFullRow and write to the a file "newfile.txt" using tfileoutputdelimited.
2. OnSubJobOK process the main file (again I simply read mine from a file) and wrote the the same file as in the first subjob ("newfile.txt"), but this time I changed the delimiter to "\t" and ticked the Append box.
This gave me the results as expected and will allow you to manipulate both the headers and the file data programatically prior to merging into the output file. It also means that you could, using context variables, use different headings for different input files.
Hope this helps,
Regards,
Rick