问题
i have a stream of json in apache nifi that contain dynamic fields (maximum 11 fields) and i want to convert it to csv file.
sample json:
{
"field1":"some text",
"field2":"some text",
"field3":"some text",
"field4":"some text",
"field5":"some text",
"field6":"some text",
"field7":"some text"
}
i don't wanna using replace or json evaluate; how i do it with ConvertRecord? using this processor is so odd and hard to work...
Clear expression about dynamic fields: i have 11 fields at total. one record may have contain 7 fields, and next record may contain 11 fields and next 9 fields...
回答1:
The steps provided below will help you in getting this done.:
- Connect your source processor which generates/outputs the JSON files to
ConvertRecord. - Configure
ConvertRecordand set 'Record Reader' to useJsonTreeReadercontroller service and 'Record Writer' to useCSVRecordSetWritercontroller service - Configure both the controller services and set
Schema Registryproperty to useAvroSchemaRegistry - Configure
AvroSchemaRegistry. Go to 'Properties' tab and click the + button which lets you add a dynamic property. - Give some property name (ex:
mySchema) and for the value, give the Avro schema expected for your input JSON. (You can useInferAvroSchemaprocessor to generate Avro schema for your JSON) - Configure both
JsonTreeReaderandCsvRecordSetWriterand set the 'Schema Name' property to the name provided above, in this case,mySchema. - Connect the relationships of
ConvertRecordto downstream processors according to your need.
来源:https://stackoverflow.com/questions/50577612/nifi-convert-json-to-csv-using-convertrecord