import time
import datetime
import traceback
from elasticsearch import Elasticsearch
连接
es = Elasticsearch(
['192.168.1.1:9200', '192.168.1.2:9200', '192.168.1.3:9200'],
timeout = 3600
)
创建索引
es.indices.create(
index = "newtest3",
body = {
"settings": {
"index": {
"number_of_shards": 6,
"number_of_replicas": 1
}
}
},
ignore = 400
)
批量写
while True:
try:
s1 = datetime.datetime.now()
try:
random_num = int(get_random_number())
random_string = get_random_string()
md5_string = get_md5(random_string)
es.index(
index = "newtest3",
body = {
"id": random_num,
"name": random_string,
"token": md5_string
}
)
except:
traceback.print_exc()
es = Elasticsearch(
['192.168.1.1:9200', '192.168.1.2:9200', '192.168.1.3:9200'],
timeout = 3600
)
s2 = datetime.datetime.now()
print("[%s]%ds%dms" % (str(s2), (s2 - s1).seconds, (s2 - s1).microseconds / 1000))
except:
break
批量读
while True:
try:
try:
s = es.search(index = "newtest3")
except:
traceback.print_exc()
es.close()
es = Elasticsearch(
['192.168.1.1:9200', '192.168.1.2:9200', '192.168.1.3:9200'],
timeout = 3600
)
print(get_now(), s)
time.sleep(1)
except:
break
网友评论