记录快速接入 postgres cdc
工具选型
- Postgres 10+
- flink 1.13.3 , flink-connector-postgres-cdc 2.2.0
- Flink-CDC文档
flink-connector Github
Github官网的README记录是支持9.6的,不过使用Postgres 9.6的docker容器测试时部分SQL语法报错了,猜测是flink-connector-postgres-cdc依赖过高的问题,Postgres 9.6可以降低至2.0 or 2.1,此处使用Postgres 10+的容器进行测试。
过程
本地环境参考之前MySQL CDC
Postgres 归档日志配置,配置文件处于Postgres Data目录的postgresql.conf
更改 WRITE-AHEAD LOG 部分然后重启
wal_level = logical # minimal, replica, or logical
# (change requires restart)
flink 程序参考
Properties properties = new Properties();
properties.setProperty("snapshot.mode", "initial");
properties.setProperty("decimal.handling.mode", "double"); //debezium 小数转换处理策略
properties.setProperty("database.serverTimezone", "GMT+8"); //debezium 配置以database. 开头的属性将被传递给jdbc url
SourceFunction<String> sourceFunction = PostgreSQLSource.<String>builder()
.hostname("192.168.31.233")
.port(5432)
.database("postgres") // monitor postgres database
.schemaList("public") // monitor inventory schema
.tableList("public.t_table") // monitor products table
.username("root")
.password("123456")
.decodingPluginName("pgoutput") // pg解码插件
.slotName("t_table_slot") // 复制槽名称 不能重复
.deserializer(new JsonDebeziumDeserializationSchema()) // converts SourceRecord to JSON String
.debeziumProperties(properties)
.build();
StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
env
.addSource(sourceFunction)
.print().setParallelism(1); // use parallelism 1 for sink to keep message ordering
env.execute();
没啥可说的,source 出来操作完通过skin适配不同的目标库实现实时流。