Prakash Tiwari
03/28/2024, 7:50 PMflink-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:
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):
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:
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!Gunnar Morling
04/02/2024, 9:44 AMPrakash Tiwari
04/02/2024, 6:14 PMGunnar Morling
04/04/2024, 1:33 PM