devops:改变思维,让一切更加高效

elasticdump实现es数据导入导出

2019-12-26  本文已影响0人  阳光的小mi

1. elasticdump 安装

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 导入导出命令

// 导出 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 文件导入 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数据清空

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.注意

参考文档

上一篇 下一篇

猜你喜欢

热点阅读