嵌套对象的颠簸变换

时间:2020-02-07 17:05:39

标签: apache-nifi jolt

    [
  {
    "cluster_id": "0125-175512-node489",
    "custom_tag": {
      "app_name": "testing1",
      "total_time": 32
    }
  },
  {
    "cluster_id": "0125-175512-node489",
    "custom_tag": {
      "app_name": "testing2",
      "total_time": 34
    }
  }
]

我希望o / p像这样。

1)添加一个常量新字段“ new_addition” 2)将嵌套值“ custom_tag.app_name”复制到上一级。 3)将嵌套值“ custom_tag.total_time”复制到上一层,但属性名称不同“ time_to_finish”。

我无法为此找到震动规格。

    [
  {
    "cluster_id": "0125-175512-node489",
    "app_name": "testing1",
    "new_addition": "new const value",
    "time_to_finish": 32,
    "custom_tag": {
      "app_name": "testing1",
      "total_time": 32
    }
  },
  {
    "cluster_id": "0125-175512-node489",
    "app_name": "testing2",
    "new_addition": "new const value",
    "time_to_finish": 34,
    "custom_tag": {
      "app_name": "testing2",
      "total_time": 34
    }
  }
]

2 个答案:

答案 0 :(得分:0)

确保您具有对流文件有价值的 new_addition 属性。

Try with this Jolt spec:

[{
"operation": "shift",
"spec": {
    "*": "&", // get all elements
    "custom_tag": {
        "app_name": "app_name", //copy app_name to one level up
        "total_time": "time_to_finish", //copy total_time to one level up and change name.
        "@": "&" //get all the struct elements as is
    }
}
}, {
    "operation": "default",
    "spec": {
        "new_addition": "${new_addition}" //get the attribute value and add new_addition element to top level.
    }
}]

Output:

{
  "cluster_id" : "0125-175512-node489",
  "custom_tag" : {
    "app_name" : "testing2",
    "total_time" : 34
  },
  "app_name" : "testing2",
  "time_to_finish" : 34,
  "new_addition" : "new_addition"
}

Jolt transform Processor config:

enter image description here

Validation:

enter image description here

答案 1 :(得分:0)

以下工作

[{
  "operation": "shift",
  "spec": {
    "@": "&",
    "*": {
      "custom_tag": {
        "app_name": "&3[&2].app_name",
        "total_time": "&3[&2].time_to_finish"
      }
    }
  }
}, {
  "operation": "default",
  "spec": {
    "root[]": {
      "*": {
        "new_addition": "1"
      }
    }
  }
}]