ES 版本: 5.2.1
步骤:
#!/usr/bin/env python
#name: recovery.py
import requests
import json
host = "http://localhost:9200/_cluster/allocation/explain"
s= requests.Session()
def reroute_shard(index,shard,node):
data = {
"commands" : [
{
"allocate_stale_primary" : {
"index" : index, "shard" : shard, "node" : node, "accept_data_loss": True
}
}
]
}
print data
url = "http://localhost:9200/_cluster/reroute"
res = s.post(url,json=data)
print res
def get_node(line):
if "UNASSIGNED" in line:
line = line.split()
index = line[0]
shard = line[1]
if line[2] != "p":
return
body = {
"index": index,
"shard": shard,
"primary": True
}
res = s.get(host, json = body)
for store in res.json().get("node_allocation_decisions"):
if store.get("store").get("allocation_id"):
node_name = store.get("node_name")
reroute_shard(index,shard,node_name)
else:
return
with open("shards", 'rb') as f:
map(get_node,f)
相关文档: