如何在NiFi中将属性拆分为嵌套JSON结构?
如何在NiFi中将扁平属性转换为嵌套JSON结构?
完全可以实现,不需要继续使用扁平格式。NiFi原生提供了JoltTransformJSON处理器,专门用于JSON结构的转换,配置简单,无需编写复杂代码。
具体实现步骤
- 在现有流程中,将
AttributesToJSON处理器的输出连接到JoltTransformJSON处理器(放在AttributesToJSON和HandleHttpResponse之间)。 - 配置
JoltTransformJSON的核心参数:- 在Jolt Specification中,使用Shift变换规则来构建嵌套结构。针对你的场景,有两种配置方式:
- 精准匹配规则(针对已知属性):
[ { "operation": "shift", "spec": { "http.request.uri": "http.request.uri", "http.context.identifier": "http.context.identifier", "http.remote.host": "http.remote.host", "http.headers.Host": "http.headers.Host", "http.local.name": "http.local.name" } } ] - 通用匹配规则(自动适配所有
http.前缀的属性):[ { "operation": "shift", "spec": { "http.*.*": "http.&1.&2", "http.*": "http.&1" } } ]
- 精准匹配规则(针对已知属性):
- 在Jolt Specification中,使用Shift变换规则来构建嵌套结构。针对你的场景,有两种配置方式:
- 保存配置并启动处理器,此时输出的JSON会自动转换为你需要的嵌套结构:
{ "http": { "request": { "uri": "/hello/1.1.1.1" }, "context": { "identifier": "92906daf-152b-4af2-90a4-c2e455e2a52d" }, "remote": { "host": "172.19.0.1" }, "headers": { "Host": "127.0.0.1:16543" }, "local": { "name": "172.19.0.4" } } }
替代方案(不推荐)
如果你偏好脚本实现,可以使用ExecuteScript处理器编写Groovy/Java代码手动构建嵌套JSON,但需要处理属性遍历和结构组装,复杂度高于Jolt的原生配置。
内容的提问来源于stack exchange,提问作者WoJ
相关产品推荐
相关产品推荐

