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 base64 decode incorrect padding错误解决方法
Jan 08 Python
Python 列表(List) 的三种遍历方法实例 详解
Apr 15 Python
Python探索之pLSA实现代码
Oct 25 Python
浅谈Python NLP入门教程
Dec 25 Python
Python使用pickle模块存储数据报错解决示例代码
Jan 26 Python
对python中的logger模块全面讲解
Apr 28 Python
python检测文件夹变化,并拷贝有更新的文件到对应目录的方法
Oct 17 Python
numpy和pandas中数组的合并、拉直和重塑实例
Jun 28 Python
Pycharm连接远程服务器并实现远程调试的实现
Aug 02 Python
基于CentOS搭建Python Django环境过程解析
Aug 24 Python
PyCharm Community安装与配置的详细教程
Nov 24 Python
python中的时区问题
Jan 14 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
PHP5在Apache下的两种模式的安装
2006/09/05 PHP
windows xp下安装pear
2006/12/02 PHP
php实现socket推送技术的示例
2017/12/20 PHP
Laravel框架实现文件上传的方法分析
2019/09/29 PHP
js switch case default 的用法示例介绍
2013/10/23 Javascript
JavaScript实现常用二级省市级联下拉列表的方法
2015/03/25 Javascript
跟我学习javascript的异步脚本加载
2015/11/20 Javascript
图解js图片轮播效果
2015/12/20 Javascript
全面解析Bootstrap中Carousel轮播的使用方法
2016/06/13 Javascript
jquery tmpl模板(实例讲解)
2017/09/02 jQuery
JS从非数组对象转数组的方法小结
2018/03/26 Javascript
vue和webpack打包项目相对路径修改的方法
2018/06/15 Javascript
angularjs结合html5实现拖拽功能
2018/06/25 Javascript
Vue+elementUI实现多图片上传与回显功能(含回显后继续上传或删除)
2020/03/23 Javascript
[02:35]DOTA2英雄基础教程 末日使者
2013/12/04 DOTA
python关键字and和or用法实例
2015/05/28 Python
解决pyecharts在jupyter notebook中使用报错问题
2020/04/23 Python
详细介绍pandas的DataFrame的append方法使用
2019/07/31 Python
浅谈keras的深度模型训练过程及结果记录方式
2020/01/24 Python
HTML5实现视频直播功能思路详解
2017/11/16 HTML / CSS
HTML5拖拽API经典实例详解
2018/04/20 HTML / CSS
浅谈HTML5新增和废弃的标签
2019/04/28 HTML / CSS
Nike英国官网:Nike.com (UK)
2017/02/13 全球购物
英文自荐信
2013/12/19 职场文书
大学毕业感言
2014/01/10 职场文书
家长对孩子评语
2014/01/30 职场文书
安全协议书
2014/04/23 职场文书
私人委托书格式
2014/09/10 职场文书
2014年学习厉行节约反对浪费思想汇报
2014/09/10 职场文书
学习党的群众路线剖析材料
2014/10/09 职场文书
中班教师个人总结
2015/02/05 职场文书
道歉短信大全
2015/05/12 职场文书
看看如何用Python绘制小米新版天价logo
2021/04/20 Python
python实现自定义日志的具体方法
2021/05/28 Python
Pyhton爬虫知识之正则表达式详解
2022/04/01 Python
JavaScript中reduce()的用法
2022/05/11 Javascript