Elasticsearch索引遷移的三種方式

0、引言

將ES中的索引拷貝到其餘ES中,或者將ES總體遷移,研究發現有兩個開源的工具:elaticserch-dump和 Elasticsearch-Exporter。
除此以外,logstash在索引同步、遷移方面的做用也很大。
兩工具及logstash實現遷移的介紹、安裝、使用、驗證效果等展現以下:javascript

 

一、elasticsearch-dump遷移

1.1 elasticsearch-dump簡介

Tools for moving and saving indicies. 歷來移動和保存索引的工具。
https://github.com/taskrabbit/elasticsearch-dumpjava

1.2 elasticsearch-dump安裝

1) yum install epel-release
2) yum install nodejs
3) yum install npm
4) npm install elasticdump
5) cd node_modules/elasticdump/bin  後即可以執行操做。

安裝後以下所示:node

[root@N3 elasticdump]# pwd
/home/tp/node_modules/elasticdump
[root@N3 elasticdump]# ls -al
total 388
drwxr-xr-x 2 root root 4096 Mar 21 15:46 bin
-rw-r--r-- 1 root root 174 Mar 18 2016 Dockerfile
-rw-r--r-- 1 root root 299251 Mar 15 2014 elasticdump.jpg
-rw-r--r-- 1 root root 6172 Feb 2 23:47 elasticdump.js
drwxr-xr-x 2 root root 4096 Jul 13 2016 .github
drwxr-xr-x 3 root root 4096 Mar 21 15:46 lib
-rw-r--r-- 1 root root 11356 May 22 2014 LICENSE.txt
drwxr-xr-x 10 root root 4096 Mar 21 15:46 node_modules
-rw-r--r-- 1 root root 44 May 22 2014 .npmignore
-rw-r--r-- 1 root root 15135 Mar 21 15:46 package.json
-rw-r--r-- 1 root root 13335 Dec 14 06:20 README.md
drwxr-xr-x 3 root root 4096 Mar 21 15:46 test
-rw-r--r-- 1 root root 1150 Dec 2 07:54 .travis.yml

 

1.3 elasticsearch-dump 使用

'#拷貝analyzer如分詞
elasticdump \
  --input=http://production.es.com:9200/my_index \
  --output=http://staging.es.com:9200/my_index \
  --type=analyzer
'#拷貝映射
elasticdump \
  --input=http://production.es.com:9200/my_index \
  --output=http://staging.es.com:9200/my_index \
  --type=mapping
'#拷貝數據
elasticdump \
  --input=http://production.es.com:9200/my_index \
  --output=http://staging.es.com:9200/my_index \
  --type=data

 

1.4 elasticsearch-dump實戰小結

源ES版本1.6.0,目標ES版本:2.3.4,驗證發現:analyzer和mapping能夠拷貝成功。
可是,data拷貝不成功。目標機器ES中不能顯示出數據。根本緣由沒有排查到。git

 

 

二、 Elasticsearch-Exporter遷移

2.1 Elasticsearch-Exporter簡介

https://github.com/mallocator/Elasticsearch-Exporter
A small script to export data from one Elasticsearch cluster into another. 將ES中的數據向其餘導出的簡單腳本實現。github

2.二、Elasticsearch-Exporter安裝

http://www.dahouduan.com/2014/12/25/centos-yum-install-nodejs-npm/
centos用 yum 方式安裝 nodejs 和 npm數據庫

npm install nomnom
npm install colors
npm install elasticsearch-exporter --production

安裝後:npm

[root@N3 elasticsearch-exporter]# ll -ls
total 80
 4 drwxr-xr-x 2 root root 4096 Mar 21 22:01 drivers
12 -rw-r--r-- 1 root root 11523 Sep 19 2014 exporter.js
12 -rw-r--r-- 1 root root 11324 Mar 16 2014 LICENSE
 4 drwxr-xr-x 4 root root 4096 Mar 21 22:01 node_modules
12 -rw-r--r-- 1 root root 11259 Sep 19 2014 options.js
16 -rw-r--r-- 1 root root 14500 Mar 21 22:01 package.json
16 -rw-r--r-- 1 root root 12645 Sep 19 2014 README.md
 4 drwxr-xr-x 2 root root 4096 Apr 25 2014 tools

 

2.三、 Elasticsearch-Exporter使用

node exporter.js -a <source hostname> -b <target hostname> -p <s port> -q <t port> -i <s index> -j <t index>

便可實現跨機器索引的遷移。json

更多的參數能夠查看node exporter.js –helpcentos

[root@N3 elasticsearch-exporter]# node exporter.js --help
Elasticsearch Exporter - Version 1.4.0
Usage: exporter [options]
Options:
  -a <hostname>, --sourceHost <hostname>  遷移源機器地址
  -b <hostname>, --targetHost <hostname>  遷移目的機器地址(若是沒有設置索引,目的地址須要有別於源地址)
  -p <port>, --sourcePort <port>   源機器的ES的端口,9200(通常)
  -q <port>, --targetPort <port>    目標機器的ES的端口,9200(通常)

  -i <index>, --sourceIndex <index> 源ES待導出的索引,若是該值不設定,整個的數據庫都會導出。
  -j <index>, --targetIndex <index>目標機器ES的索引,若是源索引設定,該值必須填寫。
  • 1

 

2.四、 Elasticsearch-Exporter 索引遷移實戰(驗證ok)

 

[root@No3 elasticsearch-exporter]# node exporter.js -a 10.221.110.31-b 100.0.1.130 -p 9200 -q 9200 -i awppx -j awppx
同步最後會顯示:
Number of calls: 169
Fetched Entries: 8064 documents
Processed Entries: 8064 documents
Source DB Size: 8064 documents

表明同步成功。
源ES版本1.6.0,目標ES版本:2.3.4,
驗證發現:可使用 Elasticsearch-Exporter跨機器、跨ES版本同步索引成功。app

三、logstash定向索引遷移

[root@N3 bin]# cat ./logstash_output_mongo/logstash_es22es.conf
input {
  elasticsearch {
  hosts => [ "100.200.10.54:9200" ]
  index => "doc"
  size => 1000
  scroll => "5m"
  docinfo => true
  scan => true
  }
}

filter {
json {
  source => "message"
  remove_field => ["message"]
  }
  mutate {
  # rename field from 'name' to 'browser_name'
  rename => { "_id" => "wid" }
}
}

output {
  elasticsearch {
  hosts => [ "100.20.32.45:9200" ]
  document_type => "docxinfo"
  index => "docx"
  }

  stdout {
  codec => "dots"
  }

}

 

四、小結
對比發現, Elasticsearch-Exporter在索引遷移方面相對更好用。(待深刻研究補充)
而logstash定向索引用於輔助解決 Elasticsearch-Exporter不一樣版本遷移有Bug的情形。

 

參考:
[1]http://blog.csdn.net/u014587343/article/details/50541494
[2]http://stackoverflow.com/questions/26547560/how-to-move-elasticsearch-data-from-one-server-to-another

2017年3月23日 21:20 思於家中牀前

做者:銘毅天下
轉載請標明出處,原文地址:
http://blog.csdn.net/laoyang360/article/details/65449407 若是感受本文對您有幫助,請點擊‘頂’支持一下,您的支持是我堅持寫做最大的動力,謝謝!

相關文章
相關標籤/搜索