授权 canal 链接 MySQL 账号具有作为 MySQL slave 的权限, 如果已有账户可直接 grant
1 2 3 4
CREATE USER canal IDENTIFIED BY 'canal'; GRANT SELECT, REPLICATION SLAVE, REPLICATION CLIENT ON *.* TO 'canal'@'%'; -- GRANT ALL PRIVILEGES ON *.* TO 'canal'@'%' ; FLUSH PRIVILEGES;
mkdir -p /usr/local/canal/canal-adapter cd /usr/local/canal/canal-adapter wget https://github.com/alibaba/canal/releases/download/canal-1.1.5/canal.adapter-1.1.5.tar.gz
将canal.adapter进行解压
1
tar -zxvf canal.adapter-1.1.5.tar.gz
修改启动器配置application.yml
1
vim conf/application.yml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
srcDataSources:#数据源 defaultDS: url:jdbc:mysql://10.0.12.4:3306/test?useUnicode=true username:canal password:123456 canalAdapters:#适配器 -instance:example# canal instance Name or mq topic name groups: -groupId:g1 outerAdapters: -name:logger - key:exampleKey name:es7# or es6,adapter将会自动加载conf/es7下的所有.yml结尾的配置文件 hosts:http://10.0.12.4:9200# 127.0.0.1:9200 for rest mode properties: mode:rest# transport or rest cluster.name:my-application# es cluster name
适配器表映射文件
修改es7下面的mytest_user.yml
1 2 3 4 5 6 7 8 9 10 11 12 13 14
dataSourceKey:defaultDS# 源数据源的key, 对应上面配置的srcDataSources中的值 outerAdapterKey:exampleKey# 对应application.yml中es配置的key destination:example# cannal的instance或者MQ的topic groupId:g1# 对应MQ模式下的groupId, 只会同步对应groupId的数据 esMapping: _index:mytest_user# es 的索引名称 _id:_id# es 的_id, 如果不配置该项必须配置下面的pk项_id则会由es自动分配 # upsert: true # pk: id # 如果不需要_id, 则需要指定一个属性为主键属性 sql:"select a.id as _id, a.id,a.name, a.role_id, a.created_time from user a" # objFields: # 数组或者对象属性, array:; 代表以;字段里面是以;分隔的 # _labels: array:; # array或者json对象 etlCondition:"where a.created_time>={}"# etl 的条件参数 commitBatch:3000
dataSourceKey:defaultDS outerAdapterKey:exampleKey# 对应application.yml中es配置的key destination:example groupId:g1 esMapping: _index:mytest_user_detail _id:_id # upsert: true # pk: id sql:"select a.id as _id, a.id,a.name, a.role_id, r.role_name,a.created_time ,d.gender,d.birthday,d.phone from user a LEFT JOIN user_role r on r.id=a.role_id LEFT JOIN user_detail d on a.id=d.user_id" # objFields: # _labels: array:; etlCondition:"where a.created_time>={}" commitBatch:3000
srcDataSources:#数据源 defaultDS: url:jdbc:mysql://10.0.12.4:3306/test?useUnicode=true username:canal password:123456 canalAdapters:#适配器 -instance:example# canal instance Name or mq topic name groups: -groupId:g1 outerAdapters: -name:logger - key:exampleKey name:es7# or es6,adapter将会自动加载conf/es7下的所有.yml结尾的配置文件 hosts:http://10.0.12.4:9200# 127.0.0.1:9200 for rest mode properties: mode:rest#transport or rest # security.auth: test:123456 # only used for rest mode cluster.name:my-application# es cluster name
#手动ETL进行全量同步 curl http://127.0.0.1:8081/etl/es7/exampleKey/mytest_user.yml -X POST curl http://127.0.0.1:8081/etl/es7/exampleKey/mytest_user_detail.yml -X POST