elasticdump实现es数据导入导出

作者: 阳光的小mi | 来源:发表于2019-12-26 17:52 被阅读0次

    1. elasticdump 安装

    • npm 安装
    • docker 安装
      docker pull taskrabbit/elasticsearch-dump
      

    2.elasticdump 使用

    elasticdump提供了多种导入导出数据的方式,可以 index <-> index 、 index <-> .json 文件,还支持将 index 查询结果导出到 .json 文件。执行的命令也很简单,只需指定数据来源 input 、数据输出 output 、数据类型 type 即可。

    2.1 支持导入导出的type列表

    type 说明
    settings 对应 es 中的 settings
    analyzer 对应 es 中的 analyzer
    data es 查询出来的数据
    mapping 对应 es 中的 mapping
    alias 对应 es 中的 alias
    template 对应 es 中的 template

    2.2 es数据的导入导出

    2.2.1 导入导出命令

    • 导出数据到 .json 文件:
    // 导出 index 的 mapping 到 .json 文件
    elasticdump \
      --input=http://production.es.com:9200/my_index \
      --output=/data/my_index_mapping.json \
      --type=mapping
    // 导出 index 的所有数据到 .json 文件
    elasticdump \
      --input=http://production.es.com:9200/my_index \
      --output=/data/my_index.json \
      --type=data
    
    • 从.json文件导入数据
    // 从 .json 文件导入 templates 到 ES 
    elasticdump \
      --input=./templates.json \
      --output=http://es.com:9200 \
      --type=template
    

    2.2.2 导入导出示例

    a.导出 index 数据到 .json 文件

    • 导出命令:
    docker run --rm -ti -v /Users/root/mnt/elasticsearch/data:/tmp taskrabbit/elasticsearch-dump \
    --input=http://your's ip:9200/my_index_log \
    --output=/tmp/my_index.json \
    --type=data
    
    • 执行结果:
    Thu, 26 Dec 2019 08:32:45 GMT | starting dump
    Thu, 26 Dec 2019 08:32:45 GMT | got 1 objects from source elasticsearch (offset: 0)
    Thu, 26 Dec 2019 08:32:45 GMT | sent 1 objects to destination file, wrote 1
    Thu, 26 Dec 2019 08:32:45 GMT | got 0 objects from source elasticsearch (offset: 1)
    Thu, 26 Dec 2019 08:32:45 GMT | Total Writes: 1
    Thu, 26 Dec 2019 08:32:45 GMT | dump complete
    

    b.导入 .json 文件中的数据到 es

    • 导入命令:
    docker run --rm -ti -v /Users/root:/tmp taskrabbit/elasticsearch-dump \
    > --input=/tmp/Desktop/my_index.json \
    > --output=http://your's ip:9200/my_index_log \
    > --type=data
    
    • 执行结果:
    Thu, 26 Dec 2019 08:57:29 GMT | starting dump
    Thu, 26 Dec 2019 08:57:34 GMT | got 100 objects from source file (offset: 0)
    Thu, 26 Dec 2019 08:57:35 GMT | sent 100 objects to destination elasticsearch, wrote 100
    Thu, 26 Dec 2019 08:57:35 GMT | got 2 objects from source file (offset: 100)
    Thu, 26 Dec 2019 08:57:35 GMT | sent 2 objects to destination elasticsearch, wrote 2
    Thu, 26 Dec 2019 08:57:35 GMT | got 0 objects from source file (offset: 102)
    Thu, 26 Dec 2019 08:57:35 GMT | Total Writes: 102
    Thu, 26 Dec 2019 08:57:35 GMT | dump complete
    

    在导入数据前可以先把测试用的index数据清空

    • 清空es index 数据
    curl -X POST \
      'http://localhost:9200/my_index_log/_delete_by_query?refresh=&slices=100&pretty=true' \
      -H 'Content-Type: application/json' \
      -d '{ "query": { "match_all": {} } }'
    

    3.注意

    • docker 执行 elasticdump 导入导出文件的文件共享问题
      在 docker 中执行 elasticdump 进行数据导入导出时,需要考虑宿主机之间文件共享的问题,在 run elasticdump 用 -v 指定共享文件的位置
    • Error: connect ECONNREFUSED 127.0.0.1:9200
      在docker中执行elasticdump,访问本机 es 注意不能用 127.0.0.1 或 localhost ,要用机器IP哦!

    参考文档

    相关文章

      网友评论

        本文标题:elasticdump实现es数据导入导出

        本文链接:https://www.haomeiwen.com/subject/efsloctx.html