jolt transformation – 在 Apache NiFi 中的数组数组

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

jolt transformation - array array inside apache nifi

问题

Unsuccessful attempt:

[
  {
    "operation": "shift",
    "spec": {
      "results": {
        "*": {
          "group.queueId": "[&].queueid",
          "group.mediaType": "[&].mediatype",
          "data": {
            "*": {
              "metric": "[&3].metric1",
              "stats.count": "[&3].metric1_count"
            }
          }
        }
      }
    }
  }
]

Unsuccessful Output:

[
  {
    "queueid": "fila1",
    "mediatype": "message1",
    "metric1": [
      "oInteracting1",
      "oWaiting1"
    ],
    "metric1_count": [
      1,
      0
    ]
  },
  {
    "queueid": "fila2",
    "mediatype": "message2",
    "metric1": [
      "oInteracting2",
      "oWaiting2"
    ],
    "metric1_count": [
      8,
      0
    ]
  },
  {
    "queueid": "fila3",
    "mediatype": "message3",
    "metric1": [
      "oInteracting3",
      "oWaiting3"
    ],
    "metric1_count": [
      1,
      0
    ]
  }
]

Expected output:

[
  {
    "queueid": "fila1",
    "mediatype": "message1",
    "oInteracting1": 1,
    "oWaiting1": 0
  },
  {
    "queueid": "fila2",
    "mediatype": "message2",
    "oInteracting2": 8,
    "oWaiting2": 0
  },
  {
    "queueid": "fila3",
    "mediatype": "message3",
    "oInteracting3": 1,
    "oWaiting3": 0
  }
]

You can use the corrected spec to achieve the expected output. The issue with your previous attempt was in the spec, and the corrected spec should give you the desired result.

英文:

my attempts

Input :

{
  "results": [
    {
      "group": {
        "queueId": "fila1",
        "mediaType": "message1"
      },
      "data": [
        {
          "metric": "oInteracting1",
          "stats": {
            "count": 1
          },
          "truncated": false
        },
        {
          "metric": "oWaiting1",
          "stats": {
            "count": 0
          }
        }
      ]
    },
    {
      "group": {
        "queueId": "fila2",
        "mediaType": "message2"
      },
      "data": [
        {
          "metric": "oInteracting2",
          "stats": {
            "count": 8
          },
          "truncated": false
        },
        {
          "metric": "oWaiting2",
          "stats": {
            "count": 0
          }
        }
      ]
    },
    {
      "group": {
        "queueId": "fila3",
        "mediaType": "message3"
      },
      "data": [
        {
          "metric": "oInteracting3",
          "stats": {
            "count": 1
          },
          "truncated": false
        },
        {
          "metric": "oWaiting3",
          "stats": {
            "count": 0
          }
        }
      ]
    }
  ]
}

Unsuccessful attempt :

[
  {
    "operation": "shift",
    "spec": {
      "results": {
        "*": {
          "@group.queueId": "[&].queueid",
          "@group.mediaType": "[&].mediatype",
          "data": {
            "*": {
              "@metric": "[&3].metric1",
              "@stats.count": "[&3].metric1_count"
            }
          }
        }
      }
    }
  }
]

Unsuccessful Output :

[
  {
    "queueid": "fila1",
    "mediatype": "message1",
    "metric1": [
      "oInteracting1",
      "oWaiting1"
    ],
    "metric1_count": [
      1,
      0
    ]
  },
  {
    "queueid": "fila2",
    "mediatype": "message2",
    "metric1": [
      "oInteracting2",
      "oWaiting2"
    ],
    "metric1_count": [
      8,
      0
    ]
  },
  {
    "queueid": "fila3",
    "mediatype": "message3",
    "metric1": [
      "oInteracting3",
      "oWaiting3"
    ],
    "metric1_count": [
      1,
      0
    ]
  }
]

Expected output :

[
  {
    "queueid": "fila1",
    "mediatype": "message1",
    "oInteracting1": 1,
    "oWaiting1": 0
  },
  {
    "queueid": "fila2",
    "mediatype": "message2",
    "oInteracting1": 8,
    "oWaiting1": 0
  },
  {
    "queueid": "fila3",
    "mediatype": "message3",
    "oInteracting3": 1,
    "oWaiting3": 0
  }
]

Also trying to add the condition

"data": {"\*": { "stats.count": "metric"}} 

but it didn't work

Thanks

答案1

得分: 1

递归匹配**stats**对象内部,例如

[
  {
    "operation": "shift",
    "spec": {
      "results": {
        "*": {
          "@group.queueId": "[&].queueid",
          "@group.mediaType": "[&].mediatype",
          "data": {
            "*": {
              "stats": {
                "@count": "[&4].@2,metric" // 遍历: 和 {(2级)以达到其在JSON中的原始级别
              }
            }
          }
        }
      }
    }
  }
]

该网站上的演示位于 http://jolt-demo.appspot.com/

jolt transformation – 在 Apache NiFi 中的数组数组

英文:

Just recursively match within the stats object such as

[
  {
    "operation": "shift",
    "spec": {
      "results": {
        "*": {
          "@group.queueId": "[&].queueid",
          "@group.mediaType": "[&].mediatype",
          "data": {
            "*": {
              "stats": {
                "@count": "[&4].@2,metric" // traverses : and { ( 2 levels ) to reach upto its original level within the JSON
              }
            }
          }
        }
      }
    }
  }
]

the demo on the site http://jolt-demo.appspot.com/ is

jolt transformation – 在 Apache NiFi 中的数组数组

huangapple
  • 本文由 发表于 2023年3月3日 17:49:06
  • 转载请务必保留本文链接:https://go.coder-hub.com/75625504.html
匿名

发表评论

匿名网友

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

确定