Debezium을 사용한 PostgreSQL CDC Kafka

  1. 安装JDK
    - # 타르 zxvf JDK-8u281-리눅스 x64.tar.gz -C / 옵션 /
    - # VI은 / etc / 프로필
    - #### #### 사육사
    수출 JAVA_HOME = / 옵션 / jdk1.8.0_281
    수출 CLASSPATH = $ JAVA_HOME / jre / lib : $ JAVA_HOME / lib
    내보내기 PATH = $ JAVA_HOME / bin : $ PATH

-# source / etc / profile-
# java -version
java version "1.8.0_281"
Java (TM) SE Runtime Environment (build 1.8.0_281-b09)
Java HotSpot (TM) 64-Bit Server VM (build 25.281-b09, 혼합 모드)
-#

  1. 安装 Kafka

-# tar zxvf kafka_2.13-2.7.0.tgz -C / opt /
-# cd /opt/kafka_2.13-2.7.0/config

-# vi / etc / profile

-#### 사육사 ####
export JAVA_HOME = / opt / jdk1.8.0_281
export KAFKA_HOME = / opt / kafka_2.13-2.7.0
export CLASSPATH = $ JAVA_HOME / jre / lib : $ JAVA_HOME / lib : $ KAFKA_HOME / lib
내보내기 PATH = $ KAFKA_HOME / bin : $ JAVA_HOME / bin : $ PATH

-#### 启动 zookeeper #####
-# cd /opt/kafka_2.13-2.7.0/-#
nohup bin / zookeeper-server-start.sh config / zookeeper.properties &

-#### 启动 kafka #####
-# nohup bin / kafka-server-start.sh config / server.properties &

-### 创建 topic ###
-# kafka-topics.sh --create --zookeeper localhost : 2181 --replication-factor 1 --partitions 1 --topic test
생성 된 주제 테스트.
-#

-### 显示 topic ###
-# kafka-topics.sh -list -zookeeper localhost : 2181
테스트
-#

kafka 생산자 생성 :
-# kafka-console-producer.sh --broker-list localhost : 9092 --topic test

안녕하세요
test_msg
test_topic

kafka 소비자 생성 :
-# kafka-console-consumer.sh --bootstrap-server localhost : 9092 --topic test --from-beginning
hello
test_msg
test_topic

설명 :
--broker-list : 기본 포트는 9092입니다. 직접 변경할 수 있습니다.

  1. 데이터베이스 준비

postgres = # 복제 암호 'sync_user'로 sync_user 사용자 생성;
postgres = # 수퍼 유저로 sync_user 사용자 변경;
postgres = # 데이터베이스 생성 tdb01;
CREATE DATABASE
postgres = # CREATE TABLE 고객 (
id SERIAL,
first_name VARCHAR (255) NOT NULL,
last_name VARCHAR (255) NOT NULL,
이메일 VARCHAR (255) NOT NULL,
PRIMARY KEY (id)
);

pg_hba.conf

-# 유형 데이터베이스 사용자 주소 방법

-# "로컬"은 Unix 도메인 소켓 연결 전용
로컬 모든 신뢰
-# IPv4 로컬 연결 :
모두 호스트 127.0.0.1/32 신뢰
호스트 모두 모두 0.0.0.0/0 md5-
# IPv6 로컬 연결 :
모두 호스트 : : 1/128 trust
-#
-# 복제 권한을 가진 사용자가 localhost에서 복제 연결을 허용합니다 .
로컬 복제 모두 신뢰
호스트 복제 모두 127.0.0.1/32 신뢰
호스트 복제 모두 192.168.1.0/24 md5
$

-### 구성 파일 수정 ####
$ vi postgresql.conf
wal_level = 논리적
#max_wal_senders = 10
#max_replication_slots = 10

-#### 데이터베이스 재부팅 ####
$ pg_ctl stop –D / pgdata
$ pg_ctl start –D / pgdata

  1. Connect 구성 및 설치

-# tar zxvf debezium-connector-postgres-1.4.1.Final-plugin.tar.gz -C / opt /
-# cd / opt / debezium-connector-postgres /
-# cp * .jar /opt/kafka_2.13 -2.7.0 / libs /
-# cd /opt/kafka_2.13-2.7.0/config-#
vi kafka-postgres.properties
name = postgres-connector
connector.class = io.debezium.connector.postgresql.PostgresConnector
데이터베이스. hostname = 192.168.1.141
database.port = 5432
database.user = sync_user
database.password = sync_user
database.dbname = tdb01
database.server.name = tdb01
plugin.name = pgoutput
table.whitelist =
errors.log.enable = true
오류입니다. logs.include.messages = true-
#

  1. 데이터베이스 삽입 데이터 테스트

-#### 数据库 端 ####
postgres = # \ c tdb01
tdb01 = # customers (first_name, last_name, email) 값 ( 'he', 'mia', '[email protected]');
삽입 0 1
tdb01 = #

- #### kafka 단 #####

-# kafka-topics.sh -list -zookeeper localhost : 2181
tdb01.public.customers-
# kafka-console-consumer.sh --bootstrap-server localhost : 9092 --topic tdb01.public.customers --from-beginning
{ "schema": { "type": "struct", "fields": [{ "type": "struct", "fields": [{ "type": "int32", "optional": false, "field ":"id "}, {"type ":"string ","optional ": false,"field ":"first_name "}, {"type ":"string ","optional ": false,"field ": "last_name"}, { "type": "string", "optional": false, "field": "email"}], "optional": true, "name": "tdb01.public.customers.Value", "field": "before"}, { "type": "struct", "fields": [{ "type": "int32", "optional": false, "field": "id"}, { "type ":"끈","optional": false, "field": "first_name"}, { "type": "string", "optional": false, "field": "last_name"}, { "type": "string", "optional ": false,"field ":"email "}],"optional ": true,"name ":"tdb01.public.customers.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 " , "선택 사항": 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 "}, {"type ":"struct ","fields " : [{ "type": "string", "optional": false, "field": "id"}, { "type": "int64", "optional": false, "field": "total_order"}, { "type": "int64", "optional": false, "field": "data_collection_order"}], "optional": true, "field": "transaction"}], "optional": false, "name" : "tdb01.public.customers.Envelope "},"payload ": {"before ": null,"after ": {"id ": 1,"first_name ":"he ","last_name ":"mia ","email ":"[email protected] "},"source ": {"version ":"1.4.1.Final ","connector ":"postgresql ","name ":"tdb01 ","ts_ms ": 1612544437937, "snapshot": "false", "db": "tdb01", "schema": "public", "table": "customers", "txId": 493, "lsn": 23092184, "xmin": null} , "op": "c", "ts_ms": 1612515639176, "transaction": null}}first_name ":"he ","last_name ":"mia ","email ":"[email protected] "},"source ": {"version ":"1.4.1.Final ","connector ":" postgresql ","name ":"tdb01 ","ts_ms ": 1612544437937,"snapshot ":"false ","db ":"tdb01 ","schema ":"public ","table ":"customers "," txId ": 493,"lsn ": 23092184,"xmin ": null},"op ":"c ","ts_ms ": 1612515639176,"transaction ": null}}first_name ":"he ","last_name ":"mia ","email ":"[email protected] "},"source ": {"version ":"1.4.1.Final ","connector ":" postgresql ","name ":"tdb01 ","ts_ms ": 1612544437937,"snapshot ":"false ","db ":"tdb01 ","schema ":"public ","table ":"customers "," txId ": 493,"lsn ": 23092184,"xmin ": null},"op ":"c ","ts_ms ": 1612515639176,"transaction ": null}}ts_ms ": 1612544437937,"snapshot ":"false ","db ":"tdb01 ","schema ":"public ","table ":"customers ","txId ": 493,"lsn ": 23092184," xmin ": null},"op ":"c ","ts_ms ": 1612515639176,"transaction ": null}}ts_ms ": 1612544437937,"snapshot ":"false ","db ":"tdb01 ","schema ":"public ","table ":"customers ","txId ": 493,"lsn ": 23092184," xmin ": null},"op ":"c ","ts_ms ": 1612515639176,"transaction ": null}}

추천

출처blog.51cto.com/heyiyi/2621287