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