博客
关于我
elasticsearch的helpers.bulk和es_client.bulk的用法
阅读量:787 次
发布时间: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/

你可能感兴趣的文章
15种下载文件的方法&文件下载方法汇总&超大文件下载
查看>>
anaconda、python卸载后重装以及anaconda--443
查看>>
AWVS工具太顶了,漏洞扫描工具AWVS介绍及安装教程
查看>>
CentOS 系列:CentOS 7 使用 virt-install + vnc 图形界面/非图形界面 创建虚拟机
查看>>
CentOS 系列:CentOS 7文件系统的组成
查看>>
CentOS系列:【Linux】CentOS7操作系统安装nginx实战(多种方法,超详细)
查看>>
CSDN----Markdown编辑器
查看>>
Docker部署postgresql-11以及主从配置
查看>>
EnvironmentNotWritableError: The current user does not have write permissions to the target environm
查看>>
Golang起步篇(Windows、Linux、mac三种系统安装配置go环境以及IDE推荐以及入门语法详细释义)
查看>>
Hyper-V系列:微软官方文章
查看>>
Java系列:【注释模板】IDEA中JAVA类、方法注释模板教程
查看>>
Kali 更换源(超详细,附国内优质镜像源地址)
查看>>
kali安装docker(亲测有效)
查看>>
Linux系列:Linux目录分析:[/] + [/usr] + [/usr/local] + [/usr/local/app-name]、Linux最全环境配置 + 动态库/静态库配置
查看>>
Linux系列:ubuntu各版本之间的区别以及Ubuntu、kubuntu、xUbuntu、lubuntu等版本区别及界面样式
查看>>
mysql系列:远程连接MySQL错误“plugin caching_sha2_password could not be loaded”的解决办法
查看>>
Nessus扫描结果出现在TE.IO或者ES容器结果查看问题解决方案
查看>>
Nmap渗透测试指南之探索网络
查看>>
Nmap渗透测试指南之防火墙/IDS逃逸、信息搜集
查看>>