一、背景介紹
canal-adapter是canal的客戶端適配器,能夠直接將canal同步的數據寫入到目標數據庫(hbase,rdb,es),rdb是關係型數據庫比如MySQL、Oracle、PostgresSQL和SQLServer等,比較的快捷方便。本文以MySQL-->canalServer-->kafka-->canalAdaper-->MySQL爲例介紹canal-adapter的使用。
前提條件:
- canal.deployer-1.1.4已經安裝,在canal1節點。安裝教程
- MySQL數據庫兩個mysql1和mysql2
- kafka的版本2.11,在kafka1,kafka2,kafka3節點上
- zookeeper版本,在zk1節點上
二、下載配置
- 下載canal.adapter-1.1.4 解壓到canal-adapter目錄裏面:tar -zxvf canal.adapter-1.1.4.tar.gz -C canal-adapter
- 配置文件: vim canal-adapter/conf/application.yml
server:
port: 8081
logging:
level:
com.alibaba.otter.canal.client.adapter.rdb: INFO #日誌級別默認是DEBUG
spring:
jackson:
date-format: yyyy-MM-dd HH:mm:ss
time-zone: GMT+8
default-property-inclusion: non_null
canal.conf:
mode: kafka # kafka rocketMQ tcp
canalServerHost: canal1:11111
zookeeperHosts: zk1:2181
mqServers: kafka1:9092,kafka2:9092,kafka3:9092 #or rocketmq
flatMessage: false #開啓了數據壓縮格式,protobuf
batchSize: 500
syncBatchSize: 1000
retries: 0
timeout:
accessKey:
secretKey:
srcDataSources:
defaultDS:
url: jdbc:mysql://mysql1:3306/testOrg?useUnicode=true
username: rootOrg
password: rootOrg
canalAdapters:
- instance: topic1 # canal 實例的名字後者是kafka的topic名字
groups:
- groupId: g1
outerAdapters:
- name: rdb
key: mysql1
properties:
jdbc.driverClassName: com.mysql.jdbc.Driver
jdbc.url: jdbc:mysql://mysql2:3306/test?useUnicode=true
jdbc.username: root
jdbc.password: root2020
3.編輯 : vim canal-adapter/conf/rdb/mytest_user.yml
dataSourceKey: defaultDS #和application裏面的srcDataSources參數一致
destination: topic1 #canal的實例名字或者是kafka的topic名字
groupId: g1 #和application裏面的一致
outerAdapterKey: mysql1 #和application裏面的一致
concurrent: true
dbMapping:
database: testOrg #源MySQL的數據庫
table: orders #源MySQL的org的表orders
targetTable: test.orders #目標數據庫和表
targetPk:
id: id #MySQL的主鍵
mapAll: true #全量映射
# targetColumns: #部分映射
# id:
# name:
# role_id:
# c_time:
# test1:
# etlCondition: "where c_time>={}" # 簡單的etl處理
commitBatch: 100 # 批量提交的大小
三、啓動驗證
1、啓動:sh bin/start.sh
2、到mysql2的test.orders上查詢數據
3、查詢狀態:curl http://127.0.0.1:8081/destinations
四、高級用法
五、問題總結
有問題交流,請加個人主頁羣