日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > python >内容正文

python

python编写es脚本_es数据迁移脚本(python)

發布時間:2025/4/5 python 26 豆豆
生活随笔 收集整理的這篇文章主要介紹了 python编写es脚本_es数据迁移脚本(python) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

#!/usr/bin/python#-*- coding: UTF-8 -*-#文件名:indiceCreate.py

importsysimportbase64importtimeimporthttplibimportjson## 老集群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=0def 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()returnresdef 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 inindicesList:if (indices.find("open") >0):

indexList.append(indices.split()[2])returnindexListdef 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)returnnewSettingdef 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\" :" +mappingsreturnnewMappingdefcreateIndexStatement(oldIndexName):

settingStr=getSettings(oldIndexName, oldClusterHost, oldClusterUserName, oldClusterPassword)

mappingStr=getMapping(oldIndexName, oldClusterHost, oldClusterUserName, oldClusterPassword)

createstatement= "{\n" + str(settingStr) + ",\n" + str(mappingStr) + "\n}"

returncreatestatementdef 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 inindexList:if (index.startswith(".")):

systemIndex.append(index)else:

createIndex(index, index)if (len(systemIndex) >0) :for index insystemIndex:print index + "或許是系統索引,不會重新創建,如有需要,請單獨處理~"

總結

以上是生活随笔為你收集整理的python编写es脚本_es数据迁移脚本(python)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。