cancel
Showing results for 
Search instead for 
Did you mean: 

Insert 2 rows at the top of a csv file

gg433
New Contributor III

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.

csv_header

3 REPLIES 3

dimitri_hristov
Contributor

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

gg433
New Contributor III

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.

mnduwayo
New Contributor II

Here there, is it possible to add the headers at the third line (After the two first rows)?