本文共 1405 字,大约阅读时间需要 4 分钟。
本工具旨在帮助用户轻松地对Elasticsearch数据库进行数据导入和导出操作,支持批量处理和条件查询,适用于开发测试及数据迁移场景。
npm install elasticdump
将elasticdump放置在项目根目录,确保路径正确。
def get_all_index(es_host): client = Elasticsearch(es_host) schema = IndicesClient(client).get_mapping() indices = schema.keys() return [index for index in indices if not index.startswith(".")] def export_es_data(): source_es_host = "http://127.0.0.1:9200" search_body = '{"query":{"match":{"xx" : xx}}}' limit = 10000 for index_str in ALL_INDEX_LIST: 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"导出索引:{index_str}") subprocess_popen(es_export_cmd) def import_es_data(): target_es_host = "http://127.0.0.1:9200" for index_str in ALL_INDEX_LIST: es_import_cmd = f"{ES_DUMP_CMD} --limit={limit} --input={ES_QUERY_JSON_DIR}{index_str}.json --output={target_es_host}" print(f"导入索引:{index_str}") subprocess_popen(es_import_cmd) if __name__ == "__main__": export_es_data() import_es_data()
limit值,默认为10000subprocess_popen性能ES_QUERY_JSON_DIR以释放存储空间通过以上工具,用户可以高效地管理Elasticsearch数据,适用于开发、测试及数据迁移场景。
转载地址:http://xtafk.baihongyu.com/