本文介绍通过reindex方式,将ECS内自建的Elasticsearch中的数据迁移至阿里云Elasticsearch(简称ES)中,包括创建索引和迁移数据。
注意事项
自建ES使用reindex迁移数据到阿里云ES,仅支持单可用区的阿里云ES实例。如果您使用的是多可用区实例,建议使用如下方案:
如果源端数据量比较大,建议使用OSS快照方式。
如果需要对源端数据进行过滤,建议使用Losgatsh迁移方案。
前提条件
自建ES所在的ECS的网络类型必须是专有网络VPC(Virtual Private Cloud),不支持Classiclink方式打通的ECS,且自建ES必须与阿里云ES在同一个VPC下。
自建ES所在的ECS的安全组不能限制阿里云ES实例的各节点IP(Kibana控制台可查看各节点的IP),且要开启9200端口。
自建ES与阿里云ES实例已经连通。可在执行脚本的机器上使用curl -XGET http://:9200进行验证。
说明 您可以通过任意一台机器执行文档中的脚本,前提是该机器可以同时访问自建ES与阿里云ES集群的9200端口。
创建索引
参考自建ES集群中需要迁移的索引配置,提前在阿里云ES集群中创建索引。或者为阿里云ES集群开启自动创建索引功能(不建议)。
以Python为例,使用如下脚本在阿里云ES集群中批量创建自建ES集群中需要迁移的索引,默认新创建的索引副本数为0。
#!/usr/bin/python
# -*- coding: UTF-8 -*-
# 文件名:indiceCreate.py
import sys
import base64
import time
import httplib
import json
## 旧集群host
oldClusterHost = "old-cluster.com"
## 旧集群用户名,可为空
oldClusterUserName = "old-username"
## 旧集群密码,可为空
oldClusterPassword = "old-password"
## 新集群host
newClusterHost = "new-cluster.com"
## 新集群用户名,可为空
newClusterUser = "new-username"
## 新集群密码,可为空
newClusterPassword = "new-password"
DEFAULT_REPLICAS = 0
def httpRequest(method, host, endpoint, params="", username="", password=""):
conn = httplib.HTTPConnection(host)
headers = {}
if (username != "") :
'Hello {name}, your age is {age} !'.format(name = 'Tom', age = '20')
base64string = base64.encodestring('{username}:{password}'.format(username = username, password = password)).replace('\n', '')
headers["Authorization"] = "Basic %s" % base64string;
if "GET" == method:
headers["Content-Type"] = "application/x-www-form-urlencoded"
conn.request(method=method, url=endpoint, headers=headers)
else :
headers["Content-Type"] = "application/json"
conn.request(method=method, url=endpoint, body=params, headers=headers)
response = conn.getresponse()
res = response.read()
return res
def httpGet(host, endpoint, username="", password=""):
return httpRequest("GET", host, endpoint, "", username, password)
def httpPost(host, endpoint, params, username="", password=""):
return httpRequest("POST", host, endpoint, params, username, password)
def httpPut(hos