Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NiFi SplitJSON and ExecuteSQL

Tags:

apache-nifi

In a NiFi flow, I want to read a JSON structure, split it, use the payload to execute a SQL query, and finally output each result in a JSON file.

However I am having problems retrieving the value of the splitted FlowFile's attribute in the ExecuteSQL processor.

enter image description here

  1. GenerateFlowFile processor, with a JSON structure as Custom Text

enter image description here

  1. SplitJSON, with data as the attribute to be splitted

enter image description here

  1. ExecuteSQL, with the query I am trying to execute, using the SplitJSON's payload attribute ${id}

enter image description here

At this point I am getting a log error of a SQL syntax error. Apparently the ExecuteQuery processor is not parsing the expression on its SQL select query property.

Is there any intermediate processing left to be done after splitting the JSON? What am I missing?

like image 859
TMichel Avatar asked Jul 18 '17 16:07

TMichel


1 Answers

You need an additional EvaluateJsonPath (or ExtractText) processor between SplitJson and ExecuteSQL -- the Expression Language expression cannot evaluate the flowfile content for parameter substitution, so your SQL expression ends up being SELECT * FROM foo WHERE id = ; which is why you have a syntax error.

Expression Language reads from flowfile attributes, so you need to parse the JSON content into an accessible attribute. The EvaluateJsonPath processor does exactly this. All you need to do is add a custom property (click the + on the top right of the properties dialog) and extract the id value from the JSON into a flowfile attribute. Then connect the matched relationship from this processor to the ExecuteSQL processor.

EvaluateJsonPath processor with custom property

like image 66
Andy Avatar answered Nov 03 '22 14:11

Andy