Kafka JDBC Sink使用TimestampConverter时出现'T'非法模式字符错误
问题根因
报错由三个配置错误共同导致:
- 转换器字段参数名写错:TimestampConverter指定处理字段的参数格式为
transforms.<转换器别名>.field,当前配置写的transforms.timestamp.field既没带定义的转换器别名TimestampConverter,参数名也不符合规范,导致转换器初始化参数读取异常。 - Shell传参转义错误:curl命令用单引号包裹整个JSON请求体,JSON内部format字段值里的单引号会提前闭合Shell的单引号引用,实际传入Kafka Connect的格式字符串丢失了包裹T的单引号(从错误日志打印的实际加载格式
yyyy-MM-ddTHH:mm:ssZ可验证),T被直接识别为日期模式字符,触发非法字符报错。 - 时间格式匹配错误:时间字符串结尾的
Z是UTC时区固定字面量,不是SimpleDateFormat中Z模式符匹配的RFC 822时区偏移(如+0800格式),直接写Z会导致解析匹配失败。
解决步骤
- 修正时间匹配格式
针对2022-06-29T04:00:00Z格式,正确的SimpleDateFormat模式为yyyy-MM-dd'T'HH:mm:ss'Z',固定字符T、Z都用单引号包裹。 - 修正转换器字段参数
删除错误的transforms.timestamp.field配置,替换为transforms.TimestampConverter.field = "timestamp"。 - 补充时区配置(必选,避免时间偏移)
增加配置transforms.TimestampConverter.timezone = "UTC",因为源时间是UTC时区,而PostgreSQL目标列是timestamp without time zone类型,不会自动做时区转换,不指定时区会用Connect工作节点的默认时区解析,导致时间值偏差。 - 规避Shell转义问题
最稳妥的方式是把连接器JSON配置存为独立文件(比如命名为sink_config.json),curl提交时用-d @sink_config.json的方式读取文件传参,完全避免Shell引号截断问题。如果要直接在curl命令里写JSON,需要把format值里的每个单引号替换为'\''做Shell转义。
修正后可用的配置参考
{ "name":"test_sink_timescale", "config":{ "connector.class":"io.confluent.connect.jdbc.JdbcSinkConnector", "connection.url":"jdbc:postgresql://<url>:5432/postgres", "connection.user":"<user>", "connection.password":"<password>", "topics":"test", "auto.create":"false", "insert.mode": "insert", "table.name.format": "public.test", "transforms": "TimestampConverter", "transforms.TimestampConverter.type": "org.apache.kafka.connect.transforms.TimestampConverter$Value", "transforms.TimestampConverter.field": "timestamp", "transforms.TimestampConverter.format": "yyyy-MM-dd'T'HH:mm:ss'Z'", "transforms.TimestampConverter.target.type": "Timestamp", "transforms.TimestampConverter.timezone": "UTC" } }
对应的文件提交curl命令为:
curl -X POST -H "Content-Type: application/json" -i http://127.0.0.1:8083/connectors -d @sink_config.json
验证标准
配置提交后任务可正常启动,无非法模式字符报错,时间字段能正确写入PostgreSQL的timestamp列,时间值和源数据一致无偏移。
内容的提问来源于stack exchange,提问作者HelloMoto
相关产品推荐
相关产品推荐

