Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MySQL 增量同步Datetime类型binlog接收到的时间 比实际UTC时间快8小时 #89

Closed
baisui1981 opened this issue Jan 14, 2022 · 1 comment
Labels
bug Something isn't working v3.4.0

Comments

@baisui1981
Copy link
Member

baisui1981 commented Jan 14, 2022

问题如题

flink-cdc中也有用户指出了类似的问题

  1. mysql 字段是datetime类型时,flink-cdc接收到数据会+8个小时 apache/flink-cdc#457
  2. MySQL timestamp type convert to user-define timezone apache/flink-cdc#28

问题溯源:

  1. TableSchemaBuilder创建Convertor

  2. 将字符串转成LocalDateTime

  3. 在JdbcValueConvert 中将日期转成UTC long值 JdbcValueConverters.java#L458,关键用了一个非常奇怪的方法转:

     public static long toEpochMillis(Object value, TemporalAdjuster adjuster) {
        if (value instanceof Long) {
            return (Long) value;
        }
        LocalDateTime dateTime = Conversions.toLocalDateTime(value);
        if (adjuster != null) {
            dateTime = dateTime.with(adjuster);
        }
        // 很想不通为啥这里写死用ZoneOffset.UTC ???硬是把时间拨快了8个小时
        return dateTime.toInstant(ZoneOffset.UTC).toEpochMilli();
    }

解决办法:

通过添加MySqlDateTimeConverter

并且在SourceChannel中添加如下配置,将以上类中的逻辑注入到Debezium处理binlog Datetime数据类型的逻辑中:
https://github.com/qlangtech/plugins/blob/df924d12549a6e11b614837af661ae2ee6965cbd/tis-incr/tis-realtime-flink/src/main/java/com/qlangtech/plugins/incr/flink/cdc/SourceChannel.java#L80

               debeziumProperties.put("converters", "datetime");
                debeziumProperties.put("datetime.type", "com.qlangtech.plugins.incr.flink.cdc.mysql.MySqlDateTimeConverter");
                debeziumProperties.put("datetime.format.date", "yyyy-MM-dd");
                debeziumProperties.put("datetime.format.time", "HH:mm:ss");
                debeziumProperties.put("datetime.format.datetime", "yyyy-MM-dd HH:mm:ss");
                debeziumProperties.put("datetime.format.timestamp", "yyyy-MM-dd HH:mm:ss");
                debeziumProperties.put("datetime.format.timestamp.zone", "UTC+8");
@baisui1981 baisui1981 added bug Something isn't working v3.4.0 labels Jan 14, 2022
@mrchenwx
Copy link

大佬 ,按照您的模式,LATEST 增量模式下是可以解决这个问题的,但是如果我设置为INITIAL, 全量+增量模式,这种情况下这么修改就不生效了,cdc由全量过渡到增量模式下时,增量阶段依然存在这个问题,大佬能指导下怎么解决下吗。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working v3.4.0
Projects
None yet
Development

No branches or pull requests

2 participants