Subject: Seeking Efficient CDC Ingestion from MySQ...
# troubleshooting
p
Subject: Seeking Efficient CDC Ingestion from MySQL to Iceberg (Hundreds of Tables!) TLDR: Need help setting up efficient CDC ingestion for hundreds of MySQL tables into Iceberg using Flink. Tried both Table API and DataStream API, but the resulting solutions look like too much effort to manage. Looking for a simpler solution, and interested to know what others are using. Hi everyone, I'm seeking advice on establishing an efficient CDC (Change Data Capture) pipeline to ingest data from hundreds of MySQL tables (spread across 10-20 database servers) into Apache Iceberg. We're currently using Flink with the
flink-mysql-cdc-connector
. I want to get some feedback whether we did it correctly or we missed anything. Setup: We've pre-created the Iceberg tables in the Glue Data Catalog for our ingestion job. Here's an example of creating an Iceberg table for the
Book
table in MySQL:
Copy code
CREATE CATALOG LibraryCatalog WITH (
 'type'='iceberg',
 'warehouse'='s3://path_here/icebergcatalog',
 'catalog-impl'='org.apache.iceberg.aws.glue.GlueCatalog',
 'io-impl'='org.apache.iceberg.aws.s3.S3FileIO'
);


CREATE TABLE LibraryCatalog.library_database.book_sink (
 `BookKey` VARCHAR(255) NOT NULL,
 Title VARCHAR(255),
 AuthorName VARCHAR(255),
 PRIMARY KEY (`BookKey`) NOT ENFORCED
) WITH (
 'catalog-name'='iceberg_catalog',
 'catalog-type'='hadoop', 
 'warehouse'='warehouse_path_here',
 'format-version'='2'
);
Current Approaches: 1. Using Flink SQL (Table API): We've implemented a solution using Flink SQL (Table API). This approach involves writing individual SQL scripts (like the provided example):
Copy code
CREATE TABLE Library_Book_Source (
  `BookKey` VARCHAR(255) NOT NULL,
  Title VARCHAR(255),
  AuthorName VARCHAR(255),
  PRIMARY KEY (`BookKey`) NOT ENFORCED
) WITH (
  'connector' = 'mysql-cdc',
  'hostname' = 'DB_IP_ADDRESS',
  'port' = '3306',
  'username' = 'username',
  'password' = 'password',
  'database-name' = 'LibraryDB',
  'table-name' = 'Book'
);

CREATE CATALOG LibraryCatalog WITH (
  'type'='iceberg',
  'warehouse'='s3://path_here/icebergcatalog',
  'catalog-impl'='org.apache.iceberg.aws.glue.GlueCatalog',
  'io-impl'='org.apache.iceberg.aws.s3.S3FileIO'
);

INSERT INTO LibraryCatalog.library_database.book_sink SELECT * FROM Library_Book_Source;
While functional, this approach requires writing queries for each table, making it difficult to manage for hundreds of tables. 2. Using Flink Datastream API: We've also explored a solution using Flink's DataStream API. This approach offers more scalability but involves custom code for schema handling. Here's a portion of the code demonstrating CDC data reading from MySQL:
Copy code
final StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
env.enableCheckpointing(3000);

// Configure the MySQL source to capture data from a specific database and table
MySqlSourceString mySqlSource = MySqlSource.Stringbuilder()
       .hostname("DB_IP_ADDRESS")
       .port(3306)
       .databaseList("LibraryDB") // set captured database
       .tableList("LibraryDB.Book") // set captured table
       .username("username")
       .password("password")
       .serverTimeZone("UTC")
       .deserializer(new JsonDebeziumDeserializationSchema()) // converts SourceRecord to JSON String
       .build();

// Create a Flink DataStream from the source
DataStreamSourceString rawData = env.fromSource(mySqlSource, WatermarkStrategy.noWatermarks(), "MySQL Source")
                                     .setParallelism(1); // Adjust parallelism as needed

// Build a DataStreamRowData from the DataStreamSourceString
DataStreamGenericRowData inputData = rawData.map(new MapFunctionString, GenericRowData() {
       @Override
       public GenericRowData map(String rawInfo) throws Exception {

           // The rawInfo string is in Debezium format, and I extract the data in "after" key in this private method
           MapString, Object hashMap = extractRowData(rawInfo);

           // I did another POC to dynamically fetch the table schema info using Glue SDK, but I omitted that part for simplicity
           GenericRowData genericRowData = new GenericRowData(3);
           genericRowData.setField(0, StringData.fromString((String) rowDataMap.get("BookKey")));
           genericRowData.setField(1, StringData.fromString((String) rowDataMap.get("Title")));
           genericRowData.setField(2, StringData.fromString((String) rowDataMap.get("AuthorName")));
           return genericRowData;
       }
});

// Sink the table into iceberg
FlinkSink.forRowData(inputData).tableLoader(tableLoader).append(); // Some in-between code omitted for simplicity
While I think this approach is easier to manage that the Table API one, I'm not confident in some code I put together just to make things work; and I'm sure there'll be more corner cases. I feel that there has to be some simpler solution for this use-case, so any suggestions are welcome. Thank you!
🤔 5
g
Hey Prakash, a few options exist: • Using upstream Debezium for streaming CDC events from many tables into corresponding Kafka topics • Using the data stream API as you show above (it's also the foundation of how we're solving this problem in Decodable's MySQL CDC source) • Using Flink CDC 3.0 which supposedly lets you ingest changes from multiple tables
p
Thanks Gunnar! More than Ingestion, I'm worried about having to convert Debezium records to Iceberg records, and that part is the most error prone IMO. Flink CDC 3.0 looks very promising, but I don't think it has support for Apache Iceberg yet. I'm interested to know how other people are setting up CDC for MySQL tables into data lake (Iceberg or similar table formats).
g
There's an Iceberg connector in Debezium Server which takes care of that: https://debezium.io/blog/2021/10/20/using-debezium-create-data-lake-with-apache-iceberg/. We do the similar thing with our managed Iceberg sink connector in Decodable.