클라우드 & NoSQL/Elastic Stack(ELK)
Get Query All Elasticsearch Data(Elasticsearch 전체 데이터 가져오기)
술취한둘리
2020. 9. 2. 11:22
반응형
기본적으로 elasticsearch query는 한번의 query로 최대 10000개의 데이터만 가져올 수 있다.
하지만 그 이상의 데이터가 필요로 할때 모든 데이터를 가져올 수 있는 방법이다.
코드는 python 버전이다.
from elasticsearch import Elasticsearch
_KEEP_ALIVE_LIMIT='20s'
es = Elasticsearch([{'host': "127.0.0.1", 'port': 9200}])
body = {
"sort": [
{
"@timestamp": {
"order": "desc"
}
}
],
"query": {
"match_all": {}
}
}
response = es.search(index = 'test_index',
scroll = _KEEP_ALIVE_LIMIT,
size = 100,
body = body)
sid = response['_scroll_id']
fetched = len(response['hits']['hits'])
datatime_list = []
for i in range(fetched):
datatime_list.append(response['hits']['hits'][i]['_source']['@timestamp'])
while(fetched>0):
response = es.scroll(scroll_id=sid, scroll=_KEEP_ALIVE_LIMIT)
fetched = len(response['hits']['hits'])
for i in range(fetched):
datatime_list.append(response['hits']['hits'][i]['_source']['@timestamp'])
print (datatime_list)
반응형