使用Kafka Connect时如何转换所有时间戳字段?

A. *_*ers 5 apache-kafka apache-kafka-connect

我正在尝试将所有时间戳字段转换为格式为yyyy-MM-dd HH:mm:ss.

要转换多个字段,我必须分别为每个字段创建一个转换。

...
"transforms":"tsFormat1,tsFormat2,...,tsFormatN",
"transforms.tsFormat1.type": "org.apache.kafka.connect.transforms.TimestampConverter$Value",
"transforms.tsFormat1.target.type": "string",
"transforms.tsFormat1.field": "ts_col1",
"transforms.tsFormat1.format": "yyyy-MM-dd HH:mm:ss",
"transforms.tsFormat2.type": "org.apache.kafka.connect.transforms.TimestampConverter$Value",
"transforms.tsFormat2.target.type": "string",
"transforms.tsFormat2.field": "ts_col2",
"transforms.tsFormat2.format": "yyyy-MM-dd HH:mm:ss",
...
"transforms.tsFormatN.type": "org.apache.kafka.connect.transforms.TimestampConverter$Value",
"transforms.tsFormatN.target.type": "string",
"transforms.tsFormatN.field": "ts_colN",
"transforms.tsFormatN.format": "yyyy-MM-dd HH:mm:ss",
...
Run Code Online (Sandbox Code Playgroud)

 

有没有办法对所有时间戳列应用单个转换?

我试过了,

...
"transforms":"tsFormat",
"transforms.tsFormat.type": "org.apache.kafka.connect.transforms.TimestampConverter$Value",
"transforms.tsFormat.target.type": "string",
"transforms.tsFormat.field": "ts_col1, ts_col2,..., ts_colN",
"transforms.tsFormat.format": "yyyy-MM-dd HH:mm:ss",
...
Run Code Online (Sandbox Code Playgroud)

...
"transforms":"tsFormat",
"transforms.tsFormat.type": "org.apache.kafka.connect.transforms.TimestampConverter$Value",
"transforms.tsFormat.target.type": "string",
"transforms.tsFormat.field": "ts_col1",
"transforms.tsFormat.field": "ts_col2",
...
"transforms.tsFormat.field": "ts_colN",
"transforms.tsFormat.format": "yyyy-MM-dd HH:mm:ss",
...
Run Code Online (Sandbox Code Playgroud)

 


更好的是数字类型匹配之类的东西"numeric.mapping": "best_fit"。就像如何numeric.mapping应用于所有数字字段(无需手动指定字段名称)以尝试找到最佳数字类型一样,是否有类似的东西可以对所有时间戳字段应用转换或字符串格式?

小智 0

我在原来的基础上做了一些调整TimestampConverter,完美地达到你想要的效果: https: //github.com/howareyouo/kafka-connect-timestamp-converter