背景:
1、使用postman查詢出來(lái)的數(shù)據(jù)無(wú)法直接導(dǎo)入到es铭段,導(dǎo)入es需要手動(dòng)去整理查詢的數(shù)據(jù)骤宣,麻煩耗時(shí)。
2序愚、自己寫(xiě)一個(gè)程序去自動(dòng)導(dǎo)出再導(dǎo)入憔披,耗時(shí)較長(zhǎng),不經(jīng)濟(jì)。
3芬膝、mapping和settings都要導(dǎo)出一并遷移至目標(biāo)服務(wù)器望门。
是否有一種專門(mén)用來(lái)處理es的索引導(dǎo)出導(dǎo)入的工具呢?當(dāng)然有锰霜!
下載elasticdump并安裝(參考官網(wǎng)https://www.npmjs.com/package/elasticdump)
執(zhí)行導(dǎo)入導(dǎo)出操作:以索引bum_authority_collection為例
# 導(dǎo)出索引Mapping數(shù)據(jù)
./elasticdump --input=http://source:9200/bum_authority_collection/ --output=./bum_authority_collection.json --type=mapping
# 導(dǎo)出索引數(shù)據(jù)
./elasticdump --input=http://source:9200/bum_authority_collection/ --output=./bum_authority_collection_data.json --limit 1000 --type=data
#導(dǎo)入索引Mapping數(shù)據(jù)
./elasticdump --input=./bum_authority_collection.json --output=http://target:9200/bum_authority_collection/ --type=mapping
#導(dǎo)入索引數(shù)據(jù)
./elasticdump --input=./bum_authority_collection_data.json --output=http://target:9200/bum_authority_collection/ --limit 1000 --type=data
那問(wèn)題來(lái)了筹误,如果我es里索引非常多,這樣一個(gè)一個(gè)去執(zhí)行依然會(huì)耗時(shí)癣缅,怎么做厨剪?當(dāng)然想到了shell腳本。
腳本名字為:esExportOrInput.sh
#!/bin/sh
index_name=$1
index_data=$1"_data"
index_settings=$1"_settings"
echo "開(kāi)始執(zhí)行"
# 導(dǎo)出索引Mapping數(shù)據(jù)
./elasticdump --input=http://source:9200/$index_name/ --output=./$index_name.json --type=mapping
./elasticdump --input=http://source:9200/$index_name/ --output=./$index_settings.json --type=settings
# 導(dǎo)出索引數(shù)據(jù)
./elasticdump --input=http://source:9200/$index_name/ --output=./$index_data.json --type=data --limit=1000
#導(dǎo)入索引Mapping數(shù)據(jù)
echo "執(zhí)行刪除目標(biāo)服務(wù)的索引start:"$index_name
curl -XDELETE http://target:9200/$index_name
echo "執(zhí)行刪除目標(biāo)服務(wù)的索引end:"$index_name
sleep 3
echo "等待三秒友存。祷膳。。"
./elasticdump --input=./$index_settings.json --output=http://target:9200/$index_name/ --type=settings
./elasticdump --input=./$index_name.json --output=http://target:9200/$index_name/ --type=mapping
./elasticdump --input=./$index_data.json --output=http://target:9200/$index_name/ --type=data --limit=1000
#清除生成的文件
rm -f ./$index_name.json
rm -f ./$index_settings.json
rm -f ./$index_data.json
#清除生成的文件
echo "索引"$index_name"執(zhí)行完畢"
執(zhí)行時(shí)候需要傳遞1個(gè)變量屡立,就是索引名字index_name
溫馨提示:
1直晨、這個(gè)腳本僅僅只能在安裝了elasticdump服務(wù)器上使用,腳本目錄在/root/node_modules/elasticdump/bin
2、導(dǎo)出導(dǎo)入默認(rèn)100條一批,可以添加--limit 每頁(yè)條數(shù) 來(lái)自定義每頁(yè)數(shù)量。
3、導(dǎo)入的時(shí)候一定要限制性settings的文件導(dǎo)入核行,在執(zhí)行mapping的導(dǎo)入,不然會(huì)沖突再扭,因?yàn)閟ettings上帶了uuid的唯一標(biāo)識(shí)骗污。
樣例:如果我要對(duì)bum_user這個(gè)索引的數(shù)據(jù)從測(cè)試環(huán)境遷移到壓測(cè)服務(wù)器target上,就這樣執(zhí)行命令
./esExportOrInput.sh bum_user
留個(gè)作業(yè)诅福,可以對(duì)源服務(wù)器和目標(biāo)服務(wù)器進(jìn)行動(dòng)態(tài)參數(shù)傳遞匾委,就可以做到萬(wàn)能適配了,有興趣就重寫(xiě)下這個(gè)腳本吧Cト蟆B咐帧!