批量向ElasticSearch添加数据:
import datetime
import time
from elasticsearch import Elasticsearch
from elasticsearch.helpers import parallel_bulk
import random
# 创建 Elasticsearch 客户端
es = Elasticsearch([{'host': 'localhost', 'port': 9200, 'scheme': 'http'}], http_auth=('username', 'password'))
start_current_time = datetime.datetime.now()
start_time = time.time()
# 准备要插入的数据
data = {
"young": 0,
"teenager": 0,
"mask": 0,
"activeDirection": 0,
}
# 定义插入函数
def generate_actions(data):
for _ in range(50000):
doc = data.copy()
for key in doc:
doc[key] = random.choice([0, 1])
action = {
"_index": "testtest01", # 替换为你的索引名称
"_source": doc
}
yield action
# 执行并行批量插入
for success, info in parallel_bulk(es, generate_actions(data), thread_count=4, chunk_size=1000):
if not success:
print('A document failed:', info)
end_time = time.time()
total_time = end_time - start_time
end_current_time = datetime.datetime.now()
print(f"开始时间为:{start_current_time},结束时间是:{end_current_time},时长为:{total_time}")
删除ES数据:
# 删除某个索引
delete_index = "test03"
if es.indices.exists(index=delete_index):
es.indices.delete(index=delete_index)
print(f"The index {delete_index} has been deleted.")
else:
print(f"The index {delete_index} does not exist.")
# 删除所有test开头的索引
# 获取所有以 'index' 开头的索引名称
indices = es.indices.get('*test*').keys()
# 删除所有以 'index' 开头的索引
for index in indices:
if index.startswith('test'):
es.indices.delete(index=index)
print('All indices starting with "index" have been deleted.')