python批量从es取数据的方法(文档数超过10000)

作者:sxf_0123 时间:2022-03-23 01:21:48 

如下所示:


"""
提取文档数超过10000的数据
按照某个字段的值具有唯一性进行升序,
按照@timestamp进行降序,
第一次查询,先将10000条数据取出,
取出最后一个时间戳,
在第二次查询中,设定@timestamp小于将第一次得到的最后一个时间戳,
同时设定某个字段的值具有唯一性进行升序,
按照@timestamp进行降序,
"""

from elasticsearch import Elasticsearch
import os

write_path = "E:\\公司\\案例数据采集\\olt告警案例分析\\10000_data.txt"
es = Elasticsearch(hosts="", timeout=1500)
write_file = open(write_path, "a+")

def _first_query():
 index_ = "gather-010"
 _source = ["TWICE_BOOK_TIME", "@timestamp"]
 try:
   rs = es.search(index=index_, body={
     "size": 10000,
     "query": {
       "match_all": {}
     },
     "sort": [
       {
         "@timestamp": {
           "order": "desc"
         }
       },
       {
         "TASK_RECEIVE_ID.keyword": {
           "order": "asc"
         }
       }
     ],
     "_source": _source
   })
   return rs
 except:
   raise Exception("{0} search error".format(index_))

def _get_first_data(first_rs):
 i = 0
 if first_rs:
   for hit in first_rs['hits']['hits']:
     IptvAccount = hit['_source']['TWICE_BOOK_TIME']
     timestamp = hit['_source']['@timestamp']
     if IptvAccount is None:
       IptvAccount = ""
     write_file.write(IptvAccount + "," + timestamp + "\n")
     i += 1
     if i == 10000:
       return timestamp

def _second_query(timestamp):
 index_ = "gather-010"
 _source = ["TWICE_BOOK_TIME", "@timestamp"]
 try:
   rs = es.search(index=index_, body={
     "size": 10000,
     "query": {
       "bool": {
         "filter": {
           "range": {
             "@timestamp": {
               "lt": timestamp
             }
           }
         }
       }
     },
     "sort": [
       {
         "@timestamp": {
           "order": "desc"
         }
       },
       {
         "TASK_RECEIVE_ID.keyword": {
           "order": "asc"
         }
       }
     ],
     "_source": _source
   })
   return rs
 except:
   raise Exception("{0} search error".format(index_))

if __name__ == "__main__":
 first_rs = _first_query()
 first_timestamp = _get_first_data(first_rs)
 print(first_timestamp)
 while True:
   second_rs = _second_query(first_timestamp)
   first_timestamp = _get_first_data(second_rs)
   if first_timestamp is None:
     break
   print(first_timestamp)

来源:https://blog.csdn.net/sxf_123456/article/details/80373818

标签:python,es
0
投稿

猜你喜欢

  • python多进程实现文件下载传输功能

    2022-06-27 02:22:41
  • 理想高通滤波实现Python opencv示例

    2022-09-29 03:58:07
  • python转换字符串为摩尔斯电码的方法

    2022-01-01 10:17:44
  • 一个简单的ASP计数器代码

    2010-04-24 15:49:00
  • PHP之mysql位运算案例讲解

    2023-06-13 06:16:19
  • MySQL 通过索引优化含ORDER BY的语句

    2010-03-25 10:28:00
  • Microsoft SQL Server 安全问题

    2010-07-26 13:20:00
  • js调用flash代码

    2010-01-23 12:35:00
  • Python3获取cookie常用三种方案

    2022-10-20 02:56:19
  • 14条最佳JS代码编写技巧[译]

    2009-11-09 19:37:00
  • 用css实现透视效果

    2009-09-05 16:33:00
  • 使用Alt提升可访问性

    2009-04-04 19:22:00
  • Python提取频域特征知识点浅析

    2021-10-31 08:01:31
  • 再论Javascript下字符串连接的性能

    2010-06-26 13:13:00
  • Python tkinter实现的图片移动碰撞动画效果【附源码下载】

    2022-03-19 03:05:26
  • python将ip地址转换成整数的方法

    2022-10-17 23:40:23
  • 用Python手把手教你实现2048小游戏

    2023-02-22 23:27:57
  • 一个NumericStepper组件

    2010-01-22 15:46:00
  • Python编程mac下使用pycharm小技巧

    2022-10-21 19:23:52
  • 基于Python实现通过微信搜索功能查看谁把你删除了

    2022-07-10 00:19:37
  • asp之家 网络编程 m.aspxhome.com