Elasticdump 数据导入/导出ElasticSearch数据

Elasticdump 数据导入/导出

一、安装nodejs环境

下载Node.js安装包进行安装

官方地址: 安装包下载

二、安装完 nodejs 后,通过npm安装elasticdump工具
# 本地安装
> npm install elasticdump
> node ./node_modules/elasticdump/bin/elasticdump
# 导出语法
node elasticdump --input {protocol}://{host}:{port}/{index} --output ./test_index.json --type=(mapping || data)
# 导入语法
node elasticdump --input ./test_index.json --output {protocol}://{host}:{port}/{index} --type=(mapping || data)
三、举个例子
  1. 将ElasticSearch中将数据导出为本地JSON文件
node ./node_modules/elasticdump/bin/elasticdump --input http://127.0.0.1:9200/test_index --output ./test_index_m.json --type=mapping
node ./node_modules/elasticdump/bin/elasticdump --input http://127.0.0.1:9200/test_index --output ./test_index_d.json --type=data

上面命令需要导出两个文件:mappingdata 。这两个文件都是导入到ES中所需要的,其中mapping 也可以自己手动生成

  1. 将json数据导入到ElasticSearch中

创建索引

# PUT请求
http:127.0.0.1:9200/test_index

导入数据

# 先导入mapping文件
node ./node_modules/elasticdump/bin/elasticdump --input=./test_index_m.json --output=http://127.0.0.1:9200/test_index --type=mapping
# 再导入data数据
node ./node_modules/elasticdump/bin/elasticdump --input=./test_index_d.json --output=http://127.0.0.1:9200/test_index --type=data
如果觉得操作麻烦,也可以简化操作,如下:

简化操作:将下方代码分别保存成 export.bat文件 和 import.bat文件,放在与node_modules 同级目录下(仅限于Windows),并在cmd命令窗口执行。

导出数据 export.bat

@echo off
set a1=%~dp0
set a2=%~1
set a3=%~2
set cc=node %a1%node_modules\elasticdump\bin\elasticdump --input http://127.0.0.1:9200/%a2% --output %a1%data\%a2%_%a3%.json --type=%a3%
echo run %cc%
echo dir is not exist?
set dataDir=%a1%data
if not exist %dataDir% (
		echo create dir...
		md %dataDir%
	) else (
		echo dir exist!
	)

%cc%

导入数据 import.bat

@echo off
set a1=%~dp0
set a2=%~1
set a3=%~2
set cc=node %a1%node_modules\elasticdump\bin\elasticdump --input %a1%data\%a2%_%a3%.json --output http://127.0.0.1:9200/ --type=%a3%
echo run %cc%
echo file is not exist?
set dataFile=%a1%data\%a2%_%a3%.json
if not exist %dataFile% (
		echo file is not exist!
		exit
	) else (
		echo file exist,please wait...
	)

%cc%

在当前目录打开cmd命令窗口,操作格式如下:

# 导出
.\export.bat your_index 类型参数(mapping || data)
# 导入
.\import.bat your_index 类型参数(mapping || data)
四、有权限认证情况,ES直接导入
node elasticdump --input=http://user1:pwd1@ip1:9200/my_index --output=http://user2:pwd2@ip2:9200/your_index --type=data

elasticdump 提供给了–httpAuthFile 参数来做认证,需要写一个ini文件 ,文件中写入用户名和密码,并使用 --httpAuthFile 来指定ini文件

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值