1. elasticdump 安裝
- npm 安裝
- docker 安裝
docker pull taskrabbit/elasticsearch-dump
2.elasticdump 使用
elasticdump
提供了多種導(dǎo)入導(dǎo)出數(shù)據(jù)的方式,可以 index <-> index 咏删、 index <-> .json 文件游桩,還支持將 index 查詢結(jié)果導(dǎo)出到 .json 文件皆串。執(zhí)行的命令也很簡單针姿,只需指定數(shù)據(jù)來源 input
、數(shù)據(jù)輸出 output
斥滤、數(shù)據(jù)類型 type
即可谍椅。
2.1 支持導(dǎo)入導(dǎo)出的type
列表
type | 說明 |
---|---|
settings | 對應(yīng) es 中的 settings |
analyzer | 對應(yīng) es 中的 analyzer |
data | es 查詢出來的數(shù)據(jù) |
mapping | 對應(yīng) es 中的 mapping |
alias | 對應(yīng) es 中的 alias |
template | 對應(yīng) es 中的 template |
2.2 es數(shù)據(jù)的導(dǎo)入導(dǎo)出
2.2.1 導(dǎo)入導(dǎo)出命令
- 導(dǎo)出數(shù)據(jù)到 .json 文件:
// 導(dǎo)出 index 的 mapping 到 .json 文件
elasticdump \
--input=http://production.es.com:9200/my_index \
--output=/data/my_index_mapping.json \
--type=mapping
// 導(dǎo)出 index 的所有數(shù)據(jù)到 .json 文件
elasticdump \
--input=http://production.es.com:9200/my_index \
--output=/data/my_index.json \
--type=data
- 從.json文件導(dǎo)入數(shù)據(jù)
// 從 .json 文件導(dǎo)入 templates 到 ES
elasticdump \
--input=./templates.json \
--output=http://es.com:9200 \
--type=template
2.2.2 導(dǎo)入導(dǎo)出示例
a.導(dǎo)出 index 數(shù)據(jù)到 .json 文件
- 導(dǎo)出命令:
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
- 執(zhí)行結(jié)果:
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.導(dǎo)入 .json 文件中的數(shù)據(jù)到 es
- 導(dǎo)入命令:
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
- 執(zhí)行結(jié)果:
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
在導(dǎo)入數(shù)據(jù)前可以先把測試用的index數(shù)據(jù)清空
- 清空es index 數(shù)據(jù)
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 執(zhí)行 elasticdump 導(dǎo)入導(dǎo)出文件的文件共享問題
在 docker 中執(zhí)行 elasticdump 進(jìn)行數(shù)據(jù)導(dǎo)入導(dǎo)出時(shí),需要考慮宿主機(jī)之間文件共享的問題界赔,在 run elasticdump 用 -v 指定共享文件的位置 - Error: connect ECONNREFUSED 127.0.0.1:9200
在docker中執(zhí)行elasticdump丢习,訪問本機(jī) es 注意不能用 127.0.0.1 或 localhost ,要用機(jī)器IP哦淮悼!