菜单

Python:连接es数据库查询并写入本地excel

2019-05-06 - 代码整理
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time    : 2019-4-29 9:45
# @Author  : Hannes.Chen
# @Site    : 
# @File    : ec_load.py
# @Software: PyCharm

from elasticsearch import Elasticsearch
from xlutils.copy import copy
import xlrd
import xlsxwriter
import numpy as np

es = Elasticsearch(
    hosts=['192.168.100.99'],
    http_auth=("chentx", "qweqweqew"),
    port=9200
)

json_str = es.search(
    index="dm_map_medical_institution",
    body={
        "size": 9999,
        "query": {
            "terms": {
                "medical_id": [
                    "0b609e9ecc25f932b22bf9a5d38bd163",
                    "ebba29d88913c2bc0c6c87ad6627d05b",
                    "b45447dcda738bf0e54f488404991f46",
                    ...
                    "bd9b6202f390f7cd8a8f912cf901520e",
                    "60e5928d4605e072815c36962e790170",
                    "1e5837f6e12c74925c52e43eda22ba19"
                ]
            }
        }
    }
)

workbook = xlsxwriter.Workbook('dm_map_medical_institution.xls')
worksheet = workbook.add_worksheet()
workbook.close()
book1 = xlrd.open_workbook('dm_map_medical_institution.xls')
book2 = copy(book1)
sheet = book2.get_sheet(0)

json_hits = json_str['hits']['hits']
i = 1
for item in json_hits:
    _id = item['_id']
    medical_id = item['_source']["medical_id"]
    name = item['_source']["name"]
    if 'other_name' in item['_source'].keys():
        other_name = item['_source']["other_name"]
    else:
        other_name = 'None'
    sheet.write(i, 0, _id)
    i = i + 1

book2.save('dm_map_medical_institution.xls')
print("write successful!")


打赏作者
标签:

发表评论

邮箱地址不会被公开。 必填项已用*标注