欧美成人午夜免费全部完,亚洲午夜福利精品久久,а√最新版在线天堂,另类亚洲综合区图片小说区,亚洲欧美日韩精品色xxx

扣丁學(xué)堂Python培訓(xùn)簡述Python批量從es取數(shù)據(jù)的方法

2019-09-20 10:02:20 5141瀏覽

本篇文章扣丁學(xué)堂Python培訓(xùn)小編給大家分享一篇Python批量從es取數(shù)據(jù)的方法,文章具有不錯的參考價值,對Python開發(fā)技術(shù)感興趣的小伙伴就隨小編來了解一下吧。


扣丁學(xué)堂Python培訓(xùn)簡述Python批量從es取數(shù)據(jù)的方法


如下所示:


"""
提取文檔數(shù)超過10000的數(shù)據(jù)
按照某個字段的值具有唯一性進(jìn)行升序,
按照@timestamp進(jìn)行降序,
第一次查詢,先將10000條數(shù)據(jù)取出,
取出最后一個時間戳,
在第二次查詢中,設(shè)定@timestamp小于將第一次得到的最后一個時間戳,
同時設(shè)定某個字段的值具有唯一性進(jìn)行升序,
按照@timestamp進(jìn)行降序,
"""

from elasticsearch import Elasticsearch
import os

write_path = "E:\\公司\\案例數(shù)據(jù)采集\\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)

想要了解更多關(guān)于Python和人工智能方面內(nèi)容的小伙伴,請關(guān)注扣丁學(xué)堂Python培訓(xùn)官網(wǎng)、微信等平臺,扣丁學(xué)堂IT職業(yè)在線學(xué)習(xí)教育平臺為您提供權(quán)威的Python開發(fā)環(huán)境搭建視頻,Python培訓(xùn)后的前景無限,行業(yè)薪資和未來的發(fā)展會越來越好的,扣丁學(xué)堂老師精心推出的Python視頻教程定能讓你快速掌握Python從入門到精通開發(fā)實(shí)戰(zhàn)技能??鄱W(xué)堂Python技術(shù)交流群:279521237。


扣丁學(xué)堂微信公眾號                          Python全棧開發(fā)爬蟲人工智能機(jī)器學(xué)習(xí)數(shù)據(jù)分析免費(fèi)公開課直播間


      【關(guān)注微信公眾號獲取更多學(xué)習(xí)資料】         【掃碼進(jìn)入Python全棧開發(fā)免費(fèi)公開課】



查看更多關(guān)于"Python開發(fā)資訊"的相關(guān)文章>

標(biāo)簽: Python培訓(xùn) Python視頻教程 Python在線視頻 Python學(xué)習(xí)視頻 Python培訓(xùn)班

熱門專區(qū)

暫無熱門資訊

課程推薦

微信
微博
15311698296

全國免費(fèi)咨詢熱線

郵箱:codingke@1000phone.com

官方群:148715490

北京千鋒互聯(lián)科技有限公司版權(quán)所有   北京市海淀區(qū)寶盛北里西區(qū)28號中關(guān)村智誠科創(chuàng)大廈4層
京ICP備2021002079號-2   Copyright ? 2017 - 2022
返回頂部 返回頂部