0

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?

1
  • One question: ExecuteSQL -> CovertRecord is good but can you use QueryDatabaseRecord instead since that combines both of them together? Commented Sep 14, 2018 at 5:43

1 Answer 1

3

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.

Sign up to request clarification or add additional context in comments.

5 Comments

This same capability will be offered as a property in ConvertRecord in NiFi 1.8.0 (via NIFI-5382).
@mattyb I was looking at that but it wasn't 100% clear if it would get that header to populate
I considered an approach like that, I wanted to keep things closer to the query (as I have many of these in a template situation); As a kind of 'workaround' I ended up using a UNION SELECT null as 'Foo' pattern, and then doing a regex replace with ReplaceText. A little extra work but seems to work. I look forward to 1.8.0 to see if that simplifies this.
@Shu when you say "content with header" are you saying the replacement text is a static string of the header, or is there some slick way to extrapolate the Avro schema metadata into the CSV compliant delimited format?
@thinice one way is using Static string of the header (or) another way is to use ExtractAvroMetaData processor and extract avro.schema from the avro data file then use ReplaceText the content of flowfile with avro.schema value. The second way is dynamic based on the avro data file's avro.schema Metadata Keys

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.