You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在NiFi中根据JSON对象类型路由至对应处理器?

在NiFi中根据JSON根对象类型路由数据的解决方案

你的问题核心是EvaluateJsonPath的~表达式仅返回完整JSON对象,无法直接提取根键(book/video/magazine)来做类型判断。下面是两种实用的解决方法:

方法1:用ExecuteScript提取根对象类型并添加属性

这个方法通过脚本直接读取JSON的根键,将其作为FlowFile属性,后续用RouteOnAttribute路由:

  1. 添加ExecuteScript处理器,选择Groovy作为脚本语言,输入以下脚本:
import groovy.json.JsonSlurper

def flowFile = session.get()
if (!flowFile) return

def jsonContent = flowFile.read().getText("UTF-8")
def jsonObject = new JsonSlurper().parseText(jsonContent)
def contentType = jsonObject.keySet().first()

flowFile = session.putAttribute(flowFile, "content_type", contentType)
session.transfer(flowFile, REL_SUCCESS)
  1. 添加RouteOnAttribute处理器,配置路由规则:
    • 新增属性route_book,值设为 ${content_type:equals('book')}
    • 新增属性route_video,值设为 ${content_type:equals('video')}
    • 新增属性route_magazine,值设为 ${content_type:equals('magazine')}
  2. 将RouteOnAttribute的对应路由关系连接到各自的目标处理器。

方法2:用JoltTransformJSON转换结构后提取类型

先通过Jolt将根键转换为明确字段,再用EvaluateJsonPath提取:

  1. 添加JoltTransformJSON处理器,配置Jolt规范为:
[
  {
    "operation": "shift",
    "spec": {
      "*": {
        "@": "content",
        "$": "type"
      }
    }
  }
]

这个规范会把原JSON转换为带type字段的结构,比如原book类型的JSON会变成:

{
  "content": {"id": "1234", "name": "book1"},
  "type": "book"
}
  1. 添加EvaluateJsonPath处理器,设置$.type为目标属性content_type(选择"Destination"为"flowfile-attribute")。
  2. 后续同样用RouteOnAttribute处理器根据content_type属性路由到对应处理器。

内容的提问来源于stack exchange,提问作者jkelley16

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.08 10:10:15