忘机山人
Elasticsearch 迁移是将数据、索引和配置从一个 Elasticsearch 集群转移到另一个集群的过程。以下是几种常见的迁移方法:
这是最推荐的迁移方法,适用于大型数据集。
步骤:
在源集群上创建共享文件系统仓库
PUT /_snapshot/my_backup
{
"type": "fs",
"settings": {
"location": "/mnt/backups/my_backup"
}
}
创建快照
PUT /_snapshot/my_backup/snapshot_1?wait_for_completion=true
{
"indices": "*",
"ignore_unavailable": true,
"include_global_state": false
}
将备份文件复制到目标集群可访问的位置
在目标集群上注册相同的仓库
从快照恢复
POST /_snapshot/my_backup/snapshot_1/_restore
适用于小规模数据或需要转换数据的迁移。
步骤:
POST _reindex
{
"source": {
"remote": {
"host": "http://source-cluster:9200"
},
"index": "source_index"
},
"dest": {
"index": "dest_index"
}
}
使用 Logstash 作为数据管道进行迁移。
示例配置:
input {
elasticsearch {
hosts => ["http://source-cluster:9200"]
index => "source_index"
}
}
output {
elasticsearch {
hosts => ["http://target-cluster:9200"]
index => "target_index"
}
}
适用于小型索引的迁移。
安装:
npm install elasticdump -g
使用:
# 导出映射
elasticdump \
--input=http://source:9200/my_index \
--output=my_index_mapping.json \
--type=mapping
# 导出数据
elasticdump \
--input=http://source:9200/my_index \
--output=my_index_data.json \
--type=data
# 导入到目标集群
elasticdump \
--input=my_index_mapping.json \
--output=http://target:9200/my_index \
--type=mapping
elasticdump \
--input=my_index_data.json \
--output=http://target:9200/my_index \
--type=data
选择哪种方法取决于您的具体需求、数据量、Elasticsearch 版本和可用资源。
除了之前提到的方法,这里还有几种额外的 Elasticsearch 迁移方案,适用于不同场景:
适用场景:需要持续同步的迁移或零停机时间迁移
要求:
步骤:
在源集群(leader)上启用 CCR:
PUT /_cluster/settings
{
"persistent": {
"cluster.remote.remote_cluster.seeds": ["<target_cluster_transport_address>:9300"]
}
}
在目标集群(follower)上创建跟随索引:
POST /<index_name>/_ccr/follow
{
"remote_cluster": "remote_cluster",
"leader_index": "<index_name>"
}
当数据同步完成后,可以停止复制关系
对于特殊需求,可以开发自定义迁移工具:
基于Scroll API的批量导出:
from elasticsearch import Elasticsearch, helpers
es_source = Elasticsearch(['source_host'])
es_target = Elasticsearch(['target_host'])
query = {"query": {"match_all": {}}}
scroll_size = 1000
docs = helpers.scan(es_source, index="source_index", query=query, size=scroll_size)
helpers.bulk(es_target, docs, index="target_index")

评论
0暂无评论