NiFi-CaptureChangeMySQL将json转换为[“ col_name”:“ col_value”]格式

时间:2019-02-15 10:10:37

标签: apache-nifi

序言

MySQL表名称:ar_tmp有两列id intname int

要做

我执行sql

insert into ar_tmp (id, name) values (1, 4);

CaptureChangeMySQL捕获了这样的CDC和流内容

{
    "type":"insert",
    "timestamp":1550221517000,
    "binlog_filename":"mysql-bin.013920",
    "binlog_position":241518646,
    "database":"platform_data",
    "table_name":"ar_tmp",
    "table_id":2899035,
    "columns":[
        {
            "id":1,
            "name":"id",
            "column_type":4,
            "value":1
        },
        {
            "id":2,
            "name":"name",
            "column_type":4,
            "value":4
        },
        {
            "id":3,
            "value":4
        }
    ]
}

但是我想要这种格式的结果

{
    "type":"insert",
    "timestamp":1550221517000,
    "binlog_filename":"mysql-bin.013920",
    "binlog_position":241518646,
    "database":"platform_data",
    "table_name":"ar_tmp",
    "table_id":2899035,
    "columns":[
        {
            "id":1,
            "name":4
        }
    ]
}

{
    "id":1,
    "name":4
}

解决方案

可以使用jsonPath函数通过硬编码来完成 enter image description here

但是可能很难做到这一点,因为每一列都使用相同的代码来使处理器冗余(例如50列)。更糟糕的是,更改列名会很危险。

有什么想法吗?

1 个答案:

答案 0 :(得分:0)

JoltTransformJSON可以在这里为您提供帮助。

尝试here

演示输入json的Jolt Spec:

[
  {
    "operation": "shift",
    "spec": {
      "columns": {
        "*": {
          "value": "columns.@(1,name)"
        }
      },
      "*": "&"
    }
  }
]

结果是:

{
  "type" : "insert",
  "timestamp" : 1550221517000,
  "binlog_filename" : "mysql-bin.013920",
  "binlog_position" : 241518646,
  "database" : "platform_data",
  "table_name" : "ar_tmp",
  "table_id" : 2899035,
  "columns" : {
    "id" : 1,
    "name" : 4
  }
}