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

你可能感兴趣的文章
myeclipse的新建severlet不见解决方法
查看>>
MyEclipse设置当前行背景颜色、选中单词前景色、背景色
查看>>
Mtab书签导航程序 LinkStore/getIcon SQL注入漏洞复现
查看>>
myeclipse配置springmvc教程
查看>>
MyEclipse配置SVN
查看>>
MTCNN 人脸检测
查看>>
MyEcplise中SpringBoot怎样定制启动banner?
查看>>
MyPython
查看>>
MTD技术介绍
查看>>
MySQL
查看>>
MySQL
查看>>
mysql
查看>>
MTK Android 如何获取系统权限
查看>>
MySQL - 4种基本索引、聚簇索引和非聚索引、索引失效情况、SQL 优化
查看>>
MySQL - ERROR 1406
查看>>
mysql - 视图
查看>>
MySQL - 解读MySQL事务与锁机制
查看>>
MTTR、MTBF、MTTF的大白话理解
查看>>
mt_rand
查看>>
mysql -存储过程
查看>>