NiFi转换avro到JSON数组格式与嵌套数组

eufgjt7s  于 2023-02-14  发布在  其他
关注(0)|答案(1)|浏览(152)

Nifi新手,希望看看我们是否可以将SQL的结果转换为所请求格式的JSON。
SQL结果为:
| 成员标识|字段2|总计|税|船|帕尔特姆|价格|数量|
| - ------|- ------|- ------|- ------|- ------|- ------|- ------|- ------|
| 八七四四五○九六三|小行星24017|一百七十三块九五|无|无|小零一五九三五九六六|四十二|1个|
| 八七四四五○九六三|小行星24017|一百七十三块九五|无|无|000756009|三十二|1个|
| 八七四四五○九六三|小行星24017|一百七十三块九五|无|无|小零一二一七九二九三|九十九块九五|1个|
使用***ConvertRecord***或***ConvertAvroToJSON***流程时,开箱即用的结果如下所示:

[
  {
    "FIELD2": "24017",
    "MEMBER_ID": "874450963",
    "PARTNUM": "015935966",
    "PRICE": "42",
    "QUANTITY": "1",
    "TIMEPLACED": null,
    "TOTALPRODUCT": "173.95",
    "TOTALSHIPPING": "0",
    "TOTALTAX": "0"
  },
  {
    "FIELD2": "24017",
    "MEMBER_ID": "874450963",
    "PARTNUM": "000756009",
    "PRICE": "32",
    "QUANTITY": "1",
    "TIMEPLACED": null,
    "TOTALPRODUCT": "173.95",
    "TOTALSHIPPING": "0",
    "TOTALTAX": "0"
  },
  {
    "FIELD2": "24017",
    "MEMBER_ID": "874450963",
    "PARTNUM": "012179293",
    "PRICE": "99.95",
    "QUANTITY": "1",
    "TIMEPLACED": null,
    "TOTALPRODUCT": "173.95",
    "TOTALSHIPPING": "0",
    "TOTALTAX": "0"
  }
]

请求是将它们分组,并将partnum、price和quantity作为一个数组,如下所示:

[
  {
    "FIELD2": "24017",
    "MEMBER_ID": "874450963",
    "TIMEPLACED": null,
    "TOTALPRODUCT": "173.95",
    "TOTALSHIPPING": "0",
    "TOTALTAX": "0",
    "itemDetails": [
      {
        "PARTNUM": "015935966",
        "PRICE": "42",
        "QUANTITY": "1"
      },
      {
        "PARTNUM": "000756009",
        "PRICE": "32",
        "QUANTITY": "1"
      },
      {
        "PARTNUM": "012179293",
        "PRICE": "99.95",
        "QUANTITY": "1"
      }
    ]
  }
]

我们该怎么做呢?
谢谢!
搜索论坛,未发现任何类似内容。

dgiusagp

dgiusagp1#

一个选项是使用***JoltTransformJSON***进程执行此转换,该进程具有以下规范:

[
  {
    "operation": "shift",
    "spec": {
      "*": {
        "*": "&", // form arrays for the attibutes with keys other than below ones 
        "PARTNUM|PRICE|QUANTITY": "itemDetails[&1].&"
      }
    }
  },
  {
    "operation": "cardinality",
    "spec": {
      "*": "ONE", // pick only one of the repeating components 
      "itemDetails": "MANY"
    }
  },
  {
    "operation": "sort"
  }
]

相关问题