基于 Flink CDC 构建 MySQL 和 Postgres 的 Streaming ETL
Flink CDC 配置过滤删除操作
在 Flink SQL CLI 中使用 Flink DDL 创建表时可以通过 debezium.skipped.operations 选项配置需要过滤的 oplog 操作。
除了使用 Flink DDL 创建表时需要额外配置
debezium.skipped.operations外,其他所有步骤保持不变
MySQL CDC 和 PostgreSQL CDC debezium.skipped.operations 选项略有不同
- MySQL CDC
Debezium’s MySQL Connector properties
| 参数 | 说明 | 是否必填 | 默认值 |
|---|---|---|---|
| debezium.skipped.operations | 需要过滤的 oplog 操作,以逗号分隔。操作包括 c 表示插入,u 表示更新,d 表示删除。默认情况下,不跳过任何操作。 | 否 | No default |
- PostgreSQL CDC
Debezium’s Postgres Connector properties
| 参数 | 说明 | 是否必填 | 默认值 |
|---|---|---|---|
| debezium.skipped.operations | 需要过滤的 oplog 操作,以逗号分隔。操作包括 c 表示插入,u 表示更新,d 表示删除,t 表示截断,none 表示不跳过任何操作。默认情况下,将跳过截断操作。 | 否 | t |
在 Flink SQL CLI 中使用 Flink DDL 创建表
使用 Flink SQL CLI 创建对应的表,用于同步这些底层数据库表的数据
-- Flink SQLFlink SQL> CREATE TABLE products (id INT,name STRING,description STRING,PRIMARY KEY (id) NOT ENFORCED) WITH ('connector' = 'mysql-cdc','hostname' = 'localhost','port' = '3306','username' = 'root','password' = '123456','database-name' = 'mydb','table-name' = 'products','debezium.skipped.operations' = 'd');Flink SQL> CREATE TABLE orders (order_id INT,order_date TIMESTAMP(0),customer_name STRING,price DECIMAL(10, 5),product_id INT,order_status BOOLEAN,PRIMARY KEY (order_id) NOT ENFORCED) WITH ('connector' = 'mysql-cdc','hostname' = 'localhost','port' = '3306','username' = 'root','password' = '123456','database-name' = 'mydb','table-name' = 'orders','debezium.skipped.operations' = 'd');Flink SQL> CREATE TABLE shipments (shipment_id INT,order_id INT,origin STRING,destination STRING,is_arrived BOOLEAN,PRIMARY KEY (shipment_id) NOT ENFORCED) WITH ('connector' = 'postgres-cdc','hostname' = 'localhost','port' = '5432','username' = 'postgres','password' = 'postgres','database-name' = 'postgres','schema-name' = 'public','table-name' = 'shipments','debezium.skipped.operations' = 'd');
在 Kibana 中查看包含商品和物流信息的订单数据
在 MySQL 的 orders 表中插入一条数据
--MySQLINSERT INTO ordersVALUES (default, '2020-07-30 15:22:00', 'Jark', 29.71, 104, false);
在 MYSQL 的 orders 表中删除那条数据
--MySQLDELETE FROM orders WHERE order_id = 10004;
可以看到 Kibana 中显示的订单数据并不会因为删除了 orders 中的一条记录而减少。
