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

Apache NiFi中JOLT JSON转换Schema失效,请求修正支持

Apache NiFi JOLT转换:从CouchDB返回格式提取目标字段数组

我最近在Apache NiFi里用JOLT做JSON转换时遇到了问题——想把CouchDB返回的嵌套JSON转成只包含指定字段的数组,但写的规则一直返回null。以下是我的场景:

原始JSON

{
  "total_rows": 5884,
  "offset": 0,
  "rows": [
    {
      "id": "03888c0ab40c32451a018be6b409eba3",
      "key": "03888c0ab40c32451a018be6b409eba3",
      "value": { "rev": "1-d5cc089dd8682422962ccab4f24bd21b" },
      "doc": {
        "_id": "03888c0ab40c32451a018be6b409eba3",
        "_rev": "1-d5cc089dd8682422962ccab4f24bd21b",
        "topic": "iot-2/type/home-iot/id/1234/evt/temp/fmt/json",
        "payload": { "temperature": 36 },
        "deviceId": "1234",
        "deviceType": "home-iot",
        "eventType": "temp",
        "format": "json"
      }
    },
    {
      "id": "03888c0ab40c32451a018be6b409f163",
      "key": "03888c0ab40c32451a018be6b409f163",
      "value": { "rev": "1-dee82cbb1b5ffa8a5e974135eb6340c5" },
      "doc": {
        "_id": "03888c0ab40c32451a018be6b409f163",
        "_rev": "1-dee82cbb1b5ffa8a5e974135eb6340c5",
        "topic": "iot-2/type/home-iot/id/1234/evt/temp/fmt/json",
        "payload": { "temperature": 22 },
        "deviceId": "1234",
        "deviceType": "home-iot",
        "eventType": "temp",
        "format": "json"
      }
    }
  ]
}

期望输出

[
  {
    "temperature": 36,
    "deviceId": "1234",
    "deviceType": "home-iot",
    "eventType": "temp"
  },
  {
    "temperature": 22,
    "deviceId": "1234",
    "deviceType": "home-iot",
    "eventType": "temp"
  }
]

我尝试的JOLT规则(返回null)

[
  {
    "operation": "shift",
    "spec": {
      "rows": {
        "*": {
          "doc": {
            "deviceId": "[&1].deviceId",
            "deviceType": "[&1].deviceType",
            "eventType": "[&1].eventType",
            "payload": { "*": "[&1]" }
          }
        }
      }
    }
  }
]

修正后的JOLT规则

问题出在两个地方:一是你用了HTML转义的&而不是JOLT原生的&;二是层级引用的数字不对。以下是可以得到期望结果的规则:

[
  {
    "operation": "shift",
    "spec": {
      "rows": {
        "*": {
          "doc": {
            "deviceId": "[&2].deviceId",
            "deviceType": "[&2].deviceType",
            "eventType": "[&2].eventType",
            "payload": {
              "temperature": "[&3].temperature"
            }
          }
        }
      }
    }
  }
]

规则解释

  • 层级引用数字:JOLT里的&n表示往上数第n层的匹配节点。比如在doc下的deviceId字段,要对应到rows数组的索引(也就是*的位置),需要往上跳2层:当前层是deviceId,上一层是doc,再上一层是*(rows的元素),所以用&2来获取这个索引,生成对应的数组项。
  • payload里的temperature:从temperature字段往上数,第一层是payload,第二层是doc,第三层是*,所以用&3来把temperature值放到同一个数组项里。
  • 转义问题:你之前写的&1是HTML转义后的符号,JOLT解析时会把它当成普通字符串,无法识别为层级引用,所以要直接用&。

这个规则运行后就能得到你想要的数组格式啦。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.12 05:12:02