python elasticsearch 导出数据到json文件导入到另一个es中

import os
import subprocess
from elasticsearch import Elasticsearch
from elasticsearch.client.indices import IndicesClient

def get_all_index(es_host):
    client = Elasticsearch(es_host)
    schema = IndicesClient(client).get_mapping()
    indices_full_list = schema.keys()
    return [index for index in indices_full_list if not index.startswith(".")]
    
ALL_INDEX_LIST = get_all_index("http://127.0.0.1:9200") # 所有的index字符串列表

# 使用 npm install elasticdump
# elasticdump 所在的路径
ES_DUMP_CMD = os.path.join(os.path.dirname(__file__), "../node_modules/.bin/elasticdump")
ES_QUERY_JSON_DIR = os.path.expanduser("~/Desktop/es_query_json_dir")

def subprocess_popen(statement):
	# 可以打印出命令的输出
    file_out = subprocess.Popen(statement, shell=True, stdout=subprocess.PIPE)
    break_count = 0
    while True:
        line = file_out.stdout.readline()
        print(line.decode('utf-8').strip('
'))
        if subprocess.Popen.poll(file_out) == 0:
            break
        if not line.decode('utf-8').strip('
'):
            break_count += 1
        else:
            break_count = 0
        if break_count >= 10:
            break

def export_es_data():
    source_es_host = "http://127.0.0.1:9200"  # http://username:password@127.0.0.1:9200"
    # 可以根据search_body仅导出查询出来的数据
    search_body = '{"query":{"match":{"xx" : xx}}}'
    limit = 10000  # 每次导出的数据条数,听说最大为10000,本人没有测试,根据自身机器的内存大小进行调节
    index_count = 0
    for index_str in ALL_INDEX_LIST[index_count:]:
        es_export_cmd = f"{ES_DUMP_CMD} --limit={limit} --input={source_es_host}/{index_str} --output={ES_QUERY_JSON_DIR}{index_str}.json --searchBody '{search_body}'"
        print(f"-------------export {index_str} ------------- index of ALL_INDEX_LIST: {index_count}")
        print(es_export_cmd)
        subprocess_popen(es_export_cmd)
        index_count += 1
        print("")


def import_es_data():
    target_es_host = "http://127.0.0.1:9200" # http://username:password@127.0.0.1:9200"
    index_count = 0
    limit = 10000
    for index_str in ALL_INDEX_LIST[index_count:]:
        es_import_cmd = f"{ES_DUMP_CMD} --limit={limit} --input={ES_QUERY_JSON_DIR}{index_str}.json --output={target_es_host}"
        print(f"-------------import {index_str} ------------- index of ALL_INDEX_LIST: {index_count}")
        print(es_import_cmd)
        subprocess_popen(es_import_cmd)
        index_count += 1
        print("")


if __name__ == '__main__':
    export_es_data() # 导出的方法
    import_es_data() # 导入的方法
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值