elasticsearch數據遷移

 1.   遷移緣由

    由於歷史緣由,elasticsearch所在的服務器和kafka,java應用混合部署在一塊兒,資源爭用,CPU爆升等緣由,因此這一次將elasticsearch遷移到獨佔的服務器上, 這裏使用的elasticsearch7.0 版本,遷移至elasticsearch7.4版本。java

2.  遷移方式

   第三方工具遷移或elasticsearch自己快照方式遷移,這裏以第三方工具遷移爲例,node

第三方遷移工具備多個:linux

Elaticsearch-dump:    支持es7.x 及前期多個版本,安裝使用簡便,結合shell腳本可作成全自動備份, github 4千星,可見很是熱門
git

github.com/taskrabbit/…
github

Elaticsearch-migration:    支持不一樣版本間相互遷移和basic auth, 使用golang編寫,博主最近也一直在寫golang,對這個遷移工具仍是蠻感興趣。
golang

github.com/medcl/esm-v…
docker

Elaticsearch-Exporter:   支持不一樣版本間相互遷移,不過最近代碼提交是2年前,不知道支不支持es7.x版本,這裏博主沒有測試shell

github.com/mallocator/…npm


經上述簡單研究,決定使用Elaticsearch-dump 作爲本次遷移的工具json

3.  遷移準備

原es7.0 遷移至 es7.4 (es最新版本), 提早去es官網下載好es rpm文件(測試的話建議使用docker安裝,簡單方便)

3.1  elasticsearch安裝

wget  https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-7.4.2-x86_64.rpm
rpm -ivh elasticsearch-7.4.2-x86_64.rpm
複製代碼

docker安裝(開啓跨域)

docker run -d --rm --name elasticsearch -p 9200:9200 -p 9300:9300 -e discovery.type=single-node -e http.cors.enabled=true -e http.cors.allow-origin=* -e http.cors.allow-headers=X-Requested-With,X-Auth-Token,Content-Type,Content-Length,Authorization -e http.cors.allow-credentials=true docker.elastic.co/elasticsearch/elasticsearch-oss:7.4.2複製代碼


3.2  elaticsearch-dump

3.2.1 安裝nodejs
cd /opt
wget https://npm.taobao.org/mirrors/node/v10.15.3/node-v10.15.3-linux-x64.tar.gz
tar xf node-v10.15.3-linux-x64.tar.gz
mv  node-v10.15.3-linux-x64 node 
echo "export NODE_HOME=/opt/node" >> /etc/profile
echo "export PATH=${NODE_HOME}/bin:$PATH" >> /etc/profile
source /etc/profile
3.2.2 安裝elasticdump 
npm install elasticdump -g     #全局安裝
elasticdump --help             #查看幫助
複製代碼

3.3  記錄待遷移elasticsearch 索引數據內容和數據總量,以便遷移後覈對

curl http://ip:9200/_cat/indices複製代碼


4. 索引遷移(導出導入操做)

4.1 索引導出

elasticdump --input=http://舊es_ip:9200/索引名稱  --output=/opt/es/索引名稱.index.json --type=data複製代碼

4.2 索引導入

elasticdump --input=/opt/es/索引名稱.index.json --output=http://新es_ip:9200複製代碼

4.3 導出導入設置

導入導出時會顯示 每次操做的 object數據(默認爲100),1萬條數據用時1分30秒,若是調大,設置--limit參數既可

4.4 直接從源es到遷移到新的es

# 備份 mapping
elasticdump --input="http://localhost:9200/MyIndex" --output="http://localhost:9200/MyIndex" --type=mapping
# 備份數據
elasticdump --input="http://localhost:9200/MyIndex" --output="http://localhost:9200/MyIndex" --type=data複製代碼

4.5 數據比對(使用shell命令diff比較既可)

數據對比方法:
老數據: curl -s http://172.1.1.6:9200/_cat/indices | awk '{print $3,$7}' |sort -nr > es1.txt
新數據: curl -s http://127.0.0.1:9200/_cat/indices | awk '{print $3,$7}' | sort -nr > es2.txt

比較數據結果:
diff es1.txt es2.txt


5.  可視化工具(es數據查看)

   目前流行的工具備elasticsearch-head, elaticsearch-HQ, kibana,dejavu

kibana爲官方工具,提供了索引查看,管理和圖表功能

head爲es插件,提供了索引管理(刪增), 數據查看功能(界面不夠美觀)

elaticsearch-HQ  官方介紹:  Monitoring and Management Web Application for ElasticSearch instances and clusters

dejavu   界面美觀,支持增刪改查,惋惜一次只能鏈接一個索引。須要頁面多開

以上4個工具都提供了docker 一鍵安裝,簡單高效

這裏使用dejavu工具,不盡界面美觀,還支持數據增刪改查功能,簡單好用

docker run -p 1358:1358 -d appbaseio/dejavu複製代碼

待啓動完成後訪:   http://ip:1358


相關文章
相關標籤/搜索