Jolt json 转换问题。有一个记录里面有一个数组,需要转换成一个记录数组

Jolt json transform question. Have a record with an array inside and need to convert to an array of records

我需要在 NIFI 中转换一些流文件,并且想使用 Jolt 转换。我已经玩了一段时间了,想不出一个可行的转换。我有...

{
  "asset": "transformer1",
  "readings": {
    "oil_temp": 0.7499248991503604,
    "otc_top_temp": 0.2870503485615279
  },
  "timestamp": "02/17/2020 19:07:31",
  "key": "e2af07ab-4d21-4a70-ba3f-9113bb335d1e"
}

并且需要将其转换为这样的记录数组...

[
  {
    "tag": "transformer1.oil_temp",
    "timestamp": "02/17/2020 19:07:31",
    "value": 0.7499248991503604
  },
  {
    "tag": "transformer1.otc_top_temp",
    "timestamp": "02/17/2020 19:07:31",
    "value": 0.2870503485615279
  }
]

我希望这是可能的。我将不胜感激任何帮助。谢谢。

您可以通过多个转换步骤实现此目的:

  1. 读数映射到列表
  2. 使用连接操作创建新的标记字段
  3. 结果只传递您想要的值

震动规格:

[
  {
    "operation": "shift",
    "spec": {
      "readings": {
        "*": {
          "@(2,asset)": "[#2].asset",
          "$": "[#2].name",
          "@": "[#2].value",
          "@(2,timestamp)": "[#2].timestamp"
        }
      }
    }
  }, {
    "operation": "modify-default-beta",
    "spec": {
      "*": {
        "tag": "=concat(@(1,asset),'.',@(1,name))"
      }
    }
  }, {
    "operation": "shift",
    "spec": {
      "*": {
        "tag": "[&1].tag",
        "timestamp": "[&1].timestamp",
        "value": "[&1].value"
      }
    }
  }
]

输入:

{
  "asset": "transformer1",
  "readings": {
    "oil_temp": 0.7499248991503604,
    "otc_top_temp": 0.2870503485615279
  },
  "timestamp": "02/17/2020 19:07:31",
  "key": "e2af07ab-4d21-4a70-ba3f-9113bb335d1e"
}

输出:

[ {
  "tag" : "transformer1.oil_temp",
  "timestamp" : "02/17/2020 19:07:31",
  "value" : 0.7499248991503604
}, {
  "tag" : "transformer1.otc_top_temp",
  "timestamp" : "02/17/2020 19:07:31",
  "value" : 0.2870503485615279
} ]

在这里测试:https://jolt-demo.appspot.com/#inception