debezium簡(jiǎn)單測(cè)試

現(xiàn)有需求穗熬,需要使用pg的數(shù)據(jù)實(shí)時(shí)統(tǒng)計(jì)一些指標(biāo)镀迂,經(jīng)過(guò)調(diào)研,決定使用kafkaCat或debezium將pg的操作日志同步到kafka中唤蔗,現(xiàn)將簡(jiǎn)單測(cè)試debezium的過(guò)程總結(jié)一下探遵。

debezium簡(jiǎn)介

1.debezium默認(rèn)行為是連接器將所有生成的事件流式傳輸?shù)皆摫淼膯为?dú)kafka主題窟赏。
2.PG需要安裝一個(gè)邏輯解碼輸出插件,插件可以是decoderbufs箱季、wal2json涯穷、pgoutput,我使用的是wal2json
3.連接器為捕獲的每個(gè)行級(jí)插入规哪、更新和刪除操作生成一個(gè)更改事件求豫,并未單獨(dú)的kafka主題中的每個(gè)表發(fā)送更改事件記錄
4.邏輯解碼不支持DDL更改,連接器無(wú)法將DDL更改事件報(bào)告回消費(fèi)者
5.只有primary的表支持邏輯解碼復(fù)制槽
6.目前僅支持使用UTF-8字符編碼的數(shù)據(jù)庫(kù)诉稍。使用單字節(jié)字符編碼蝠嘉,無(wú)法正確處理包含擴(kuò)展ASCII代碼字符的字符串

安裝wal2json插件

1.配置環(huán)境變量

#不是必須是~/.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項(xiàng)目并編譯安裝

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

3.修改postgresql.conf參數(shù)

因?yàn)閜ostgresql.conf在data目錄下杯巨,正常是沒(méi)有訪問(wèn)權(quán)限的蚤告,需要先使用管理員權(quán)限修改權(quán)限

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參數(shù),運(yùn)行kafka-connect后會(huì)報(bào)錯(cuò)

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目錄權(quán)限修改為700,否則會(huì)報(bào)錯(cuò)

安裝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

測(cè)試

1.啟動(dòng)kafka-connect

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

2.查看kafka的topic

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

3.啟動(dòng)消費(fèi)者杜恰,查看topic是否有數(shù)據(jù)

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

4.在pg的test庫(kù)下面對(duì)test_table進(jìn)行操作

4.1 test_table表結(jié)構(gòu)(必須要設(shè)置主鍵)

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

4.1 Insert操作

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

消費(fèi)者消費(fèi)到信息:

{"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';

消費(fèi)者消費(fèi)到信息:

{"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';

消費(fèi)者消費(fèi)到信息:如果是delete操作,則會(huì)多輸出一行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操作不會(huì)產(chǎn)生Topic數(shù)據(jù)

參考:

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

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末仍源,一起剝皮案震驚了整個(gè)濱河市心褐,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌笼踩,老刑警劉巖逗爹,帶你破解...
    沈念sama閱讀 219,490評(píng)論 6 508
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場(chǎng)離奇詭異嚎于,居然都是意外死亡掘而,警方通過(guò)查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,581評(píng)論 3 395
  • 文/潘曉璐 我一進(jìn)店門(mén)于购,熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái)袍睡,“玉大人,你說(shuō)我怎么就攤上這事肋僧“呤ぃ” “怎么了?”我有些...
    開(kāi)封第一講書(shū)人閱讀 165,830評(píng)論 0 356
  • 文/不壞的土叔 我叫張陵嫌吠,是天一觀的道長(zhǎng)止潘。 經(jīng)常有香客問(wèn)我,道長(zhǎng)居兆,這世上最難降的妖魔是什么覆山? 我笑而不...
    開(kāi)封第一講書(shū)人閱讀 58,957評(píng)論 1 295
  • 正文 為了忘掉前任,我火速辦了婚禮泥栖,結(jié)果婚禮上簇宽,老公的妹妹穿的比我還像新娘勋篓。我一直安慰自己,他們只是感情好魏割,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,974評(píng)論 6 393
  • 文/花漫 我一把揭開(kāi)白布譬嚣。 她就那樣靜靜地躺著,像睡著了一般钞它。 火紅的嫁衣襯著肌膚如雪拜银。 梳的紋絲不亂的頭發(fā)上,一...
    開(kāi)封第一講書(shū)人閱讀 51,754評(píng)論 1 307
  • 那天遭垛,我揣著相機(jī)與錄音尼桶,去河邊找鬼。 笑死锯仪,一個(gè)胖子當(dāng)著我的面吹牛泵督,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播庶喜,決...
    沈念sama閱讀 40,464評(píng)論 3 420
  • 文/蒼蘭香墨 我猛地睜開(kāi)眼小腊,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼!你這毒婦竟也來(lái)了久窟?” 一聲冷哼從身側(cè)響起秩冈,我...
    開(kāi)封第一講書(shū)人閱讀 39,357評(píng)論 0 276
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤,失蹤者是張志新(化名)和其女友劉穎斥扛,沒(méi)想到半個(gè)月后入问,有當(dāng)?shù)厝嗽跇?shù)林里發(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 45,847評(píng)論 1 317
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡犹赖,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,995評(píng)論 3 338
  • 正文 我和宋清朗相戀三年队他,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了卷仑。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片峻村。...
    茶點(diǎn)故事閱讀 40,137評(píng)論 1 351
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡,死狀恐怖锡凝,靈堂內(nèi)的尸體忽然破棺而出粘昨,到底是詐尸還是另有隱情,我是刑警寧澤窜锯,帶...
    沈念sama閱讀 35,819評(píng)論 5 346
  • 正文 年R本政府宣布张肾,位于F島的核電站,受9級(jí)特大地震影響锚扎,放射性物質(zhì)發(fā)生泄漏吞瞪。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,482評(píng)論 3 331
  • 文/蒙蒙 一驾孔、第九天 我趴在偏房一處隱蔽的房頂上張望芍秆。 院中可真熱鬧惯疙,春花似錦、人聲如沸妖啥。這莊子的主人今日做“春日...
    開(kāi)封第一講書(shū)人閱讀 32,023評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)荆虱。三九已至蒿偎,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間怀读,已是汗流浹背诉位。 一陣腳步聲響...
    開(kāi)封第一講書(shū)人閱讀 33,149評(píng)論 1 272
  • 我被黑心中介騙來(lái)泰國(guó)打工, 沒(méi)想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留菜枷,地道東北人不从。 一個(gè)月前我還...
    沈念sama閱讀 48,409評(píng)論 3 373
  • 正文 我出身青樓,卻偏偏與公主長(zhǎng)得像犁跪,于是被迫代替她去往敵國(guó)和親椿息。 傳聞我的和親對(duì)象是個(gè)殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 45,086評(píng)論 2 355

推薦閱讀更多精彩內(nèi)容