1. 首页
  2. >
  3. 技术专题
  4. >
  5. Lucene

Elasticsearch数据迁移方案

前言

最近要去给客户部署一套系统涉及到了Mysql和ES数据的迁移,下面就给大家分享一下ES数据迁移的几套方案,根据具体的使用场景来选择不同的迁移方案能使你事倍功半,话多说下面就一一介绍。

Elasticsearch数据迁移方案

Elasticsearch-dump(工具)

Elasticsearch数据迁移方案

这里有很多版本需要大家注意:

Elasticdump的1.0.0版更改了转储创建的文件的格式。使用此工具的版本0.x.x创建的文件可能不适用于以后的版本。要了解有关重大更改的更多信息,请访问1.0.0版的发行说明。如果收到“内存不足”错误,则可能是或很可能是原因。

Elasticdump版本2.0.0删除了批量选项。这些选项是错误的,并且在Elasticsearch的版本之间有所不同。如果需要导出多个索引,请查找该工具的multielasticdump部分。

Elasticdump版本2.1.0从使用扫描/滚动(ES 1.x)变为仅扫描(ES 2.x)。这是Elasticsearch中的向后兼容更改,但是在2.x之前的Elasticsearch版本上性能可能会受到影响。

Elasticdump版本3.0.0的默认查询已更新,仅适用于ElasticSearch版本5+。该工具可能与Elasticsearch的早期版本兼容,但是我们的版本检测方法可能不适用于所有ES集群拓扑

Elasticdump的5.0.0版包含s3传输的重大更改。不再支持s3Bucket和s3RecordKey参数,请改用s3urls

Elasticdump的6.1.0及更高版本包含对上载/转储过程的更改。此更改允许重叠的承诺处理。其优点是由于增加了并行处理而提高了性能,但是存在副作用,即记录记录(数据集)没有按顺序进行处理(不再保证排序)

  • 安装

本地安装

Elasticsearch数据迁移方案

npm install xx

因为这个工具是用node.js写的,所以可以直接使用npm直接安装

全局安装

npm install elasticdump -g elasticdump

局部安装

npm install elasticdump ./bin/elasticdump

Docker镜像

咱们在DockerHub上面可以搜索到

Elasticsearch数据迁移方案

这里我使用的是:

docker pull elasticdump/elasticsearch-dump

Elasticsearch数据迁移方案

  • 使用

主要参数:

--input: 源地址,可为 ES 集群 URL、文件或 stdin,可指定索引,格式为:{protocol}://{host}:{port}/{inde x} --input-index: 源 ES 集群中的索引 --output: 目标地址,可为 ES 集群地址 URL、文件或 stdout,可指定索引,格式为:{protocol}://{host}: {port}/{index} --output-index: 目标 ES 集群的索引 --type: 迁移类型,默认为 data,表明只迁移数据,可选 settings, analyzer, data, mapping, alias

上面这只是一部分,具体大家可以使用--help查看

Elasticsearch数据迁移方案

下面就举几个例子:

本地安装适用:

# Copy an index from production to staging with mappings: elasticdump \   --input=http://production.es.com:9200/my_index \   --output=http://staging.es.com:9200/my_index \   --type=mapping elasticdump \   --input=http://production.es.com:9200/my_index \   --output=http://staging.es.com:9200/my_index \   --type=data  # Backup index data to a file: elasticdump \   --input=http://production.es.com:9200/my_index \   --output=/data/my_index_mapping.json \   --type=mapping elasticdump \   --input=http://production.es.com:9200/my_index \   --output=/data/my_index.json \   --type=data  # Backup and index to a gzip using stdout: elasticdump \   --input=http://production.es.com:9200/my_index \   --output=$ \   | gzip > /data/my_index.json.gz  # Backup the results of a query to a file elasticdump \   --input=http://production.es.com:9200/my_index \   --output=query.json \   --searchBody "{\"query\":{\"term\":{\"username\": \"admin\"}}}"

Docker安装使用

# Copy an index from production to staging with mappings: docker run --rm -ti elasticdump/elasticsearch-dump \   --input=http://production.es.com:9200/my_index \   --output=http://staging.es.com:9200/my_index \   --type=mapping docker run --rm -ti elasticdump/elasticsearch-dump \   --input=http://production.es.com:9200/my_index \   --output=http://staging.es.com:9200/my_index \   --type=data  # Backup index data to a file: docker run --rm -ti -v /data:/tmp elasticdump/elasticsearch-dump \   --input=http://production.es.com:9200/my_index \   --output=/tmp/my_index_mapping.json \   --type=data
  • 适用范围

适用于数据量不大并且迁移索引量小的情况下

注意: 此操作并不能迁移索引的配置,例如分片数量和副本数量,必须对每个索引单独进行配置的迁移,或者直 接在目标集群中将索引创建完毕后再迁移数据。

Logstash

logstash 支持从一个 ES 集群中读取数据然后写入到另一个 ES 集群,因此可以使用 logstash 进行数据迁移

  • 使用
input {   elasticsearch {     hosts => ["http://xxx.xx.xx.x:9200"] index => "*"     docinfo => true     }   }   output {     elasticsearch {     	hosts => ["http://xxx.xx.xx.x:9200"] index => "%{[@metadata][_index]}"      } }
  • 适用范围

目标集群和源集群必须同一局域网内,否则不能使用。

Snapshot

Snapshot api 是 ES 用于对数据进行备份和恢复的一组 api 接口,可以通过 snapshot api 进行跨集群的数据迁移, 原理就是从源 ES 集群创建数据快照,然后在目标 ES 集群中进行恢复。


说明:
目标 ES 集群的主版本号(如5.6.4版本中的5为主版本号)要大于等于源 ES 集群的主版本号。1.x 版本的集群 创建的快照不能在 5.x 版本中恢复。

  • 使用

1.首先要在源 ES 集群中创建 repository
创建快照前必须先创建 repository 仓库,一个 repository 仓库可以包含多份快照文件,repository 主要有以下 几种类型:

fs:共享文件系统,将快照文件存放于文件系统中。 url:指定文件系统的 URL 路径,支持协议:http、https、ftp、file、jar。 s3:AWS S3 对象存储,快照存放于 S3 中,以插件形式支持,安装插件 repository-s3。  hdfs:快照存放于 hdfs 中,以插件形式支持,安装插件 repository-hdfs。 cos:快照存放于腾讯云 COS 对象存储中,以插件形式支持,安装插件 elasticsearch-repository-cos。

在elasticsearch.yml中配置参数path.repo: ["/usr/local/es/test"]

配置好后重启ES集群

接下来就是要(调用api)创建 repository

curl -XPUT http://xxx.xxx.xx.xx:9200/_snapshot/my_backup -H 'Content-Type: application/json' -d  '{    "type": "fs",     "settings": {       "location": "/usr/local/es/test"       "compress": true      } }'

AWS的S3、阿里云OSS、腾讯云的COS使用

curl -XPUT http://xxx.xx.xx.xx:9200/_snapshot/my_s3_repository  {   "type": "s3",   "settings": {   	"bucket": "my_bucket_name",      "region": "us-west"   } }

2. 源 ES 集群中创建 snapshot
调用 snapshot api 在创建好的仓库中创建快照。创建快照可以指定索引,也可以指定快照中包含的内容,具体的api 接口参数可以查阅官方文档https://www.elastic.co/guide/en/elasticsearch/reference/6.4/modules-snapshots.html。

curl -XPUT http://xxx.xxx.xx.xx:9200/_snapshot/my_backup/snapshot_1?wait_for_completion=true

3.目标 ES 集群中创建 repository
目标 ES 集群中创建仓库和在源 ES 集群中创建仓库类似

用户可在腾讯云上创建 COS 对象 bucket,把仓库建在 COS 的某个 bucket 下。

4.移动源 ES 集群 snapshot 至目标 ES 集群的仓库
把源 ES 集群创建好的 snapshot 上传至目标 ES 集群创建好的仓库中。

5.从快照恢复

curl -XPUT http://xxx.xx.xxx.xx:9200/_snapshot/my_backup/snapshot_1/_restore

6.查看快照恢复状态

curl http://xxx.xx.xx.xx:9200/_snapshot/_status
  • 适用范围

当遇到数据量大的时候,会表现非常不错,但是需要重启集群,若线上会影响其它服务

Reindex

reindex 是 ES 提供的一个 api 接口,可以把数据从源 ES 集群导入到当前 ES 集群,实现数据的迁移。但仅限于 ES 的实现方式。还可以帮我们把数据从一个index到另外一个index进行重新reindex。这个对于特别适用于我们在修改我们数据的mapping后,需要重新把数据从现有的index转到新的index建立新的索引,这是因为我们不能修改现有的index的mapping一旦已经定下来了。

Elasticsearch数据迁移方案

包括ES5.x升级到7.x,这里我写了一个小小的Python脚本,大家有用到的可以关注私信我,我一一私发给大家,共同学习。

  • 使用
  1. 配置 reindex.remote.whitelist 参数
    需要在目标 ES 集群中配置该参数,指明能够 reindex 的远程集群的白名单。
  2. 调用 reindex api
    以下操作表示从源 ES 集群中查询到名为 test1 的索引,查询条件为 title 字段为 elasticsearch,将结果写入当前集 群的 test2 索引。
POST _reindex {   "source": {     "remote": {       "host": "http://xxx.xx.xx.xxx:9200"      },     "index": "test1",      "query": {       "match": {         "title": "test-allen"        }     }   },   "dest": {   	"index": "test2"   } }
  • 适用范围

同局域网内,缺点是需要修改ES配置重启集群

总结

1. elasticsearch-dump 和 logstash 做跨集群数据迁移时,都要求用于执行迁移任务的机器可以同时访问到两个集群,因为网络无法连通的情况下无法实现迁移。而使用 snapshot 的方式则没有这个限制,因为 snapshot 方式 是完全离线的。因此 elasticsearch-dump 和 logstash 迁移方式更适合于源 ES 集群和目标 ES 集群处于同一网 络的情况下进行迁移。而需要跨云厂商的迁移,可以选择使用 snapshot 的方式进行迁移,例如从阿里云 ES 集群 迁移至腾讯云 ES 集群,也可以通过打通网络实现集群互通,但是成本较高。

2. elasticsearchdump 工具和 MySQL 数据库是用于做数据备份的工具 mysqldump 类似,都是逻辑备份,需要将数据一条一条导出后再执行导入,所以适合数据量小的场景下进行迁移。

3. snapshot 这种方式适合数据量大的场景下进行迁移。