首页 > 解决方案 > Apache Nifi - 拆分数组并格式化为 JSON?

问题描述

可能是一个简单的初学者问题:

使用 NIFI,我想以以下形式拆分一个数组(表示 flowfile-content)

["x1","x2", ..]

并将其格式化为表单的 JSON 对象

{"key1":"x1", "key2":"x2", ..}

(也作为流文件内容)

使用什么处理器最有效,表达式脚本会是什么样子?

提前致谢, 马克

标签: apache-nifijolt

解决方案


通过使用JoltTransformJSON处理器应用Jolt转换可能是一个不错的选择。在其中,您可以通过单击“配置处理器”对话框的“设置”选项卡中所述的“高级”按钮在下面添加这样的规范:

[
  //Determine respective lists to be used to calculate their sizes within the next step 
  //in order to get ordinals for the key values
  {
    "operation": "shift",
    "spec": {
      "*": {
        "*": "&(0,0).[#2]"
      }
    }
  },
  {
    "operation": "modify-overwrite-beta",
    "spec": {
      "*": "=size(@(1,&))"
    }
  },
  //exchange key and values
  {
    "operation": "shift",
    "spec": {
      "*": {
        "$": "@(0)"
      }
    }
  },
  //add the desired word such as "key"
  {
    "operation": "shift",
    "spec": {
      "*": "key&(0,0)"
    }
  }
]

推荐阅读