โ06-25-2020 06:26 AM
Iโm querying a database thatโs returning 2 columns, id and color. My requirement is to save this data to a csv file. The first 2 rows of the csv file must have these values, row1 โ&CID=555573โ, row2 โ&SUBID=266690โ. The data should have no header row. This is what the csv file should end up looking like if I brought back 2 records. How do I accomplish the hard-coded header rows 1 and 2? The csv formatter doesnโt seem to have the configs I need, I looked into the Document to Binary snap, but cannot figure it out.
โ06-25-2020 09:17 AM
Hi @gg433,
Iโve attached a pipeline to illustrate my thinking process here.
TwoRowsOnTopCSV_2020_06_25.slp (9.7 KB)
First I generate some test data in two JSON Generators. Then I combine their outputs with a Gate Snap. The Gate Snap outputs the documents it receives on input in arrays. It helps if you name the inputs of the Gate Snap, because then you can refer to the outputted arrays by name. Like I do, $top (for the aggregated documents we intend to put on top in the CSV file) and $bottom for the array containing the dummy data from the DB. In your case you would have the documents from the DB normally in the output of whichever snap you use, no need to transform it with a JSON Generator. Then in the Mapper Snap following, i have this expression:
$top.concat($bottom)
and I assign it to $, as a Target Path. Then I simply use a JSON Splitter to split the array, after that a CSV Formatter Snap, and so on.
One thing to note. In the top JSON Generator at the beginning, the field key for โ&CID=555573โ and โ&SUBID=266690โ is โidโ. That, and another thing. In the CSV Formatter Snap have the โUse headerโ option unchecked.
Hope this helps.
BR,
Dimitri
โ06-25-2020 09:42 AM
Dimitri, that is simply a perfect solution, well done! I did not even know about the Gate snap, but its obviously very powerful. Thank you so much for your response here.
โ02-06-2024 07:16 AM
Here there, is it possible to add the headers at the third line (After the two first rows)?