**
概述
**
现在的项目数据量越来越大,全文检索功能使用场景也越来越普遍。
而我们一般的生产数据是在mysql,或其它一些数据库,
我们的产品数据就是mysql,而又要使用全文检索,
所以要把mysql 数据同步到es,再进行全文检索功能。
现在简单把数据同步的配置及遇到的问题罗列如下:
数据流程图
mysql -> adapter -> es
安装步骤不再累述,需要安装以下软件。
1.安装 es,
2.安装 canal.adapter
3.安装 canal.deployer
4.安装 zookeeper
数据流同步过程中,需要配置的地方:
(dev 开发环境示例)
1.adapter:
application.yml
canal.conf:
mode: tcp # kafka rocketMQ
canalServerHost: 172.16.1.2:11111
zookeeperHosts: 172.16.1.2:2181
mqServers: 172.16.1.2:9876 #or rocketmq
srcDataSources:
defaultDS:
url: jdbc:mysql://172.16.1.203:33306/nuocheng?useUnicode=true
username: canal
password: canal.1qaz
canalAdapters:
- instance: example # canal instance Name or mq topic name
groups:- groupId: g1
outerAdapters:- name: es7
key: es204
hosts: 172.16.1.2:9200
properties:
mode: rest # or rest
cluster.name: my-application
- name: es7
- groupId: g1
3.安装 canal.deployer
vi canal.properties
canal.ip = 172.16.1.2
canal.port = 11111
canal.metrics.pull.port = 11112
canal.zkServers = 172.16.1.2
vi example/instalce.properties
# username/password
canal.instance.dbUsername=canal
canal.instance.dbPassword=canal.123456
canal.instance.connectionCharset = UTF-8
canal.instance.defaultDatabaseName = nuocheng
(重建立索引全量生成数据)
重建立索引:
curl -XDELETE http://172.16.1.2:9200/nt_product
curl -XDELETE http://172.16.1.2:9200/nt_news
curl -XDELETE http://172.16.1.2:9200/nt_plan
curl -XPUT -H “Content-Type: application/json” http://172.16.1.2:9200/nt_product?include_type_name=true -d “@nt_product.json”
curl -XPUT -H “Content-Type: application/json” http://172.16.1.2:9200/nt_news?include_type_name=true -d “@nt_news.json”
curl -XPUT -H “Content-Type: application/json” http://172.16.1.2:9200/nt_plan?include_type_name=true -d “@nt_plan.json”
全量同步一次数据
curl http://172.16.1.204:38081/etl/es/nt_product.yml -XPOST
curl http://172.16.1.204:38081/etl/es/nt_news.yml -XPOST
curl http://172.16.1.204:38081/etl/es/nt_plan.yml -XPOST
#########################################################################
(使用导出导入方式重新生成数据)
ES 同级后,数据的导入导出:
1.安装 elasticdump:
npm install elasticdump -g
elasticdump --version
6.33.4
---- 出错解决方法
[root@nctest004 ~]# elasticdump --version
/usr/lib/node_modules/elasticdump/lib/processor.js:40
async _loop (limit, offset, totalWrites) {
^^^^^
SyntaxError: Unexpected identifier
at createScript (vm.js:56:10)
at Object.runInThisContext (vm.js:97:10)
at Module._compile (module.js:549:28)
at Object.Module._extensions..js (module.js:586:10)
at Module.load (module.js:494:32)
at tryModuleLoad (module.js:453:12)
at Function.Module._load (module.js:445:3)
at Module.require (module.js:504:17)
at require (internal/module.js:20:19)
at Object.<anonymous> (/usr/lib/node_modules/elasticdump/elasticdump.js:3:28)
--------------------------- 安装出错 -----------------------
原因是资源的问题,要配置淘宝镜像
npm config set registry https://registry.npm.taobao.org
配置完成后检验是否成功
npm config get registry
重新安装
npm install nrm -g
查看所用的镜像
nrm ls
安装成功
解决办法:
#升级nodejs
[root@localhost ~]# npm install -g n
[root@localhost ~]# n latest
2.导出数据:
elasticdump --input=http://172.16.1.2:9200/nt_product --output=/opt/download/nt_product.json --type=data
3:删除数据:
curl --location --request DELETE 'http://172.16.1.2:9200/rangetest'
4.导入数据
elasticdump --input=/opt/download/nt_product.json --output=http://172.16.1.2:9200/nt_product --type=data