【问题标题】:JoltTransformJson - Json TransformationJoltTransformJson - Json 转换
【发布时间】:2022-11-15 20:23:22
【问题描述】:

我有如下 JSON 值:

{
  "table": "table_name",
  "op_type": "U",
  "before": {
    "AAAA": "1-1111",
    "BBBB": "2022-08-31 03:57:01",
    "CCCC": "2023-08-31 23:59:59"
  },
  "after": {
    "AAAA": "1-1112",
    "BBBB": "2022-08-31 10:10:34"
  }
}

我想这样做我该怎么做?

{ 
  "AAAA": "1-1112",
  "BBBB": "2022-08-31 10:10:34",
  "CCCC": "2023-08-31 23:59:59" 
  "changed_columns": "AAAA, BBBB"
}

AAAA:“如果你有 after.AAAA,则采取 AAAA else before.AAAA”,BBBB:“如果你有 after.BBBB,则采取 BBBB else before.BBBB。

我想像这样添加 changed_columns 字段:

,"changed_columns": "AAAA, BBBB"

有没有办法做到这一点?

【问题讨论】:

  • 如果两个对象中都存在CCCC属性怎么办?
  • 如果两个对象都有 CCCC,那么应采用 after.CCCC 的 CCCC

标签: json transform apache-nifi jolt


【解决方案1】:

您可以使用shift 操作并首先获取after 值。然后你可以使用before 值。因此,如果键与一起匹配,则您将拥有一个包含两个元素的数组。

现在您可以使用modify-overwrite-beta 操作和=firstElement 函数获取第一个元素来获取after 值。

[
  {
    "operation": "shift",
    "spec": {
      "after": {
        "*": {
          "$": "changed_columns[]",
          "@(1,&)": "&1"
        }
      },
      "before": {
        "*": "&"
      }
    }
  },
  {
    "operation": "modify-overwrite-beta",
    "spec": {
      "*": "=firstElement(@(1,&))",
      "changed_columns": "=join(', ',@(1,changed_columns))"
    }
  }
]

【讨论】:

    【解决方案2】:

    您可以使用

    • 基数最近在使用 "after|before" 作为此顺序中的键以确定优先级之后进行规范
    • 连续交换键值对两次,判断是否 真的改变了组件以形成"changed_columns"

    [
      {
        // multiplex the attributes in order to generate three independent groups
        "operation": "shift",
        "spec": {
          "after|before": { // this order is important to determine the precedence in the upcoming cardinality spec
            "*": { 
              "@": "&",
              "@(0)": "l.&",
              "*": {
                "@1": "f.&2"
              }
            }
          }
        }
      },
      {
        // determine whether before vs. after values equal through this and next two specs
        "operation": "modify-overwrite-beta",
        "spec": {
          "l": {
            "*": "=lastElement(@(1,&))"
          },
          "f": {
            "*": "=firstElement(@(1,&))"
          }
        }
      },
      {
        "operation": "shift",
        "spec": {
          "*": "&",
          "l|f": {
            "*": {
              "$": "lf.@(0)"
            }
          }
        }
      },
      {
        "operation": "shift",
        "spec": {
          "*": "&",
          "lf": {
            "*": {
              "$": "&2.@(0)"
            }
          }
        }
      },
      {
        // construct an array from those newly formed keys
        "operation": "shift",
        "spec": {
          "*": "&",
          "lf": {
            "*": {
              "$": "changed_columns"
            }
          }
        }
      },
      {
        "operation": "modify-overwrite-beta",
        "spec": {
          "changed_columns": "=join(', ',@(1,&))"
        }
      },
      {
        "operation": "cardinality",
        "spec": {
          "*": "ONE"
        }
      },
      {
        "operation": "sort"
      }
    ]
    

    【讨论】:

      猜你喜欢
      • 2023-02-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-10-05
      • 2021-01-30
      • 2012-10-15
      • 2021-04-29
      相关资源
      最近更新 更多