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

如何在Apache NiFi中用JOLTTransformJSON为JSON添加来源属性

Apache NiFi JOLTTransformJSON 添加来源字段问题解决

需求:用GetFile读取帖子、广告、视频4个JSON文件,合并后给每个对象的string_map_data层级(与Author、Time同级)添加type_interaction字段,值为Update Attribute处理后的${filename:substringBeforeLast('.')}。当前JOLT规则无法实现该需求。

原JSON示例

{
  "impressions_history_ads_seen": [
    {
      "string_map_data": {
        "Time": {
          "timestamp": 1705789300
        }
      }
    },
    {
      "string_map_data": {
        "Time": {
          "timestamp": 1705789746
        }
      }
    },
    {
      "string_map_data": {
        "Author": {
          "value": "twdss"
        },
        "Time": {
          "timestamp": 1705790493
        }
      }
    },
    {
      "string_map_data": {
        "Author": {
          "value": "mitprofessionaled"
        },
        "Time": {
          "timestamp": 1705809684
        }
      }
    }
  ]
}

Update Attribute 配置

filename = ${filename:substringBeforeLast('.')}

现有JOLTTransformJSON 配置(无效)

[
  {
    "operation": "shift",
    "spec": {
      "*": {
        "*": {
            "*":&,
            "type_interaction":"${filename}"
        }
      }
    }
  }
]

正确JOLT配置

原有规则未准确定位到string_map_data层级,导致新字段未添加到目标位置。调整后的JOLT规则如下:

[
  {
    "operation": "shift",
    "spec": {
      "*": {
        "*": {
          "string_map_data": {
            "@": "&3.&2.&", // 保留string_map_data下原有所有字段
            "type_interaction": "${filename}" // 新增type_interaction字段,取值为NiFi属性filename
          }
        }
      }
    }
  }
]

规则说明

  • &3 指代最外层的父节点(如impressions_history_ads_seen)
  • &2 指代数组中的当前元素索引
  • @ 表示保留当前string_map_data下的所有原有键值对
  • ${filename} 直接引用NiFi中已处理后的属性值,确保来源信息正确添加到目标层级

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.30 14:18:27