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


Posted in Python onDecember 27, 2018

如下所示:

"""
提取文档数超过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)

以上这篇python批量从es取数据的方法(文档数超过10000)就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持三水点靠木。

Python 相关文章推荐
Python使用Flask框架获取当前查询参数的方法
Mar 21 Python
讲解Python中的递归函数
Apr 27 Python
python基于xmlrpc实现二进制文件传输的方法
Jun 02 Python
使用Python来开发Markdown脚本扩展的实例分享
Mar 04 Python
Python paramiko模块的使用示例
Apr 11 Python
Python批量发送post请求的实现代码
May 05 Python
在python中用url_for构造URL的方法
Jul 25 Python
Python破解BiliBili滑块验证码的思路详解(完美避开人机识别)
Feb 17 Python
Python OpenCV中的numpy与图像类型转换操作
Dec 11 Python
python四个坐标点对图片区域最小外接矩形进行裁剪
Jun 04 Python
Python实现列表拼接和去重的三种方式
Jul 02 Python
python 使用tkinter与messagebox写界面和弹窗
Mar 20 Python
python 与服务器的共享文件夹交互方法
Dec 27 #Python
通过pykafka接收Kafka消息队列的方法
Dec 27 #Python
python利用thrift服务读取hbase数据的方法
Dec 27 #Python
python hbase读取数据发送kafka的方法
Dec 27 #Python
Python类和对象的定义与实际应用案例分析
Dec 27 #Python
在python环境下运用kafka对数据进行实时传输的方法
Dec 27 #Python
kafka-python批量发送数据的实例
Dec 27 #Python
You might like
PHP 输出缓存详解
2009/06/20 PHP
php 在文件指定行插入数据的代码
2010/05/08 PHP
php文件上传类完整实例
2016/05/14 PHP
针对thinkPHP5框架存储过程bug重写的存储过程扩展类完整实例
2018/06/16 PHP
php实现生成PDF文件的方法示例【基于FPDF类库】
2018/07/21 PHP
HTML-CSS群中单选引发的“事件”
2007/03/05 Javascript
js如何实现设计模式中的模板方法
2013/07/23 Javascript
jQuery表单获取和失去焦点输入框提示效果的实例代码
2013/08/01 Javascript
Javascript弹出窗口的各种方法总结
2013/11/11 Javascript
JS判断不能为空实例代码
2013/11/26 Javascript
jQuery数据类型小结(14个)
2016/01/08 Javascript
深入理解jquery跨域请求方法
2016/05/18 Javascript
利用select实现年月日三级联动的日期选择效果【推荐】
2016/12/13 Javascript
Jquery中attr与prop的区别详解
2017/05/27 jQuery
Element UI框架中巧用树选择器的实现
2018/12/12 Javascript
微信小程序整个页面的自动适应布局的实现
2020/07/12 Javascript
原生JavaScript实现拖动校验功能
2020/09/29 Javascript
详解python使用turtle库来画一朵花
2019/03/21 Python
Python企业编码生成系统之主程序模块设计详解
2019/07/26 Python
Django错误:TypeError at / 'bool' object is not callable解决
2019/08/16 Python
pytorch 批次遍历数据集打印数据的例子
2019/12/30 Python
tensorflow生成多个tfrecord文件实例
2020/02/17 Python
Python绘制K线图之可视化神器pyecharts的使用
2021/03/02 Python
上海天奕面试题笔试题
2015/04/19 面试题
艺术专业大学生自我评价
2013/09/22 职场文书
应届生服务员求职信
2013/10/31 职场文书
写演讲稿所需要注意的4个条件
2014/01/09 职场文书
房产转让协议书
2014/04/11 职场文书
结婚保证书范文
2014/04/29 职场文书
2014年社区重阳节活动策划方案
2014/09/16 职场文书
党的群众路线对照检查材料范文
2014/09/24 职场文书
Html5通过数据流方式播放视频的实现
2021/04/27 HTML / CSS
PHP遍历数组的6种方式总结
2021/11/17 PHP
Python机器学习应用之工业蒸汽数据分析篇详解
2022/01/18 Python
利用Python实现模拟登录知乎
2022/05/25 Python
Vue Mint UI mt-swipe的使用方式
2022/06/05 Vue.js