当前位置: 首页 > backend >正文

如何解决Flink CDC同步时间类型字段8小时时间差的问题,以MySQL为例

在使用Flink CDC进行数据同步时,默认情况下经常会遇到时间类型的字段与实际值相差8个小时的问题。本文以MySQL为例提供解决方案,其他数据源也可以参考这类实现。

原文链接:
https://mp.weixin.qq.com/s/_f41ES8UquM-kj3Ie8JU_g

1. 设置server时区

比如MySQL服务的时区为UTC时间,可以参考以下code设置时区。

MySqlSource<String> mySqlSource = MySqlSource.<String>builder()
.hostname(${hostName})
.port(${port})
.databaseList(${dbList})
.tableList(${tableList})
.username(${userName})
.password(${dbPwd}) 
.scanNewlyAddedTableEnabled(${true})
.serverTimeZone("UTC")
.serverId(${serverID})
.startupOptions(${startupOptions})
.includeSchemaChanges(${includeSchemaChanges})
.debeziumProperties(${debeziumProperties})
.deserializer(new JsonDebeziumDeserializationSchema(false, decimalConfigs)).build();

2. 指定Debezium Time Converter

Flink CDC/Debezium官方有一个Time Converter自定义实现
MysqlDebeziumTimeConverter,可以在配置Debezium properties时进行指定来解决时间字段时间差的问题。
参考如下code进行配置:

String dateFormat = "yyyy-MM-dd";
String timeFormat = "HH:mm:ss";
String datetimeFormat = "yyyy-MM-dd HH:mm:ss";
String timestampFormat = "yyyy-MM-dd HH:mm:ss";
Properties debeziumProperties = new Properties();
debeziumProperties.setProperty(converters,"mysqlTimeConverters");
debeziumProperties.setProperty("mysqlTimeConverters.type","io.debezium.connector.mysql.converters.MysqlDebeziumTimeConverter");
debeziumProperties.setProperty("mysqlTimeConverters.format.timezone",timezone);
debeziumProperties.setProperty("mysqlTimeConverters.format.date",dateFormat);
debeziumProperties.setProperty("mysqlTimeConverters.format.time",timeFormat);
debeziumProperties.setProperty("mysqlTimeConverters.format.datetime",datetimeFormat);
debeziumProperties.setProperty("mysqlTimeConverters.format.timestamp",timestampFormat);

io.debezium.connector.mysql.converters.MysqlDebeziumTimeConverter源码实现片段。
在这里插入图片描述
在这里插入图片描述

如果我们需要自定义一个Converter,可以通过继承CustomConverter实现对应的方法即可,然后通过参考MysqlDebeziumTimeConverter的实现可以解决很多数据源比如SQL Server,PG等时间字段数据出现时间差的问题。

原文链接:
https://mp.weixin.qq.com/s/_f41ES8UquM-kj3Ie8JU_g

http://www.xdnf.cn/news/15753.html

相关文章:

  • Redis Sentinel哨兵集群
  • Spring之【AnnotatedBeanDefinitionReader】
  • 针对大规模语言模型的上下文工程技术调研与总结(翻译并摘要)
  • 【C++】入门阶段
  • 基于开放API接口采集的定制开发开源AI智能名片S2B2C商城小程序数据整合与增长策略研究
  • 本地部署开源的 AI 驱动的搜索引擎 Perplexica 并实现外部访问
  • Spring Bean 的作用域(Bean Scope)
  • SpringAI_Chat模型_DeepSeek模型--基础对话
  • 扭蛋机系统开发:打造多元化娱乐生态的新引擎
  • Libevent(3)之使用教程(2)创建事件
  • Spring MVC @RequestParam注解全解析
  • 【Linux】重生之从零开始学习运维之Nginx之server小实践
  • 最新版vscode 连接ubuntu 18.04 保姆级教程
  • 编程实现Word自动排版:从理论到实践的全面指南
  • SurfaceView、TextureView、SurfaceTexture 和 GLSurfaceView
  • 【Android】ListView与RecyclerView的基础使用
  • 【unity游戏开发入门到精通——3D篇】3D光源之——unity使用Lens Flare (SRP) 组件实现太阳耀斑镜头光晕效果
  • C++实现单层时间轮
  • 4644电源管理芯片在微波射频组件中的技术优势与国产化实践
  • Linux驱动学习day24(UART子系统)
  • Ubuntu系统下快速体验iperf3工具(网络性能测试)
  • 嵌入式Linux:什么是线程?
  • 【RK3576】【Android14】开发环境搭建
  • Thread,ThreadLocal,ThreadLocalMap 三者的关系, 以及在实际开发中的应用【AI记录用】
  • 荷兰KIPP ZONEN CMP4 太阳辐射传感器耐热仪器设计高温日射计一种辐射计
  • 熔断和降*的区别
  • synchronized锁升级过程【AI笔记,仅供自己参考】
  • NIO网络通信基础
  • P1205 [USACO1.2] 方块转换 Transformations
  • 如何检查GitHub上可能潜在的信息泄漏