简体   繁体   English

具有多个输入的Apache Nifi EvaluateJsonPath

[英]Apache Nifi EvaluateJsonPath with Multiple Inputs

I have JSON objects coming into Nifi via MQTT from two different inputs - for instance, let's say one is from a top sensor, and one is from a bottom sensor. 我有来自两个不同输入的JSON对象通过MQTT通过NTT进入Nifi-例如,假设一个来自顶部传感器,一个来自底部传感器。 Each of the sensors has its own MQTT topic, so I am using two different ConsumeMQTT Processors to ingest this data into my Nifi Flow. 每个传感器都有其自己的MQTT主题,因此我正在使用两个不同的ConsumeMQTT处理器将此数据提取到我的Nifi流中。

JSON Object for top sensor is {"Top_Data": "value"} 顶部传感器的JSON对象{"Top_Data": "value"}

JSON Object for bottom sensor is {"Bottom_Data": "value"} 底部传感器的JSON对象{"Bottom_Data": "value"}

I am currently using two separate EvaluateJsonPath Processors to store either the value of Top_Data or Bottom_Data in an attribute called sensorData. 我目前正在使用两个单独的EvaluateJsonPath处理器将Top_DataBottom_Data的值存储在名为sensorData的属性中。

How can I use some kind of if/or statement to only use one processor to EvaluateJsonPath for both of the JSON objects I could get from MQTT? 我如何使用某种if / or语句仅使用一个处理器来处理我可以从MQTT获得的两个JSON对象的EvaluateJsonPath? Basically, I want to have an expression that says "If my JSON object has a property called Top_Data, use its value for the attribute sensorData, otherwise, use the value from the property Bottom_Data." 基本上,我希望有一个表达式表示“如果我的JSON对象具有一个名为Top_Data的属性,则将其值用于属性sensorData,否则,请使用属性Bottom_Data中的值。”

Example of my EvaluateJsonPath Processor 我的EvaluateJsonPath处理器示例

maybe try JSONPath expression 也许尝试JSONPath表达式

$[Top_Data,Bottom_Data]

in the single EvaluateJSONPathProcessor . 在单个EvaluateJSONPathProcessor According to https://goessner.net/articles/JsonPath/ there is a possibility to use alternate operator [,] : 根据https://goessner.net/articles/JsonPath/ ,可以使用备用运算符[,]

[,] Union operator in XPath results in a combination of node sets. XPath中的[,] Union运算符可导致节点集的组合。 JSONPath allows alternate names or array indices as a set. JSONPath允许将备用名称或数组索引作为一组。

I have tested the expression using http://jsonpath.com/ and it should work. 我已经使用http://jsonpath.com/测试了该表达式,它应该可以工作。

Let us know if that helps. 让我们知道是否有帮助。

You could try extracting them both using EvaluateJsonPath (property 1: top: $['top'] , property 2: bottom: $['bottom'] ) and of course don't forget to set Destination to flowfile-attribute . 您可以尝试使用EvaluateJsonPath (属性1: top: $['top'] ,属性2: bottom: $['bottom'] )提取它们,当然不要忘记将Destination设置为flowfile-attribute

Then, transfer to UpdateAttribute and set property finalData as ${top:isEmpty():ifElse(${bottom}, ${top})} . 然后,传输到UpdateAttribute并将属性finalData设置为${top:isEmpty():ifElse(${bottom}, ${top})}

If EvaluateJsonPath won't find a full element, then it will set it as empty string, so all you need to do is check if either of them is empty and if it is, set the final data as the other one. 如果EvaluateJsonPath找不到完整的元素,则会将其设置为空字符串,因此您所需要做的就是检查它们中的任何一个是否为空,然后将最终数据设置为另一个。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM