debezium简单测试

现有需求,需要使用pg的数据实时统计一些指标,经过调研,决定使用kafkaCat或debezium将pg的操作日志同步到kafka中,现将简单测试debezium的过程总结一下。

debezium简介

1.debezium默认行为是连接器将所有生成的事件流式传输到该表的单独kafka主题。
2.PG需要安装一个逻辑解码输出插件,插件可以是decoderbufs、wal2json、pgoutput,我使用的是wal2json
3.连接器为捕获的每个行级插入、更新和删除操作生成一个更改事件,并未单独的kafka主题中的每个表发送更改事件记录
4.逻辑解码不支持DDL更改,连接器无法将DDL更改事件报告回消费者
5.只有primary的表支持逻辑解码复制槽
6.目前仅支持使用UTF-8字符编码的数据库。使用单字节字符编码,无法正确处理包含扩展ASCII代码字符的字符串

安装wal2json插件

1.配置环境变量

#不是必须是~/.bash_profile ,也可以是与其他组件相同的profile文件
vim ~/.bash_profile
export PATH=/Library/PostgreSQL/10/bin:$PATH
export PGDATA=//Library/PostgreSQL/10/data
export PATH=/Library/PostgreSQL/10/bin/pg_config:$PATH
#退出
source ~/.bash_profile

2.clone wal2json项目并编译安装

git clone https://github.com/streamsets/wal2json.git 
export PGDATA=//Library/PostgreSQL/10/data
make
make install

3.修改postgresql.conf参数

因为postgresql.conf在data目录下,正常是没有访问权限的,需要先使用管理员权限修改权限

sudo chmod 755 data

#postgresql.conf
wal_level = logical
#
# these parameters only need to set in versions 9.4, 9.5 and 9.6
# default values are ok in version 10 or later
#
max_replication_slots = 10
max_wal_senders = 10
wal_sender_timeout = 2000

如果不修改wal_level参数,运行kafka-connect后会报错

Task threw an uncaught and unrecoverable exception. Task is being killed and will not recover until manually restarted (org.apache.kafka.connect.runtime.WorkerTask:193)
org.apache.kafka.connect.errors.ConnectException: org.postgresql.util.PSQLException: ERROR: logical decoding requires wal_level >= logical
    at io.debezium.connector.postgresql.PostgresConnectorTask.start(PostgresConnectorTask.java:114)
    at io.debezium.connector.common.BaseSourceTask.start(BaseSourceTask.java:77)
    at org.apache.kafka.connect.runtime.WorkerSourceTask.execute(WorkerSourceTask.java:232)
    at org.apache.kafka.connect.runtime.WorkerTask.doRun(WorkerTask.java:186)
    at org.apache.kafka.connect.runtime.WorkerTask.run(WorkerTask.java:241)
    at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)
    at java.util.concurrent.FutureTask.run(FutureTask.java:266)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
    at java.lang.Thread.run(Thread.java:748)

4.将wal2json.so拷贝到/Library/PostgreSQL/10/lib下

5.重启pg

./pg_ctl -l /Library/PostgreSQL/10/logs/pg_server.log -D /Library/PostgreSQL/10/data/ restart

在重启前需要将data目录权限修改为700,否则会报错

安装debezium

1.下载安装包

$ wget https://repo1.maven.org/maven2/io/debezium/debezium-connector-postgres/1.7.1.Final/debezium-connector-postgres-1.7.1.Final-plugin.tar.gz
$ tar -zxvf ./debezium-connector-postgres-1.7.1.Final-plugin.tar.gz
$ cd debezium-connector-postgres/
$ cp ./*.jar /Users/yanhaowen/bigdata_tools/kafka/libs
$ cp -R ../debezium-connector-postgres /Users/yanhaowen/bigdata_tools/kafka/plugins

2.修改connect standalone

配置postgres.properties

#postgres.properties
name=dbz-pg-connector-source
connector.class=io.debezium.connector.postgresql.PostgresConnector
plugin.name=pgoutput
database.hostname=localhost
database.port=5432
database.user=postgres
database.password=123456
database.dbname=test
database.history.kafka.bootstrap.servers=localhost:9092
database.server.name=test
database.whitelist=public.test_table #白名单,指同步哪张表

3.修改connect-standalone.properties/connect-distributed.properties

bootstrap.servers=localhost:9092
key.converter=org.apache.kafka.connect.json.JsonConverter
value.converter=org.apache.kafka.connect.json.JsonConverter
key.converter.schemas.enable=true
value.converter.schemas.enable=true
offset.storage.file.filename=/tmp/connect.offsets
offset.flush.interval.ms=10000
plugin.path=/Users/yanhaowen/bigdata_tools/kafka/plugins

测试

1.启动kafka-connect

./connect-standalone.sh ../config/connect-standalone.properties ../config/postgres.properties

2.查看kafka的topic

./kafka-topics.sh --list --bootstrap-server localhost:9092

3.启动消费者,查看topic是否有数据

./kafka-console-consumer.sh --bootstrap-server localhost:9092 --topic test.public.test_table

4.在pg的test库下面对test_table进行操作

4.1 test_table表结构(必须要设置主键)

create table test_table( id varchar, node varchar, primary key (id) );

4.1 Insert操作

insert into test_table(id,code) values(3,'c');

消费者消费到信息:

{"schema":{"type":"struct","fields":[{"type":"struct","fields":[{"type":"string","optional":false,"field":"id"},{"type":"string","optional":true,"field":"code"}],"optional":true,"name":"test.public.test_table.Value","field":"before"},{"type":"struct","fields":[{"type":"string","optional":false,"field":"id"},{"type":"string","optional":true,"field":"code"}],"optional":true,"name":"test.public.test_table.Value","field":"after"},{"type":"struct","fields":[{"type":"string","optional":false,"field":"version"},{"type":"string","optional":false,"field":"connector"},{"type":"string","optional":false,"field":"name"},{"type":"int64","optional":false,"field":"ts_ms"},{"type":"string","optional":true,"name":"io.debezium.data.Enum","version":1,"parameters":{"allowed":"true,last,false"},"default":"false","field":"snapshot"},{"type":"string","optional":false,"field":"db"},{"type":"string","optional":false,"field":"schema"},{"type":"string","optional":false,"field":"table"},{"type":"int64","optional":true,"field":"txId"},{"type":"int64","optional":true,"field":"lsn"},{"type":"int64","optional":true,"field":"xmin"}],"optional":false,"name":"io.debezium.connector.postgresql.Source","field":"source"},{"type":"string","optional":false,"field":"op"},{"type":"int64","optional":true,"field":"ts_ms"}],"optional":false,"name":"test.public.test_table.Envelope"},"payload":{"before":null,"after":{"id":"3 ","code":"c "},"source":{"version":"1.0.3.Final","connector":"postgresql","name":"test","ts_ms":1641449386360,"snapshot":"false","db":"test","schema":"public","table":"test_table","txId":574,"lsn":25157688,"xmin":null},"op":"c","ts_ms":1641449386362}}

4.2 Update操作

update test_table set code='aaa' where id='3';

消费者消费到信息:

{"schema":{"type":"struct","fields":[{"type":"struct","fields":[{"type":"string","optional":false,"field":"id"},{"type":"string","optional":true,"field":"code"}],"optional":true,"name":"test.public.test_table.Value","field":"before"},{"type":"struct","fields":[{"type":"string","optional":false,"field":"id"},{"type":"string","optional":true,"field":"code"}],"optional":true,"name":"test.public.test_table.Value","field":"after"},{"type":"struct","fields":[{"type":"string","optional":false,"field":"version"},{"type":"string","optional":false,"field":"connector"},{"type":"string","optional":false,"field":"name"},{"type":"int64","optional":false,"field":"ts_ms"},{"type":"string","optional":true,"name":"io.debezium.data.Enum","version":1,"parameters":{"allowed":"true,last,false"},"default":"false","field":"snapshot"},{"type":"string","optional":false,"field":"db"},{"type":"string","optional":false,"field":"schema"},{"type":"string","optional":false,"field":"table"},{"type":"int64","optional":true,"field":"txId"},{"type":"int64","optional":true,"field":"lsn"},{"type":"int64","optional":true,"field":"xmin"}],"optional":false,"name":"io.debezium.connector.postgresql.Source","field":"source"},{"type":"string","optional":false,"field":"op"},{"type":"int64","optional":true,"field":"ts_ms"}],"optional":false,"name":"test.public.test_table.Envelope"},"payload":{"before":null,"after":{"id":"3 ","code":"aa "},"source":{"version":"1.0.3.Final","connector":"postgresql","name":"test","ts_ms":1641450009438,"snapshot":"false","db":"test","schema":"public","table":"test_table","txId":575,"lsn":25158168,"xmin":null},"op":"u","ts_ms":1641450009444}}

4.3 Delete操作

delete from test_table where id='3';

消费者消费到信息:如果是delete操作,则会多输出一行null

{"schema":{"type":"struct","fields":[{"type":"struct","fields":[{"type":"string","optional":false,"field":"id"},{"type":"string","optional":true,"field":"code"}],"optional":true,"name":"test.public.test_table.Value","field":"before"},{"type":"struct","fields":[{"type":"string","optional":false,"field":"id"},{"type":"string","optional":true,"field":"code"}],"optional":true,"name":"test.public.test_table.Value","field":"after"},{"type":"struct","fields":[{"type":"string","optional":false,"field":"version"},{"type":"string","optional":false,"field":"connector"},{"type":"string","optional":false,"field":"name"},{"type":"int64","optional":false,"field":"ts_ms"},{"type":"string","optional":true,"name":"io.debezium.data.Enum","version":1,"parameters":{"allowed":"true,last,false"},"default":"false","field":"snapshot"},{"type":"string","optional":false,"field":"db"},{"type":"string","optional":false,"field":"schema"},{"type":"string","optional":false,"field":"table"},{"type":"int64","optional":true,"field":"txId"},{"type":"int64","optional":true,"field":"lsn"},{"type":"int64","optional":true,"field":"xmin"}],"optional":false,"name":"io.debezium.connector.postgresql.Source","field":"source"},{"type":"string","optional":false,"field":"op"},{"type":"int64","optional":true,"field":"ts_ms"}],"optional":false,"name":"test.public.test_table.Envelope"},"payload":{"before":{"id":"1","code":null},"after":null,"source":{"version":"1.0.3.Final","connector":"postgresql","name":"test","ts_ms":1641450949231,"snapshot":"false","db":"test","schema":"public","table":"test_table","txId":589,"lsn":25325152,"xmin":null},"op":"d","ts_ms":1641450949232}} 
null

4.4 Create 和 Drop操作不会产生Topic数据

参考:

https://ctypyb2002.blog.csdn.net/article/details/121427957?spm=1001.2101.3001.6650.6&utm_medium=distribute.pc_relevant.none-task-blog-2%7Edefault%7EBlogCommendFromBaidu%7Edefault-6.pc_relevant_paycolumn_v2&depth_1-utm_source=distribute.pc_relevant.none-task-blog-2%7Edefault%7EBlogCommendFromBaidu%7Edefault-6.pc_relevant_paycolumn_v2&utm_relevant_index=9
https://blog.csdn.net/weixin_40898246/article/details/108327247
https://debezium.io/documentation/reference/1.2/connectors/postgresql.html#how-the-postgresql-connector-works
https://blog.csdn.net/javahelpyou/article/details/114536386

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 212,294评论 6 493
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 90,493评论 3 385
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 157,790评论 0 348
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 56,595评论 1 284
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 65,718评论 6 386
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 49,906评论 1 290
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 39,053评论 3 410
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 37,797评论 0 268
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 44,250评论 1 303
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 36,570评论 2 327
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 38,711评论 1 341
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 34,388评论 4 332
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 40,018评论 3 316
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 30,796评论 0 21
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 32,023评论 1 266
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 46,461评论 2 360
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 43,595评论 2 350

推荐阅读更多精彩内容