不要急,总有办法的

将ES索引从一个集群迁移到另一个集群的python脚本

Elasticsearch | 作者 | 发布于2021年08月25日 | | 阅读数:1555

将索引从一个集群(ip:192.168.0.1)迁移到另一个集群(ip:192.168.0.2),新集群需要配置白名单:

# elasticsearch.yml
reindex.remote.whitelist: ['192.168.0.1:9200']
import json

import requests

import base64

# 迁移索引数据
def reindex(index):
    print("{0} 索引正在迁移中".format(index))
    jsonData = {
        "source": {
            "remote": {
                "host": "http://192.168.0.1:9200",
                "socket_timeout": "30s",
                "connect_timeout": "30s",
                "username": "elastic",
                "password": "123456"
            },
            "index": index
        },
        "dest": {
            "index": index
        }
    }
    res = requests.post("http://192.168.0.2:9200/_reindex", json=jsonData)
    print(res.status_code)
    print(res.content)

# 获取创建索引的语句
def getIndexDslJson(index):

    username = "elastic"
    password = "123456"
    user_info_str = username + ":" + password
    user_info = base64.b64encode(user_info_str.encode())  # 这个得到是个字节类型的数据
    headers = {
        "Authorization": "Basic {0}".format(user_info.decode())  # 这个就是需要验证的信息
    }

    res = requests.get("http://192.168.0.1:9200/{0}".format(index),
                       headers=headers)
    print(res.status_code)
    jsonIndex = json.loads(res.content)
    print(jsonIndex[index])
    del jsonIndex[index]['settings']['index']['creation_date']
    del jsonIndex[index]['settings']['index']['provided_name']
    del jsonIndex[index]['settings']['index']['uuid']
    del jsonIndex[index]['settings']['index']['version']
    jsonIndex[index]['settings']['index']['number_of_shards'] = 1
    jsonIndex[index]['settings']['index']['number_of_replicas'] = 0
    return jsonIndex[index]

# 创建索引
def createIndex(index, dslJson):
    res = requests.put("http://192.168.0.2:9200/{0}".format(index), json=dslJson)
    print(res.status_code)
    print(res.content)

if __name__ == '__main__':
    # 需要创建的索引
    indexList = [
        "index1_v1", "index2_v1"
    ]
    for index in indexList:
        dslJson = getIndexDslJson(index) # 获取原索引的结构
        createIndex(index, dslJson) # 根据原索引结构,在新集群创建索引
        reindex(index) # 将原集群索引迁移到新集群

[尊重社区原创,转载请保留或注明出处]
本文地址:http://elasticsearch.cn/article/14372


2 个评论

tell me why, curl 可以直接执行, 为啥你用python
因为我用python,能很方便的将很多索引的结构及其数据迁过来,不用单个去执行curl请求呀

要回复文章请先登录注册