方式一 :elasticSearchDump
官网文档:elasticdump - npm
全局安装:
npm install elasticdump -g
1) 从sourceIp集群迁移数据到dstIp
elasticdump \
--input=http://sourceIp:9200/my_index \
--output=http://dstIp:9200/my_index \
--type=data
2) 迁移mapping
elasticdump \
--input=http://production.es.com:9200/my_index \
--output=http://staging.es.com:9200/my_index \
--type=mapping
3)下载数据
elasticdump \ --input=http://production.es.com:9200/my_index \ --output=query.json \ --searchBody="{\"query\":{\"term\":{\"username\": \"admin\"}}}"
4)将下载的数据压缩
elasticdump \
--input=http://192.168.3.100:9200/doc_hit_info \
--searchBody="{\"query\":{\"bool\":{\"must\":[{\"range\":{\"datetime\":{\"gt\":\"1451577600000\",\"lt\":\"1464710400000\"}}}]}}}" \
--type=data \
--output=$ \
| gzip > /data/zjc/doc_hit_info.gz
方式二:elasticSearch5.0以上支持reindex接口的迁移
官网文档:Reindex from a remote cluster | Elasticsearch Guide [7.13] | Elastic
1)使用前必须在新集群配置白名单
reindex.remote.whitelist: oldhost:9200
2)开始执行迁移任务,需要提前创建索引
curl -X POST "localhost:9200/_reindex?pretty" -H 'Content-Type: application/json' -d' { "source": { "remote": { "host": "http://oldhost:9200" }, "index": "source", "query": { "match": { "test": "data" } } }, "dest": { "index": "dest" } } '
异步执行,返回taskID
3)通过taskID可以查询任务执行情况
GET _tasks/TASK_ID
4)向新集群迁移数据过程中,如果出现告警 ,需要杀死正在进行的迁移任务,重新设置查询条件。
参照官网文档:Task Management API | Elasticsearch Guide [6.4] | Elastic
执行下行杀死未执行完的任务
curl -X POST "localhost:9200/_tasks/oTUltX4IQMOUUVeiohTt8A:12345/_cancel?pretty"
两种方式的对比:
elasticSearchDump 不限制es版本,可同步迁移也可异步迁移数据,不需要配置白名单,缺少任务监控,数据量大控制不好查询条件容易内存溢出;
reindex es5.0以上新增API;可以支持从es5.0以下版本向es5.0及以上迁移,必须配置白名单,异步执行,详细的任务状态监控;速度相对更快;