python导出ElasticSearch的全部内容

分类:N05_python

标签:

from flask import Flask, abort, request
from elasticsearch import Elasticsearch
import json
import os



app = Flask(__name__, template_folder='template', static_url_path='/', static_folder='resource')
app.config['SECRET_KEY'] = os.urandom(24)

app.jinja_env.auto_reload = True
app.config['TEMPLATES_AUTO_RELOAD'] = True


# 连接到Elasticsearch
es = Elasticsearch("http://172.17.0.2:9200")


# 显示文章列表,及其文章详情页
@app.route('/')
def index():
    return 'wx服务器启动了4'


# 显示文章列表,及其文章详情页
@app.route('/exportES', methods=['GET', 'POST'])
def export_es():
    name  = request.args.get('name', None)
    if name is None:
        return "请输入index名称"
    print("name:"+name)
    export_real2(name)
    return 'ok4'


def export_real2(name):
    # 初始化scroll
    index = "javatool03"
    # 查询体,这里是一个简单的match_all查询
    query_body = {
        "query": {
            "match_all": {}
        }
    }
    # page = es.search(index=index, scroll='2m', size=1000, query={"query": {"match_all": {}}})
    page = es.search(index=index, scroll='2m', size=1000, body=query_body)
    sid = page['_scroll_id']
    # print(page)
    # scroll_size = page['hits']['total']['value']
    scroll_size = page['hits']['total']
    print("scroll_size:"+str(scroll_size))
    # 开始滚动
    filename = "es_export.json"  
    with open(filename, 'w') as f:  
        f.write('[')  
        first = True  
        if scroll_size > 0:
            print(f"Scrolling over {scroll_size} documents...")  
            # page = es.scroll(scroll_id=sid, scroll='2m')
            #
            # print("page_size:" + str(page))
              
            # 写入文件  
            if not first:  
                f.write(',')  
            first = False  
              
            # 将hits转换为字符串并写入文件  
            hits = json.dumps(page['hits']['hits'], indent=4, ensure_ascii=False)
            f.write(hits)  
              
            # 更新scroll_id和剩余文档数  
            # sid = page['_scroll_id']
            # scroll_size = len(page['hits']['hits']["_source"])
      
        f.write(']')  
      
    # 清除scroll  
    es.clear_scroll(scroll_id=sid)  
      
    print(f"Exported {scroll_size} documents to {filename}")



def export_real(name):
    # 指定索引名
    index_name = name

    # 查询所有文档
    query = {
        "query": {
            "match_all": {}
        }
    }

    # 获取数据
    response = es.search(index=index_name, body=query)

    # 将文档写入JSON文件
    with open('export_%s.json' % index_name, 'w') as f:
        for doc in response['hits']['hits']:
            f.write(json.dumps(doc['_source']) + '\n')


if __name__ == '__main__':
    app.run(debug=True)
    # csrf.init_app(app)


修改内容