博客
关于我
elasticsearch的helpers.bulk和es_client.bulk的用法
阅读量:798 次
发布时间:2023-01-24

本文共 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/

你可能感兴趣的文章
MySQL 高性能优化规范建议
查看>>
mysql 默认事务隔离级别下锁分析
查看>>
Mysql--逻辑架构
查看>>
MySql-2019-4-21-复习
查看>>
mysql-5.6.17-win32免安装版配置
查看>>
mysql-5.7.18安装
查看>>
MySQL-Buffer的应用
查看>>
mysql-cluster 安装篇(1)---简介
查看>>
mysql-connector-java.jar乱码,最新版mysql-connector-java-8.0.15.jar,如何愉快的进行JDBC操作...
查看>>
mysql-connector-java各种版本下载地址
查看>>
mysql-EXPLAIN
查看>>
MySQL-Explain的详解
查看>>
mysql-group_concat
查看>>
MySQL-redo日志
查看>>
MySQL-【1】配置
查看>>
MySQL-【4】基本操作
查看>>
Mysql-丢失更新
查看>>
Mysql-事务阻塞
查看>>
Mysql-存储引擎
查看>>
mysql-开启慢查询&所有操作记录日志
查看>>