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

huangapple go评论57阅读模式
英文:

How to split json array into sub array using jolt specifcation in Apache Nifi?

问题

我正在尝试为预期输出准备一个jolt规范,请帮助我。

注意:

预期输出:如果信用额度在数组中具有多个字段,则JSON将拆分为子JSON,其中InvoiceMonth将与输入相同,但在第一个子JSON中,InvoiceMonth的值保持不变,其余子JSON的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": ""
  }
]

如果您需要进一步的帮助,请告诉我。

英文:

I am tring to prepare a jolt specification for the expected output, please help me out.

Note:

The expected output: where credit is in array with multiple field, then json will split into subjson where InvoiceMonth will be same as input only but in 1st subjson the InvoiceMonth value has as it is remaining subjsons InvoiceMonth values needs to be 0.

Input:

[
  {
    "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": ""
  }
]

Output:

[
  {
    "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": ""
  }
]

答案1

得分: 1

以下是翻译好的部分:

[
  {
    "operation": "shift",
    "spec": {
      "*": {
        "credit": {
          "*": {
            "0": {
              "@3,InvoiceMonth": "[#2].InvoiceMonth",
              "@": "[#2].&3.&2",
              "@3,resource_name": "[#2].resource_name",
              "@3,resource_global_name": "[#2].resource_global_name",
              "@3,label": "[#2].label"
            },
            "*": {
              "#0": "[#2].InvoiceMonth",
              "@": "[#2].&3.&2",
              "@3,resource_name": "[#2].resource_name",
              "@3,resource_global_name": "[#2].resource_global_name",
              "@3,label": "[#2].label"
            }
          }
        }
      }
    }
  },
  {
    "operation": "cardinality",
    "spec": {
      "*": {
        "*": "ONE"
      }
    }
  }
]

希望这有帮助!如果你需要更多翻译,请随时告诉我。

英文:

You can use the following spec

[
  {
    "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"
      }
    }
  }
]

where the main idea is looping through the arrays under "credit" object, and then adding the other attributes.

huangapple
  • 本文由 发表于 2023年6月12日 21:19:50
  • 转载请务必保留本文链接:https://go.coder-hub.com/76457085.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定