如何使用Jolt Specification将Json转换为Json?

时间:2019-02-25 12:32:42

标签: java json apache-nifi jolt

我有JSON这样的有效载荷;

   [ {
      "Samples" : {
        "Load" : [ {
          "dataItemId" : "a5",
          "timestamp" : "2019-02-17T04:58:44.097Z",
          "name" : "Aload",
          "sequence" : "19",
          "subType" : null,
          "content" : null
        }, {
          "dataItemId" : "a7",
          "timestamp" : "2019-02-17T04:58:44.097Z",
          "name" : "AAA",
          "sequence" : "19",
          "subType" : null,
          "content" : null
        } ],
        "Angle" : [ {
          "dataItemId" : "a6",
          "timestamp" : "2019-02-17T04:58:44.097Z",
          "name" : "Aact",
          "sequence" : "20",
          "subType" : "ACTUAL",
          "content" : null
        } ]
      }
    } ]

我想这样收到JSON

{
      "Samples" : [
            {
              "tag_name": "Load",
              "dataItemId" : "a5",
              "timestamp" : "2019-02-17T04:58:44.097Z",
              "name" : "Aload",
              "sequence" : "19",
              "subType" : null,
              "content" : null
            }, {
              "tag_name": "Load",
              "dataItemId" : "a7",
              "timestamp" : "2019-02-17T04:58:44.097Z",
              "name" : "AAA",
              "sequence" : "19",
              "subType" : null,
              "content" : null
            }, {
              "tag_name": "Angle",
              "dataItemId" : "a6",
              "timestamp" : "2019-02-17T04:58:44.097Z",
              "name" : "Aact",
              "sequence" : "20",
              "subType" : "ACTUAL",
              "content" : null
            }
      ]
}

在我的场景中,我必须转换上面定义的每个json数据。我每秒收到 500个JSON数据。如何使用Jolt Specification执行此操作?震动规格是否快速?它是否适合流式传输?还是我应该为此编写我的自己的脚本

1 个答案:

答案 0 :(得分:3)

如果您的有效载荷是具有单个对象的数组,则此规范适用:

[
  {
    "operation": "shift",
    "spec": {
      "*": {
        "Samples": {
          "*": {
            "*": {
              "$1": "Samples.&2[#2].tag_name",
              "*": "Samples.&2[#2].&"
            }
          }
        }
      }
    }
  },
  {
    "operation": "shift",
    "spec": {
      "Samples": {
        "*": {
          "*": "Samples[]"
        }
      }
    }
  }
]

否则,您可能需要拆分数组,对每个元素进行转换,然后使用MergeContent或MergeRecord将它们重新组合在一起。另外,您可以使用JoltTransformRecord将规范应用到数组中的每个元素/记录,只需更改规范即可在第一个*操作中删除初始的shift部分。