python批量從es取數(shù)據(jù)的方法(文檔數(shù)超過10000)
更新時間:2018年12月27日 14:31:23 作者:sxf_0123
今天小編就為大家分享一篇python批量從es取數(shù)據(jù)的方法(文檔數(shù)超過10000),具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
如下所示:
"""
提取文檔數(shù)超過10000的數(shù)據(jù)
按照某個字段的值具有唯一性進行升序,
按照@timestamp進行降序,
第一次查詢,先將10000條數(shù)據(jù)取出,
取出最后一個時間戳,
在第二次查詢中,設(shè)定@timestamp小于將第一次得到的最后一個時間戳,
同時設(shè)定某個字段的值具有唯一性進行升序,
按照@timestamp進行降序,
"""
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)
以上這篇python批量從es取數(shù)據(jù)的方法(文檔數(shù)超過10000)就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
Python計算標準差之numpy.std和torch.std的區(qū)別
Torch自稱為神經(jīng)網(wǎng)絡(luò)中的numpy,它會將torch產(chǎn)生的tensor放在GPU中加速運算,就像numpy會把array放在CPU中加速運算,下面這篇文章主要給大家介紹了關(guān)于Python?Numpy計算標準差之numpy.std和torch.std區(qū)別的相關(guān)資料,需要的朋友可以參考下2022-08-08
Python連接Mysql實現(xiàn)圖書借閱系統(tǒng)
這篇文章主要為大家詳細介紹了Python連接Mysql實現(xiàn)圖書借閱系統(tǒng),文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2022-03-03
學會Python數(shù)據(jù)可視化必須嘗試這7個庫
數(shù)據(jù)可視化是使用一些繪圖和圖形更詳細地理解數(shù)據(jù)的過程.最著名的庫之一是 matplotlib,它可以繪制幾乎所有您可以想象的繪圖類型.matplotlib 唯一的問題是初學者很難掌握.在本文中,我將介紹七個數(shù)據(jù)可視化庫,你可以嘗試使用它們來代替 matplotlib ,需要的朋友可以參考下2021-06-06
Pandas剔除混合數(shù)據(jù)中非數(shù)字的數(shù)據(jù)操作
這篇文章主要介紹了Pandas剔除混合數(shù)據(jù)中非數(shù)字的數(shù)據(jù)操作,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2021-03-03
Python操作MySQL數(shù)據(jù)庫9個實用實例
這篇文章主要介紹了Python操作MySQL數(shù)據(jù)庫9個實用實例,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2015-12-12
淺談Python實現(xiàn)opencv之圖片色素的數(shù)值運算和邏輯運算
今天帶大家來學習的是關(guān)于Python的相關(guān)知識,文章圍繞著圖片色素的數(shù)值運算和邏輯運算展開,文中有非常詳細的的介紹及代碼示例,需要的朋友可以參考下2021-06-06

