thinice
thinice

Reputation: 710

How to use ConvertRecord and CSVRecordSetWriter to output header (with no data) in Apache NiFi?

I'm working with Apache NiFi to pull data from a database table and use the ConvertRecord to change the fetched records from avro to CSV file using AvroReader and CSVRecordSetWriter.

My process works fine when at least one row comes back from the database.

However, when the ExecuteSQL processor outputs a FlowFile containing 0 records (the FlowFile still contains the metadata/avro schema) - ConvertRecord gives me a 0-byte FlowFile output. My settings in the CSVRecordSetWriter service are set to output the header.

Is there a way to have ConvertRecord output a FlowFile that contains just the the header line in this circumstance?

Upvotes: 0

Views: 2516

Answers (1)

notNull
notNull

Reputation: 31490

ExecuteSql Processor adds executesql.row.count(Contains the number of rows returned in the select query) attribute to the flow.

You can check value of the attribute(executesql.row.count) and take decision do we need to use ConvertRecord processor or not.

Flow:

Executesql(success)--> RouteOnAttribute(add new property ${executesql.row.count:equals(0)}) 
     --> matched --> ReplaceText //Add Header
     --> unmatched --> ConvertRecord //convert to csv format

if matched then use ReplaceText processor to overwrite the existing flowfile content with header.

if Unmatched that means you are having some rows of data in the flowfile content so use ConvertRecord processor and prepare csv data.

Upvotes: 3

Related Questions