一 安裝
BottledWater-PG的安裝前文已經表述,本文不贅述直接進入集成應用階段。
二 啟動KafKa
#啟動zookeeper
[root@bogon kafka_2.11-0.10.2.0]# bin/zookeeper-server-start.sh config/zookeeper.properties
#啟動kafka服務端
[root@bogon kafka_2.11-0.10.2.0]# bin/kafka-server-start.sh config/server.properties
三 配置PostgreSQL
3.1 配置讀取權限
Bottled Water會連接到postgresql獲取相關數據,連接的賬戶需要有replication權限,pg中數據庫的變化存儲在WAL中,至少需要replication權限才能讀取WAL。
編輯$PGDATA目錄中postgresql.conf和pg_hba.conf文件。
vi $PGDATA/postgresql.conf
#編輯內容如下:
listen_addresses = '*'
port = 5432
wal_level = logical
max_wal_senders = 8
wal_keep_segments = 4
max_replication_slots = 4
vi $PGDATA/pg_hba.conf
#編輯內容如下:
# IPv4 local connections:
host all all 0.0.0.0/0 md5
# replication privilege.
local replication freerep trust
host replication freerep 127.0.0.1/32 trust
host replication freerep ::1/128 trust
編輯完保存,重啟數據庫服務:
pg_ctl restart
psql
postgres=# CREATE ROLE freerep WITH REPLICATION PASSWORD 'password' LOGIN;
CREATE ROLE
配置完畢!
3.2 Bottled Water使用演示
3.2.1 創建測試庫表
創建一個測試數據庫,建立測試表
postgres=# create database mcsas;
postgres=# \c mcsas;
mcsas=# create extension bottledwater;
mcsas=# create extension postgis;
#賦予public下的表給freerep角色,要創建如下語句,否則建立的表freerep沒有讀取權限
mcsas=# alter default privileges in schema public grant all on tables to freerep;
mcsas=# create table gps(gid serial primary key,name text,geom text);
mcsas=# create index gps_geom_idx on gps using gist(ST_GeomFromText(geom,4326));
在另一個終端啟動bottledwater可執行程序:
source /home/postgres/.bashrc
cd /opt/bottledwater-pg-master/kafka
[root@localhost kafka]# ./bottledwater -d postgres://freerep:password@127.0.0.1/mcsas -b 192.168.43.27:9092 -f json
啟動結果如下:
[root@bogon kafka]# ./bottledwater -d postgres://freerep:password@127.0.0.1/mcsas -b 192.168.43.27:9092 -f json
[INFO] Writing messages to Kafka in JSON format
[INFO] Created replication slot "bottledwater", capturing consistent snapshot "0000DA72-1".
INFO: bottledwater_export: Table public.spatial_ref_sys is keyed by index spatial_ref_sys_pkey
INFO: bottledwater_export: Table public.mark is keyed by index mark_pkey
[INFO] Registering metadata for table spatial_ref_sys (relid 24263)
[INFO] Opening Kafka topic "spatial_ref_sys" for table "spatial_ref_sys"
[INFO] Storing key schema for table 24263
[INFO] Storing row schema for table 24263
[INFO] Snapshot complete, streaming changes from 0/AB016F30.
代表啟動成功了。
3.2.2 監聽數據改變消息
插入數據
mcsas=# insert into gps(name,geom) values ('china','Point(118 32)');
INSERT 0 1
mcsas=# insert into gps(name,geom) values ('england','Point(118 12)');
INSERT 0 1
啟動監聽topic
bin/kafka-console-consumer.sh --bootstrap-server 192.168.43.27:9092 --topic gps --from-beginning
{"gid": {"int": 1}, "name": {"string": "china"}, "geom": {"string": "Point(118 32)"}}
{"gid": {"int": 2}, "name": {"string": "england"}, "geom": {"string": "Point(118 12)"}}
每當插入或者更新,收聽的消息會源源不斷的輸出出來,這樣,pg與kafka集成就完畢了。