python3查询es数据并导出csv

python3查询es数据并导出csv

  • 代码展示

代码展示

from elasticsearch import Elasticsearch
import csv
# 获取es数据库
ES = [
    'http://192.168.1.111:9200'
]
es = Elasticsearch(ES)
'''
    查询所有数据并导出
'''
def ExportCsv():
    body = {
      # "query": {
      #   "match": {"name": "张三"},
      # }
    }
    query = es.search(index='lagou',body=body,scroll='5m',size=1000)
    # es查询出的结果第一页
    results = query['hits']['hits']
    # es查询出的结果总量
    total = query['hits']['total']["value"]
    # 游标用于输出es查询出的所有结果
    scroll_id = query['_scroll_id']

    for i in range(0, int(total/100)+1):
        # scroll参数必须指定否则会报错
        query_scroll = es.scroll(scroll_id=scroll_id,scroll='5m')['hits']['hits']
        results += query_scroll

    with open('./test.csv', 'w', newline='', encoding="utf_8_sig") as flow:
        csv_writer = csv.writer(flow)
        for res in results:
            csvrow1=[]
            csvrow1.append( res['_id'] )
            csvrow1.append( res["_source"]['name'] )
            csv_writer.writerow(csvrow1)
    print('done!')

ExportCsv()

你可能感兴趣的:(python,python,elasticsearch,es,csv)