Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NiFi QueryRecord processor - select optional JSON attribute

I got several JSON objects going through a QueryRecord processor. I want to extract some attributes from it including some optional ones. The problem is that selecting an optional attribute results in an error saying that the column is not present in the table.

I tried a workaround using RPATH which looked like this:

SELECT is_answered,
       view_count,
       answer_count,
       score,
       last_activity_date,
       creation_date,
       question_id,
       title,
       CASE
         WHEN HAS_PATH(flowfile, '/last_edit_date') THEN
          RPATH(flowfile, '/last_edit_date')
         ELSE
          NULL
       END AS last_edit_date
  FROM flowfile

In this case last_edit_date is an optional attribute. This however throws an error that flowfile column does not exists. I have tried to find another workaround which would not include flowfile in the select statement however I've not been able to find anything. Any ideas on how to solve this issue

like image 412
donut123 Avatar asked Dec 29 '25 21:12

donut123


1 Answers

The SQL part of QueryRecord uses the content of the FlowFile, so HAS_PATH is looking for that field in the record not the attributes. Instead, use Expression Language (probably the isEmpty() function) to generate that part of the query, maybe something like:

SELECT is_answered, 
  view_count, 
  answer_count, 
  score, 
  last_activity_date, 
  creation_date, 
  question_id, 
  title,
  CAST("${last_edit_date:isEmpty():ifElse(null,${last_edit_date})}" AS DATE) AS last_edit_date
FROM FLOWFILE

I didn't test this but it should give you the general idea of how to use Expression Language to get at attributes and inject the value into the query.

like image 113
mattyb Avatar answered Dec 31 '25 14:12

mattyb



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!