本文共 1219 字,大约阅读时间需要 4 分钟。
在Elasticsearch中批量删除日志数据,可以通过两种方式实现
from elasticsearch import Elasticsearch, helpersimport datetimees_client = Elasticsearch(["127.0.0.1:9200"], timeout=20)# 创建索引es_client.indices.create(index='log_index', ignore=400)# 准备待处理数据body1 = {"func_info":"删除日志", "error_info":"id为空111", "write_date":datetime.datetime.now()}body2 = {"func_info":"删除日志", "error_info":"id为空222", "write_date":datetime.datetime.now()}# 结果数组result = [ {'index': {'_index': 'log_index', '_type': 'log_index'}}, body1, {'index': {'_index': 'log_index', '_type': 'log_index'}}, body2]# 批量插入数据es_result = es_client.bulk( index="log_index", doc_type="log_index", body=result)# 刷/indexes_client.indices.flush()使用es_client.bulk方法可以在批量插入数据时无需预先创建索引,直接操作目标index
这种方法在处理大量数据时可以减少事务 scrollbar相关的冲突和性能问题
from elasticsearch import Elasticsearchfrom elasticsearch import helpersimport datetimees_client = Elasticsearch(["127.0.0.1:9200"], timeout=20)# 创建索引es_client.indices.create(index='d_kl', ignore=400)# 准备单次操作action = { "_index": "d_kl", "_type": "d_kl", "_source": { "data": "数据" }}result = [action]# 批量处理数据helpers.bulk(es_client, result)# 刷/indexes_client.indices.flush()两种方法都能实现批量插入数据的目标,但在具体场景选择时需要考虑效率和可读性
转载地址:http://teeyk.baihongyu.com/