#!/usr/bin/python
# -*- coding: UTF-8 -*-
# 文件名:indiceCreate.py
import sys
import base64
import time
import httplib
import json
## 老集群host(ip+port)
oldClusterHost = "192.168.1.85:9200"
## 老集群用户名,可为空
oldClusterUserName = "elastic"
## 老集群密码,可为空
oldClusterPassword = "elastic"
## 新集群host(ip+port)
newClusterHost = "192.168.1.118:9200"
## 新集群用户名,可为空
newClusterUser = ""
## 新集群密码,可为空
newClusterPassword = ""
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 = '')
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(host, endpoint, params, username="", password=""):
return httpRequest("PUT", host, endpoint, params, username, password)
def getIndices(host, username="", password=""):
endpoint = "/_cat/indices"
indicesResult = httpGet(oldClusterHost, endpoint, oldClusterUserName, oldClusterPassword)
indicesList = indicesResult.split("\n")
indexList = []
for indices in indicesList:
if (indices.find("open") > 0):
indexList.append(indices.split()[2])
return indexList
def getSettings(index, host, username="", password=""):
endpoint = "/" + index + "/_settings"
indexSettings = httpGet(host, endpoint, username, password)
print index + " 原始settings如下:\n" + indexSettings
settingsDict = json.loads(indexSettings)
## 分片数默认和老集群索引保持一致
number_of_shards = settingsDict[index]["settings"]["index"]["number_of_shards"]
## 副本数默认为0
number_of_replicas = DEFAULT_REPLICAS
newSetting = "\"settings\": {\"number_of_shards\": %s, \"number_of_replicas\": %s}" % (number_of_shards, number_of_replicas)
return newSetting
def getMapping(index, host, username="", password=""):
endpoint = "/" + index + "/_mapping"
indexMapping = httpGet(host, endpoint, username, password)
print index + " 原始mapping如下:\n" + indexMapping
mappingDict = json.loads(indexMapping)
mappings = json.dumps(mappingDict[index]["mappings"])
newMapping = "\"mappings\" : " + mappings
return newMapping
def createIndexStatement(oldIndexName):
settingStr = getSettings(oldIndexName, oldClusterHost, oldClusterUserName, oldClusterPassword)
mappingStr = getMapping(oldIndexName, oldClusterHost, oldClusterUserName, oldClusterPassword)
createstatement = "{\n" + str(settingStr) + ",\n" + str(mappingStr) + "\n}"
return createstatement
def createIndex(oldIndexName, newIndexName=""):
if (newIndexName == "") :
newIndexName = oldIndexName
createstatement = createIndexStatement(oldIndexName)
print "新索引 " + newIndexName + " 的setting和mapping如下:\n" + createstatement
endpoint = "/" + newIndexName
createResult = httpPut(newClusterHost, endpoint, createstatement, newClusterUser, newClusterPassword)
print "新索引 " + newIndexName + " 创建结果:" + createResult
## main
indexList = getIndices(oldClusterHost, oldClusterUserName, oldClusterPassword)
systemIndex = []
for index in indexList:
if (index.startswith(".")):
systemIndex.append(index)
else :
createIndex(index, index)
if (len(systemIndex) > 0) :
for index in systemIndex:
print index + " 或许是系统索引,不会重新创建,如有需要,请单独处理~"
以上同步的时候设置的副本数是0,目的是加快同步速度,同步完成后需要设置副本数,如下:
curl -H "Content-Type: application/json" -XPUT 'http://192.168.1.118:9200/db_customer/_settings' -d '{
   "number_of_replicas" : 1
}'
 

es数据迁移脚本(python)的更多相关文章

  1. 运维脚本-elasticsearch数据迁移python3脚本

    elasticsearch数据迁移python3脚本 #!/usr/bin/python3 #elsearch 数据迁移脚本 #迁移工具路径 import time,os #下面命令是用到了一个go语 ...

  2. ELK数据迁移,ES快照备份迁移

    通过curl命令或者kibana快照备份,恢复的方式进行数据迁移 环境介绍 之前创建的ELK 因为VPC环境的问题,需要对ELK从新部署,但是还需要保留现有的数据,于是便有了这篇文档. 10.0.20 ...

  3. Entity Framework Code First Migrations--EF 的数据迁移

    1. 为了演示方便,首先新建一个控制台项目,然后添加对entityframework的引用 使用nuget控制台执行: Install-Package EntityFramework 2.新建一个实体 ...

  4. Mycat 分片规则详解--数据迁移及节点扩容

    使用的是 Mycat 提供的 dataMigrate 脚本进行对数据进行迁移和节点扩容,目前支持的 Mycat 是1.6 版本,由于 Mycat 是由 Java 编写的因此在做数据迁移及节点扩容时需要 ...

  5. MySQL 到 ES 数据实时同步技术架构

    MySQL 到 ES 数据实时同步技术架构 我们已经讨论了数据去规范化的几种实现方式.MySQL 到 ES 数据同步本质上是数据去规范化多种实现方式中的一种,即通过"数据迁移同步" ...

  6. 使用rdb文件进行redis数据迁移--python脚本

    查找了一些redis迁移的方法,一般做法就是 1. 从源数据库把rdb文件保存,然后传到新的主机上,启动新的redis即可 2. 把新的redis当做源数据库的slave,同步数据 今天开发提了一个测 ...

  7. 【Python】Django删除数据迁移记录

    find . -path "*migrations*" -name "*.py" -not -path "*__init__*" -exec ...

  8. Python进行Redis数据迁移

    Python进行Redis数据迁移 由于开发时的误操作,导致redis数据损坏,所以需要进行redis的数据迁移,网上大佬的教程基本都是需要下载附加工具,亦或是需要一些复杂的操作,个人觉得麻烦还不如写 ...

  9. elasticsearch-dump 迁移es数据 (elasticdump)

    elasticsearch 部分查询语句 # 获取集群的节点列表: curl 'localhost:9200/_cat/nodes?v' # 列出所有索引: curl 'localhost:9200/ ...

随机推荐

  1. CentOS6.5安装sqoop2

    1.下载软件:http://archive.cloudera.com/cdh5/cdh/5/ 2.解压:tar -zxvf mysofts/sqoop2-1.99.5-cdh5.6.0.tar.gz ...

  2. sparse.coo_matrix()

    coo_matrix.tocsr(copy = False ) 将此矩阵转换为压缩稀疏行格式,重复的条目将汇总在一起. 举例: from numpy import array from scipy.s ...

  3. JMeter TCP性能测试

    jmeter是一款纯java的性能测试工具,跨平台运行方便.提供图形化界面设置.简单易用.     在性能测试方法论中,很典型的方法就是二八原则,量化业务需求. 二八原则:指80%的业务量在20%的时 ...

  4. VS 应用模板 所交税和实发工资的运算

    double SFGZ, SL, SSKCS, YFGZ,a,YJS; //应发工资(基本工资),税率,速算扣除数,应发工资,判断标准,交多少税 //double QZD = 3500;//起征点 无 ...

  5. SqlServer表和EXCEL数据互相复制方法

    一.SqlServer表数据复制到excel 1.新建查询,用sql语句把表数据读出来 2.然后,选择数据,右键,复制(也可以点击连同标题复制),复制到记事本中(不然会乱码) 3.然后再把记事本的内容 ...

  6. linux本地机上传文件到服务器

    最近工作全部切换到了linux环境下,就是吃喝拉撒全在linux下,微信,web端,qq,web端,-------,各种socket编程,网络通讯- 本地linux机从阿里云下载文件

  7. Shell Necklace (dp递推改cdq分治 + fft)

    首先读出题意,然后发现这是一道DP,我们可以获得递推式为 然后就知道,不行啊,时间复杂度为O(n2),然后又可以根据递推式看出这里面可以拆解成多项式乘法,但是即使用了fft,我们还需要做n次多项式乘法 ...

  8. arc 092C 2D Plane 2N Points

    题意: 有n个红色的点和n个蓝色的点,如果红色的点的横坐标和纵坐标分别比蓝色的点的横坐标和纵坐标小,那么这两个点就可以成为一对友好的点. 问最多可以形成多少对友好的点. 思路: 裸的二分图匹配,对于满 ...

  9. PGPDesktop在win7环境下的安装和使用

    PGPDesktop在win7环境下的安装和使用 PGP的简介 PGP(Pretty Good Privacy),是一个基于RSA公钥加密体系的邮件加密软件,它提供了非对称加密和数字签名,是目前非常流 ...

  10. vim 命令学习(基础篇)

    [1]三种模式 vi的三种模式:命令模式.末行模式.编辑模式. 三种模式相互切换逻辑与命令图: 1.命令模式是vi的默认模式(即每打开一个文件时的初始模式). 2.命令模式切换至末行模式,末行模式切换 ...