NiFi:JsonQueryElasticsearch缺失结果转属性功能,求简便替代方案
解决NiFi新版JsonQueryElasticsearch组件设置搜索结果为FlowFile属性的简便方法
方法1:使用ExecuteScript组件直接处理
在JsonQueryElasticsearch之后串联ExecuteScript组件,用脚本直接解析ES返回的JSON内容,将目标字段设置为FlowFile属性,无需额外合并步骤。
- 示例Groovy核心逻辑:
import groovy.json.JsonSlurper def flowFile = session.get() if (!flowFile) return def jsonContent = new JsonSlurper().parseText(flowFile.read().getText("UTF-8")) // 按需提取hits中的目标字段,示例取第一个hit的指定字段 def resultValue = jsonContent.hits.hits[0]._source.target_field flowFile = session.putAttribute(flowFile, "es_search_result", resultValue.toString()) session.transfer(flowFile, REL_SUCCESS)
- 优势:单组件完成解析与属性设置,流程更简洁。
方法2:UpdateAttribute结合JsonPath表达式直接提取
新版NiFi的UpdateAttribute支持直接通过JsonPath从FlowFile内容取值并赋值为属性,无需单独使用EvaluateJsonPath:
- 在
JsonQueryElasticsearch后添加UpdateAttribute组件 - 添加自定义属性(比如
es_result),属性值填写:${json-path:evaluate('$.hits.hits[0]._source.target_field', content)} - 配置完成后,组件会自动读取FlowFile的JSON内容,提取值并设置为指定属性
方法3:自定义处理器(进阶场景)
如果需要重复复用该逻辑,可基于NiFi API开发自定义处理器,封装ES结果提取到属性的逻辑,适合有开发能力的团队长期使用。
内容的提问来源于stack exchange,提问作者Renison Correya
相关产品推荐
相关产品推荐

