flink 实现 postgre-CDC
一、前置工作
1. 修改 postgresql 配置文件
/data/pgsql/13/data/postgresql.conf
相关配置:
# 更改wal日志方式为logical-- logical会增加支持逻辑解码所需的信息。
wal_level = logical # minimal, replica, or logical
# 更改solts最大数量(默认值为10),flink-cdc默认一张表占用一个slots
max_replication_slots = 20 # max number of replication slots
# 更改wal发送最大进程数(默认值为10),这个值和上面的solts设置一样
max_wal_senders = 20 # max number of walsender processes
# 中断那些停止活动超过指定毫秒数的复制连接,可以适当设置大一点(默认60s)
wal_sender_timeout = 180s # in milliseconds; 0 disable
#Archiving 归档设置
archive_mode = on
archive_command = 'test ! -f /data/pg_wals/%f && cp %p /data/pg_wals/%f' #shell命令
wal_level是必须更改的,其它参数选着性更改,如果同步表数量超过10张建议修改为合适的值
更改配置文件postgresql.conf完成,需要重启pg服务生效,所以一般是在业务低峰期更改
systemctl restart postgresql-13
2. 修改 pg_hba.conf
# IPv4 local connections: host all all 0.0.0.0/0 md5 host replication all 0.0.0.0/0 md5
该文件用于控制访问安全性,管理客户端对于PostgreSQL服务器的访问权限,内容包括:允许哪些用户连接到哪个数据库,允许哪些IP或者哪个网段的IP连接到本服务器,以及指定连接时使用的身份验证模式。
systemctl restart postgresql-13
3. 创建用户,数据库授权
-- pg新建用户 CREATE USER finkcdc WITH PASSWORD 'finkcdc'; -- 给用户复制流权限 ALTER ROLE finkcdc replication; -- 给用户数据库权限 create database health; grant CONNECT ON DATABASE health to finkcdc;
--create database testdb owner finkcdc; --grant all privileges on database testdb to finkcdc; -- 把当前库所有表查询权限赋给用户 GRANT SELECT ON ALL TABLES IN SCHEMA public TO finkcdc;
4. 发布表
--切换到要发布的数据库
\c health
-- 设置发布为true update pg_publication set puballtables=true where pubname is not null; -- 把所有表进行发布----注意要切换到相关的数据库下面执行,不然会失败 CREATE PUBLICATION dbz_publication FOR ALL TABLES; -- 查询哪些表已经发布 select * from pg_publication_tables; --新增发布等相关操作 --ALTER PUBLICATION name OWNER TO { new_owner | CURRENT_USER | SESSION_USER } --ALTER PUBLICATION name ADD TABLE table_name [, ...] --ALTER PUBLICATION name SET TABLE table_name [, ...] --ALTER PUBLICATION name DROP TABLE table_name [, ...]
--ALTER PUBLICATION dbz_publication ADD TABLE health
二、flinkCDC代码
StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment(); Properties properties = new Properties(); properties.setProperty("snapshot.mode", "never"); properties.setProperty("debezium.slot.name", "pg_cdc"); properties.setProperty("debezium.slot.drop.on.stop", "true"); properties.setProperty("include.schema.changes", "true"); //使用连接器配置属性启用定期心跳记录生成 properties.setProperty("heartbeat.interval.ms", String.valueOf(DEFAULT_HEARTBEAT_MS)); SourceFunction<String> sourceFunction = PostgreSQLSource.<String>builder() .hostname("192.168.40.131") .port(5432) .database("health") // monitor postgres database .schemaList("public") // monitor inventory schema .tableList("public.personinfo") // monitor products table .username("postgres") .password("Univalsoft_2022") .decodingPluginName("pgoutput") //pgoutput是 PostgreSQL 10+ 中的标准逻辑解码输出插件。需要设置一下。添加如下配置 .deserializer(new StringDebeziumDeserializationSchema()) // converts SourceRecord to JSON String //配置 .debeziumProperties(properties) .build(); DataStreamSource<String> pgsqlDS = env.addSource(sourceFunction); pgsqlDS.print(); //env.addSource(sourceFunction) // .print(); // use parallelism 1 for sink to keep message ordering env.execute();
三、问题:
1. 更新是before没有值: 更新主键时会伴随删除操作和插入操作,其他按照主键来
参考官方文档
https://debezium.io/documentation/reference/1.9/connectors/postgresql.html
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 分享4款.NET开源、免费、实用的商城系统
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
· 上周热点回顾(2.24-3.2)