Flink CDC 採集MySQL 初始化或者指定時間戳時,沒有采集到資料

元亨利貞發表於2023-09-23

介紹

在使用 Flink 1.13.x 整合 CDC 2.3.0 時,如果遇到時間戳作為起點開始採集但沒有采集到資料的問題,可能有以下幾個原因:


解決方案

時區問題:Flink 執行機器時區和 MySQL Server 時區不匹配,database.serverTimezone 配置會受到影響。解決辦法是手動指定 Flink 執行的時區,和連線的資料庫時區資訊保持一致1。例如,可以在資料庫屬性中新增 dbProps.put("database.serverTimezone", "UTC");。

包衝突:Flink 為瞭解決包衝突,對一些通用的工具包做了 shaded1。Flink CDC Connectors 2.3.0 版本引用了 Flink 1.16.0,這個版本的 Flink 使用了 flink-shaded-guava:30.1.1-jre-15.0 版本。而 Flink 1.13.0 使用的是 flink-shaded-guava:18.0-13.0 版本,兩個版本的 shaded package 不一樣引起的。解決方法是在 CDC 中再次 shaded 一下,讓 CDC 裡面引用到的 guava30 變為 guava18。


程式碼示例

import org.apache.flink.api.common.eventtime.WatermarkStrategy;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import com.ververica.cdc.debezium.JsonDebeziumDeserializationSchema;
import com.ververica.cdc.connectors.mysql.source.MySqlSource;
public class MySqlSourceExample {
  public static void main(String[] args) throws Exception {
        // 建立 Properties 物件
        Properties properties = new Properties();
        // 設定時區為 "Asia/Shanghai"
        properties.setProperty("database.serverTimezone", "Asia/Shanghai");
    MySqlSource<String> mySqlSource = MySqlSource.<String>builder()
        .hostname("yourHostname")
        .port(yourPort)
        .databaseList("yourDatabaseName") // set captured database, If you need to synchronize the whole database, Please set tableList to ".*".
        .tableList("yourDatabaseName.yourTableName") // set captured table
        .username("yourUsername")
        .password("yourPassword")
        .debeziumProperties(properties)
        .deserializer(new JsonDebeziumDeserializationSchema()) // converts SourceRecord to JSON String
        .build();
    StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
    // enable checkpoint
    env.enableCheckpointing(3000);
    env
      .fromSource(mySqlSource, WatermarkStrategy.noWatermarks(), "MySQL Source")
      // set 4 parallel source tasks
      .setParallelism(4)
      .print().setParallelism(1); // use parallelism 1 for sink to keep message ordering
    env.execute("Print MySQL Snapshot + Binlog");
  }
}


來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/70024433/viewspace-2985515/,如需轉載,請註明出處,否則將追究法律責任。

相關文章