存储大量数据时,使用批量操作是最快的方法。在Python中,可以使用Elasticsearch的bulk API实现批量操作。以下是一个示例代码:

from elasticsearch import Elasticsearch
from elasticsearch.helpers import bulk

es = Elasticsearch()

# 将数据存储在列表中
data = [{"name": "John", "age": 30}, {"name": "Jane", "age": 25}, {"name": "Bob", "age": 40}]

# 构建批量操作的请求列表
actions = [{"_index": "my_index", "_type": "my_type", "_source": d} for d in data]

# 使用bulk API执行批量操作
success, _ = bulk(es, actions, index="my_index", raise_on_error=True)

print("Indexed %d documents" % success)

通过将数据存储在列表中,并使用批量操作API,可以大大提高数据存储的速度

使用python将数据存入ES速度最快的方法是什么

原文地址: https://www.cveoy.top/t/topic/f68K 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录