环境:python3.7
注意elasticsearch包要跟es环境版本号对应上
from elasticsearch import Elasticsearch as es
import xlwt
instance = es(hosts='192.168.21.120')
query = {
"query": {
"bool": {
"must": [
{"wildcard": {"eventLevel": "error"}},
{"wildcard": {"@message": "*exception*"}}
]
}
}
}
query_all = {
"query":{
"match_all":{}
}
}
page = instance.search(index='test',doc_type='log',from_=0,body=query,scroll='2m',size=10000)
firstdata = page['hits']['hits']
alldata = [i['_source'] for i in firstdata]
sid = page['_scroll_id']
scroll_size = page['hits']['total']
print('共计命中{}行'.format(scroll_size))
print('scroll_size:{}'.format(len(alldata)))
while scroll_size>0:
page = instance.scroll(scroll_id=sid,scroll='2m')
sid = page['_scroll_id']
scroll_size = len(page['hits']['hits'])
print('scroll_size:{}'.format(scroll_size))
data = [i['_source'] for i in page['hits']['hits']]
alldata = alldata + data
import os
os.chdir(os.path.dirname(__file__))
'''
wk = xlwt.Workbook(encoding = 'utf-8')
st = wk.add_sheet('data')
'''
import openpyxl
wb = openpyxl.Workbook()
ws = wb.create_sheet("Mysheet")
columns = list(alldata[0].keys())
for line_num in range(len(alldata)):
line_data = alldata[line_num]
for k,v in enumerate(line_data.values()):
ws.cell(row=line_num+1,column=k+1,value=v)
wb.save('result.xlsx')