Canal同步mysql數據至ES
1、更改Mysql配置
1.1 開啓 Binlog 寫入功能
配置 binlog-format 爲 ROW 模式,配置my.cnf
[mysqld]
log-bin=mysql-bin # 開啓 binlog
binlog-format=ROW # 選擇 ROW 模式
server_id=1 # 配置 MySQL replaction 需要定義,不要和 canal 的 slaveId 重複
1.2 授權
授權 canal 鏈接 MySQL 賬號具有作爲 MySQL slave 的權限, 如果已有賬戶可直接 grant
CREATE USER canal IDENTIFIED BY 'canal';
GRANT SELECT, REPLICATION SLAVE, REPLICATION CLIENT ON *.* TO 'canal'@'%';
-- GRANT ALL PRIVILEGES ON *.* TO 'canal'@'%' ;
FLUSH PRIVILEGES;
2、下載canal.deployer
2.1 建議下載最新穩定版,我下載的是1.1.4版本。
wget https://github.com/alibaba/canal/releases/download/canal-1.1.4/canal.deployer-1.1.4.tar.gz
2.2 解壓縮
mkdir /data/canal/deployer
tar -zxvf canal.deployer-1.1.4.tar.gz -C deployer/
解壓後爲:
2.3 修改canal配置
vi conf/example/instance.properties
## mysql serverId
canal.instance.mysql.slaveId = 1234
#position info,需要改成自己的數據庫信息
canal.instance.master.address = 127.0.0.1:3306
canal.instance.master.journal.name =
canal.instance.master.position =
canal.instance.master.timestamp =
#canal.instance.standby.address =
#canal.instance.standby.journal.name =
#canal.instance.standby.position =
#canal.instance.standby.timestamp =
#username/password,需要改成自己的數據庫信息
canal.instance.dbUsername = canal
canal.instance.dbPassword = canal
canal.instance.defaultDatabaseName =
canal.instance.connectionCharset = UTF-8
#table regex
canal.instance.filter.regex = .\*\\\\..\*
2.4 啓動
sh bin/startup.sh
3、下載canal.adapter
3.1 下載
wget https://github.com/alibaba/canal/releases/download/canal-1.1.4/canal.adapter-1.1.4.tar.gz
3.1 適配器整體結構
client-adapter分爲適配器和啓動器兩部分, 適配器爲多個fat jar, 每個適配器會將自己所需的依賴打成一個包, 以SPI的方式讓啓動器動態加載, 目前所有支持的適配器都放置在plugin目錄下
啓動器爲 SpringBoot 項目, 支持canal-client啓動的同時提供相關REST管理接口, 運行目錄結構爲:
- bin
restart.sh
startup.bat
startup.sh
stop.sh
- lib
...
- plugin
client-adapter.logger-1.1.1-jar-with-dependencies.jar
client-adapter.hbase-1.1.1-jar-with-dependencies.jar
...
- conf
application.yml
- hbase
mytest_person2.yml
- logs
以上目錄結構最終會打包成 canal-adapter-1.1.4.tar.gz 壓縮包
3.1 適配器配置介紹
1、總配置文件application.yml
1.1 adapter定義配置部分
canal.conf:
mode: tcp # canal client的模式: tcp kafka rocketMQ
canalServerHost: 123.126.41.204:20482 # 對應單機模式下的canal server的ip:port
batchSize: 500 # 每次獲取數據的批大小, 單位爲K
syncBatchSize: 1000 # 每次同步的批數量
retries: 0 # 重試次數, -1爲無限重試
timeout: # 同步超時時間, 單位毫秒
srcDataSources:
defaultDS: # 自定義名稱
url: jdbc:mysql://123.126.41.204:20456/canal_test?useUnicode=true
username: root
password: root
canalAdapters: # 適配器列表
- instance: example # canal 實例名或者 MQ topic 名
groups:
- groupId: g1 # 分組id
outerAdapters: # 分組內適配器列表
- name: logger # 日誌
- name: es #elasticsearch
hosts: 123.126.41.204:20475
properties:
cluster.name: eshao-application
- instance: one
groups:
- groupId: g1
outerAdapters:
- name: logger
- name: es
hosts: 123.126.41.204:20475
properties:
cluster.name: eshao-application
說明:
一份數據可以被多個group同時消費, 多個group之間會是一個並行執行, 一個group內部是一個串行執行多個outerAdapters, 比如例子中logger和es
3.1 適配器啓動
3.1.1、啓動canal server
見2.4
3.1.2、修改conf/application.yml
server:
port: 8081
spring:
jackson:
date-format: yyyy-MM-dd HH:mm:ss
time-zone: GMT+8
default-property-inclusion: non_null
canal.conf:
mode: tcp
canalServerHost: ip:port
batchSize: 500
syncBatchSize: 1000
retries: 0
timeout:
srcDataSources:
defaultDS:
url: jdbc:mysql://ip:port/db?useUnicode=true
username: username
password: password
canalAdapters:
- instance: example
groups:
- groupId: g1
outerAdapters:
- name: logger
3.1.3、啓動
bin/startup.sh
3.1.4、測試:adapter管理REST接口
查詢所有訂閱同步的canal instance或MQ topic
curl http://127.0.0.1:8081/destinations
數據同步開關
curl http://127.0.0.1:8081/syncSwitch/example/off -X PUT
針對 example 這個canal instance/MQ topic 進行開關操作. off代表關閉, instance/topic下的同步將阻塞或者斷開連接不再接收數據, on代表開啓
數據同步開關狀態
curl http://127.0.0.1:8081/syncSwitch/example
手動同步
curl http://127.0.0.1:8081/etl/es/mytest_person2.yml -X POST
導入數據到指定類型的庫, 如果params參數爲空則全表導入, 參數對應的查詢條件在配置中的etlCondition指定
查看相關庫總數據
curl http://127.0.0.1:8081/count/hbase/mytest_person2.yml
3.2 同步至ES
3.2.1 修改啓動器配置: application.yml
server:
port: 8081
spring:
jackson:
date-format: yyyy-MM-dd HH:mm:ss
time-zone: GMT+8
default-property-inclusion: non_null
canal.conf:
mode: tcp
canalServerHost: ip:port
batchSize: 500
syncBatchSize: 1000
retries: 0
timeout:
srcDataSources:
defaultDS:
url: jdbc:mysql://ip:port/db?useUnicode=true
username: root
password: root
canalAdapters:
- instance: example
groups:
- groupId: g1
outerAdapters:
- name: logger
- name: es
hosts: ip:port
properties:
cluster.name: eshao-application
- instance: one
groups:
- groupId: g1
outerAdapters:
- name: logger
- name: es
hosts: ip:port
properties:
cluster.name: eshao-application
adapter將會自動加載 conf/es 下的所有.yml結尾的配置文件
3.2.2 適配器表映射文件
修改 conf/es/mytest_user.yml文件:
dataSourceKey: defaultDS # 源數據源的key, 對應上面配置的srcDataSources中的值
destination: example # cannal的instance或者MQ的topic
esMapping:
_index: mytest_person # es 的索引名稱
_type: _doc # es 的type名稱
_id: _id # es 的_id, 如果不配置該項必須配置下面的pk項_id則會由es自動分配
upsert: true
# pk: id # 如果不需要_id, 則需要指定一個屬性爲主鍵屬性
sql: "SELECT p.id as _id,name,age,last_update_time as time from person p"
# objFields:
# _labels: array:; # 數組或者對象屬性, array:; 代表以;字段裏面是以;分隔的
# etlCondition: "where a.c_time>={}"
commitBatch: 3000
修改 conf/es/one.yml文件:
dataSourceKey: defaultDS # 源數據源的key, 對應上面配置的srcDataSources中的值
destination: one # cannal的instance或者MQ的topic
esMapping:
_index: index_one # es 的索引名稱
_type: _doc # es 的type名稱
_id: _id # es 的_id, 如果不配置該項必須配置下面的pk項_id則會由es自動分配
upsert: true
# pk: id # 如果不需要_id, 則需要指定一個屬性爲主鍵屬性
sql: "SELECT o.id as _id, name from one o"
# objFields:
# _labels: array:; # 數組或者對象屬性, array:; 代表以;字段裏面是以;分隔的
# etlCondition: "where a.c_time>={}"
commitBatch: 3000
注意:進行同步時需要先在ES中建立索引及映射