博客
关于我
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/

你可能感兴趣的文章
msp430入门编程45
查看>>
MSSQL数据库查询优化(一)
查看>>
MSSQL数据库迁移到Oracle(二)
查看>>
MSSQL日期格式转换函数(使用CONVERT)
查看>>
MSTP多生成树协议(第二课)
查看>>
MSTP是什么?有哪些专有名词?
查看>>
Mstsc 远程桌面链接 And 网络映射
查看>>
Myeclipse常用快捷键
查看>>
MyEclipse更改项目名web发布名字不改问题
查看>>
MyEclipse用(JDBC)连接SQL出现的问题~
查看>>
mt-datetime-picker type="date" 时间格式 bug
查看>>
myeclipse的新建severlet不见解决方法
查看>>
MyEclipse设置当前行背景颜色、选中单词前景色、背景色
查看>>
Mtab书签导航程序 LinkStore/getIcon SQL注入漏洞复现
查看>>
myeclipse配置springmvc教程
查看>>
MyEclipse配置SVN
查看>>
MTCNN 人脸检测
查看>>
MyEcplise中SpringBoot怎样定制启动banner?
查看>>
MyPython
查看>>
MTD技术介绍
查看>>