Support Questions

Find answers, ask questions, and share your expertise

Need Help to generate CSV file from JSON

avatar
New Contributor

I am new to Nifi and exploring Nifi to generate CSV file from a database table and as part of this exercise using 'QueryDatabaseTable' processor to extract data and then converting to JSON flowfile-attribute with the following sequence of processors: QueryDatabaseTable--> ConvertAvrotoJSON-->EvaluateJSONPath-->AttributestoJSON-->ReplaceText-->PutFile

And below is the sample records output claim from the data provenance of 'AttributesToJSON' processor

{"Code": "ABC", "Desc": "ABC DESC"}

{"Code": "DEF", "Desc": "DEF DESC"}

{"Code": "XYZ", "Desc": "XYZ DESC"}

Target CSV file has to be as follows along with header

Code, Desc (Header)

ABC,ABC DESC

DEF,DEF DESC

XYZ,XYZ DESC

Can you help me to configure required properties for "ReplaceText" processor like Search Value, Replacement Value

Any processor/inputs/examples on how to generate CSV file output directly from Database table?

4 REPLIES 4

avatar
Guru
What version of NiFi are you using? If you are on 1.2.0 or later, you can make use of the new Record Reader/Writer capabilities. Check out the HCC article I wrote showing CSV to JSON to learn more: https://community.hortonworks.com/content/kbentry/115311/convert-csv-to-json-avro-xml-using-convertr...

There are some URLs at the end for other articles about the Record Readers/Writers. In your scenario, you would use the AvroReader and the CSVRecordSetWriter in your ConvertRecord processor

avatar
Super Collaborator

hi @G Sankar

To make the current flow working, you can remove the AttributetoJSON processor

now the flow would be

QueryDatabaseTable--> ConvertAvrotoJSON-->EvaluateJSONPath-->ReplaceText-->PutFile

EvaluteJSONPath will make sure that the results of those expressions are assigned to FlowFile Attributes, now we can replace the entire flow file content with attributes.

the parameters of the Replace text would be

Search Value : (^.*$)

replace values : ${col1},${col2},${col3}.. etc

col1,2,3 will be attributes of the flow file which extracted by evaluteJSONPath(you may see the flow file attributes in provenence ).

on the other note latest version of the NiFi offer the Convert Record processor where you can convert straight from Avro to CSV using controller service.

38563-nifi-avro-to-csv.jpg

avatar
New Contributor

Thanks Everyone, I will try and get back to you if any questions

,

Thanks Raju & Lim, l will try and let you know both if any questions

avatar
New Contributor

Hi, I tried to implement using ConvertRecord Processor and getting below error:

SchemaNotFoundException: Flow File did not contain appropriate attributes to determine Schema Name.