注意 py 代码要和 mapping 目录在同一级,mapping 目录下存放索引库的 json 文件,文件名为index_name.json
import requests
import json
import os
mapping_folder = "./mapping"
headers = {"Content-Type":"application/json","Accept":"application/json"}
is_append = True
## 新集群host
newClusterHost = ""
## 新集群用户名,可为空
newClusterUser = ""
## 新集群密码,可为空
newClusterPassword = ""
def syn_index(index_name, data):
# 索引存在则放弃同步
if exist_index(index_name):
return
print("syn_index-start:{}".format(index_name))
syn_index_res = requests.put("http://" + newClusterHost + ":9200/" + index_name,
auth=(newClusterUser, newClusterPassword),
headers={"content-type": "application/json"},
data=data)
if syn_index_res.status_code != 200:
print("syn_index-err:{}".format(syn_index_res.text))
else:
print("syn_index-success:{}".format(index_name))
def exist_index(index_name):
exist_index_res = requests.get("http://" + newClusterHost + ":9200/" + index_name, auth=(newClusterUser, newClusterPassword))
return exist_index_res.status_code == 200
def select_mapping():
print("\r\n>> select_mapping running \r\n")
mapping_list = os.listdir(mapping_folder)
# 存放 key: index_name value:mapping
mapping_dic = {}
for file_name in mapping_list:
file_path = "{}/{}".format(mapping_folder, file_name)
with open(file_path, encoding='utf-8') as f:
if f:
mapping = json.loads(f.read().strip())
data = json.dumps(mapping, indent=4, sort_keys=True)
mapping_dic[file_name.split('.')[0]] = data
return mapping_dic
def main():
# 获取 mappings 字典 key 为 index_name
mapping = select_mapping()
# 创建索引库
for k, v in mapping.items():
syn_index(index_name=k, data=v)
print("Done for syn indies")
if __name__ == "__main__":
main()
{
"aliases" : {
"user_at_index : { }
},
"settings" : {
"index" : {
"number_of_shards" : 3,
"number_of_replicas" : 0
}
},
"mappings" : {
"_meta": {
"software_version_mapping": "1.0",
"description": "作品艾特信息表"
},
"properties" : {
"at_time" : {
"type" : "keyword"
},
"at_time_long" : {
"type" : "long"
},
"at_user_id" : {
"type" : "long"
},
"be_at_user_id" : {
"type" : "long"
},
"del_flag" : {
"type" : "byte"
},
"id" : {
"type" : "long"
},
"update_time" : {
"type" : "keyword"
}
}
}
}