文章目录
canal之简单场景应用Demo
转载自本人文章:http://techblog.sishuxuefu.com/atricle.html?5dfc918bdd3c1300729ce424
用于数据同步
针对mysql场景进行介绍
参考文档 https://www.bookstack.cn/read/canal-v1.1.4/507a40ad1c1335da.md
非集群-表映射模式
场景:将A库中的表同步到B库中的表,表与表之间可以做字段级别的映射配置
组件:
- deployer模拟了myslq从库
- adapter提供了具体的映射实现,需要连接到deployer提供的服务
- 可选admin,这里不做介绍
deployer
也是canal的server端,伪装成mysql从库
配置文件详细说明可以参考 https://www.bookstack.cn/read/canal-v1.1.4/10a3a22ce51cd92e.md
配置文件
-
canal.properties配置文件
此模式下不需要调整,但是可以更改暴露的服务端口
-
example/instance.properties
-
配置主库的ip、port、用户名、密码等
-
根据需要配置同步的binlog位置,如果留空,默认是当前位置
-
canal.instance.filter.regex给出了对表监听的过滤,详细可见本组件连接。
-
-
启动类
com/alibaba/otter/canal/deployer/CanalLauncher.java
client-adapter
适配器,完成同步工作,需要连接到deployer
此模式下介绍:
完全镜像,类似mysql主从复制
部分映射,可配置表A到表B的映射字段规则
- 所有字段映射
- 部分字段映射
配置文件
-
auncher模块中的application.yml
- 配置与deployer的连接
- 配置源库和目标库
- 为instance命名
示例文件
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: 127.0.0.1:11111 batchSize: 500 syncBatchSize: 1000 retries: 0 timeout: accessKey: secretKey: srcDataSources: defaultDS: url: jdbc:mysql://192.168.2.116:13306/avengers_canal?useUnicode=true username: canal password: canal canalAdapters: - instance: example # canal instance Name or mq topic name groups: - groupId: g1 outerAdapters: - name: logger - name: rdb key: mysql1 properties: jdbc.driverClassName: com.mysql.jdbc.Driver jdbc.url: jdbc:mysql://127.0.0.1:3306/avengers_test?useUnicode=true jdbc.username: root jdbc.password: 123456
-
rdb模块中resources/rdb目录的配置
示例文件
dataSourceKey: defaultDS destination: example groupId: g1 outerAdapterKey: mysql1 concurrent: true dbMapping: database: avengers_canal table: config_orders targetTable: config_orders targetPk: id: id targetColumns: id: status: name: org_id: valid_date_start: valid_date_end: config_orders_type_id: config_period_price_id: serie: tenant_id: created_at: updated_at: created_by: updated_by: is_delete:
dataSourceKey:对应了application.yml中的canal.conf.srcDataSources.defaultDS
destination:对应了canal.conf.canalAdapters[0].instance
groupId:canal.conf.canalAdapters[0].groups[0].groupId
outerAdapterKey:canal.conf.canalAdapters[0].groups[0].outerAdapters[1].key
dbMapping下配置具体的表映射规则
- database对应defaultDS下配置的数据库名称
- table原表
- targetTable目标表
- targetPk主键映射
- targetColumns列映射,需要把主键写上。value不写默认名称一致。
- mapAll属性为true时,全映射,此时如果有targetColumns,以targetColumns为准
-
如果是在源码中调试,如果变动了就需要install到本地库
rest接口
-
手动etl
支持将某个表下的数据从某事件点开始进行同步
curl -X POST \ http://127.0.0.1:8081/etl/rdb/mysql1/config_period_price.yml \ -H 'Content-Type: application/x-www-form-urlencoded' \ -d params=2018-10-21%2000%3A00%3A00
config_period_price:rdb配置的yml文件名
rdb:同步方式canal.conf.canalAdapters[0].groups[0].outerAdapters[1].name
mysql1:canal.conf.canalAdapters[0].groups[0].outerAdapters[1].key
-
获取所有instance
http://127.0.0.1:8081/destinations
-
统计某表总数
http://127.0.0.1:8081/count/rdb/mysql1/config_discount.yml
-
同步开关
curl http://127.0.0.1:8081/syncSwitch/example/off -X PUT
admin
运维与配置中心
client
客户端
附录
简单工具集
python批量生成yml工具
https://gitee.com/fantasyzsp/py-yml2md/blob/master/mysql/ReadTables.py
批量请求Etl接口脚本
https://gitee.com/fantasyzsp/py-yml2md/blob/master/mysql/Etl.py