批量写入:
# https://www.cnblogs.com/Neeo/articles/10788573.html可以看这个博客
import time
from elasticsearch import Elasticsearch
from elasticsearch import helpers
es = Elasticsearch(["127.0.0.1:9200"])
def timer(func):
def wrapper(*args, **kwargs):
start = time.time()
res = func(*args, **kwargs)
print('共耗时约 {:.2f} 秒'.format(time.time() - start))
return res
return wrapper
@timer
def gen():
""" 使用生成器批量写入数据 """
with open("./word.txt", "r", encoding="utf-8") as f:
actions = ({
"_index": "word",
"_type": "doc",
"_source": { # 这个字段中写你需要输入的数据,因为是一个字典所以可以写入多个字段
"word": line.strip()
}
} for line in f) # 建议这里使用生成器,不要直接使用列表,没有写过大数据的可以试一下,如果是一个列表的话,这个时候你查看任务管理器,会发现内存被撑爆了,程序直接停止运行,同时电脑卡主,重启吧。
helpers.bulk(es, actions=actions)
gen()
最后我这里写入了21万的数据,共耗时约 16.82 秒