如何在Apache Nifi中使用jolt规范将json数组拆分为子数组?

txu3uszq  于 2023-06-25  发布在  Apache
关注(0)|答案(1)|浏览(190)

我特灵准备一个jolt规范的预期输出,请帮助我。

注意:

预期输出:如果credit在具有多个字段的数组中,则json将被拆分为subjson,其中InvoiceMonth将仅与input相同,但在第一个subjson中,InvoiceMonth值具有剩余的subjson InvoiceMonth值需要为0。
输入:

[
  {
    "InvoiceMonth": "202306",
    "credit": {
      "name": [
        "7828credit:0",
        "2738credit:0"
      ],
      "amount": [
        "2.5E-5",
        "5.0E-5"
      ],
      "full_name": [
        "Committed USAGE",
        "Committed USAGE "
      ],
      "id": [
        "7822credit:0",
        "2732credit:0"
      ],
      "type": [
        "COMMITTEDDISCOUNT",
        "COMMITTEDDISCOUNTBASE"
      ]
    },
    "resource_name": "projects-2",
    "resource_global_name": "//https.googleapis.com/",
    "label": ""
  }
]

输出:

[
  {
    "InvoiceMonth": "202306",
    "credit": {
      "name": "7822credit:0",
      "amount": "2.5E-5",
      "full_name": "Committed",
      "id": "7822credit:0",
      "type": "COMMITTEDUSAGE"
    },
    "resource_name": "projects-2",
    "resource_global_name": "//https.googleapis.com/",
    "label": ""
  },
  {
    "InvoiceMonth": "0",
    "credit": {
      "name": "2738credit:0",
      "amount": "5.0E-5",
      "full_name": "Committed ",
      "id": "2738_credit:0",
      "type": "COMMITTEDUSAGEDISCOUNT"
    },
    "resource_name": "projects-2",
    "resource_global_name": "//https.googleapis.com/",
    "label": ""
  }
]
ndh0cuux

ndh0cuux1#

您可以使用以下规范

[
  {
    "operation": "shift",
    "spec": {
      "*": {
        "credit": {
          "*": {
            "0": {
              "@3,InvoiceMonth": "[#2].InvoiceMonth", // go up the tree three levels to reach the level of the attribute and grab its value
              "@": "[#2].&3.&2",
              "@3,resource_name": "[#2].resource_name",
              "@3,resource_global_name": "[#2].resource_global_name",
              "@3,label": "[#2].label"
            },
            "*": {
              "#0": "[#2].InvoiceMonth", // all values expect for the first one for the attribute is set to zero
              "@": "[#2].&3.&2",
              "@3,resource_name": "[#2].resource_name",
              "@3,resource_global_name": "[#2].resource_global_name",
              "@3,label": "[#2].label"
            }
          }
        }
      }
    }
  },
  { // pick only one component from the reformed arrays with the multiple repeating components 
    "operation": "cardinality",
    "spec": {
      "*": {
        "*": "ONE"
      }
    }
  }
]

其中的主要思想是循环遍历"credit"对象下的数组,然后添加其他属性。

相关问题