Python操作ES,读取大量数据,写入到JSON文件中
# -*- coding: utf-8 -*-
import json
from elasticsearch import Elasticsearch
HOST_PORT = [{"host": "xxx.xx.xx.xx", "port": 9200}] # 连接ES的主机IP和端口号
TIME_OUT = 20 # 设置请求的超时时间(秒)
INDEX_NAME = "test_2019.11.28" # 连接的ES的Index名称
#INDEX_NAME = "test_*" # 连接的ES的index名称,匹配所有以test_开头的所有Index
es = Elasticsearch(HOST_PORT, timeout=TIME_OUT)
json_file_name = "test" # 生成的JSON文件名称
def func_name() :
# body 为你的ES语句
body = {
"query": {
...
},
"aggs":{
...
},
"size": 10000 # 每次读取10000条数据写入
}
try :
res = es.search(index=INDEX_NAME, body=body, scroll="2m")
sid = res[&#