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

你可能感兴趣的文章
MS SQL查询库、表、列数据结构信息汇总
查看>>
MS UC 2013-0-Prepare Tool
查看>>
MSBuild 教程(2)
查看>>
msbuild发布web应用程序
查看>>
MSB与LSB
查看>>
MSCRM调用外部JS文件
查看>>
MSCRM调用外部JS文件
查看>>
MSEdgeDriver (Chromium) 不适用于版本 >= 79.0.313 (Canary)
查看>>
MsEdgeTTS开源项目使用教程
查看>>
msf
查看>>
MSP430F149学习之路——SPI
查看>>
msp430入门编程45
查看>>
MSSQL数据库查询优化(一)
查看>>
MSSQL数据库迁移到Oracle(二)
查看>>
MSSQL日期格式转换函数(使用CONVERT)
查看>>
MSTP多生成树协议(第二课)
查看>>
MSTP是什么?有哪些专有名词?
查看>>
Mstsc 远程桌面链接 And 网络映射
查看>>
Myeclipse常用快捷键
查看>>
MyEclipse更改项目名web发布名字不改问题
查看>>