1. 程式人生 > >本地ES叢集資料通過_reindex方式遷移到騰訊雲伺服器(親測有效)

本地ES叢集資料通過_reindex方式遷移到騰訊雲伺服器(親測有效)

# 本地ES叢集資料通過_reindex方式遷移到騰訊雲伺服器(親測有效) 隨著業務量的增加,本地的ES叢集伺服器效能和磁碟空間有點不夠使用,專案組考慮使用騰訊雲伺服器,以下是我測試的使用_reindex方式遷移ES資料的具體步驟。 ## 1.在騰訊雲的ES上建立新索引 可根據業務需求,自行刪減mappings無用的欄位,更改欄位型別和settings的設定,重新設定新索引。 ```bash PUT /test1 { "mappings" : { "properties" : { "num" : { "type" : "text", "analyzer": "my_analyzer" }, "name" : { "type" : "text", "fields" : { "keyword" : { "type" : "keyword", "ignore_above" : 256 } }, "analyzer": "my_analyzer" }, "englishName" : { "type" : "text", "fields" : { "keyword" : { "type" : "keyword", "ignore_above" : 256 } }, "analyzer": "my_analyzer" }, "msg" : { "type" : "text", "fields" : { "keyword" : { "type" : "keyword", "ignore_above" : 256 } }, "analyzer": "my_analyzer" } } }, "settings": { "index": { "analysis": { "analyzer": { "my_analyzer": { "type": "custom", "tokenizer": "my_tokenizer" } }, "tokenizer": { "my_tokenizer": { "type": "ngram", "min_gram": "1", "max_gram": "2" } } } } } } ``` ## 2.設定白名單 在騰訊雲ES的elasticsearch.yml配置檔案中新增本地的ES叢集IP白名單. ==注意:如果本地使用的是內網,需要開通外網訪問地址和埠,這裡白名單的ip和埠也要換成外網的== ```bash #reindex.remote.whitelist: ["ip:9200","ip2:9201"] 遷移資料白名單 reindex.remote.whitelist: ["localhost:9200"] #跨域問題 http.cors.enabled: true http.cors.allow-origin: "*" ``` ## 3.準備_reindex的設定 可根據個人業務需求,自行選擇下面需要的配置選項和設定 更多_reindex引數相關配置可參考官網 https://www.elastic.co/guide/en/elasticsearch/reference/6.2/docs-reindex.html - "scroll": 每次複製5M的資料,一般設定為5-15 M效能較佳,根據伺服器效能自行選擇 - "wait_for_completion": false 設定不用前臺等待返回結果,後臺自動執行 - "max_docs": 定義只同步100個文件 - "conflicts","op_type":這兩個一般一起使用,op_type to create將導致_reindex僅在目標索引中建立缺少的文件,但是會報導致版本衝突中止_reindex操作,可以設定 "conflicts": "proceed",_reindex程序將繼續發生版本衝突並返回遇到的版本衝突計數。(不建議使用,ES會自動處理ID相同的資料覆蓋刪除) - "source": 本地要遷移的ES索引設定 - "remote":本地ES的對外地址,超時時間設定 - "index": 本地要遷移的ES索引名稱 - "_source": 可設定保留只需要遷移的索引欄位 - "query": 可設定篩選條件 - "size": 每次傳輸文件的資料量,預設值為1000,可設定為5000-20000 - "dest": "index" 騰訊雲要接受資料的索引,第一步建立的那個 ```bash POST /_reindex?scroll=5m&wait_for_completion=false { "max_docs": 100, "conflicts": "proceed", "source": { "remote": { "host": "http://:9200", "socket_timeout": "5m", "connect_timeout": "300s" }, "index": "test1", "_source": ["name", "msg",], "query": { "match": { "name": "小明" } } "size": 5000 }, "dest": { "index": "test1", "op_type": "create" } } ``` ## 4.執行命令,遷移資料 以下都在騰訊雲的kibana中執行的 設定不重新整理和副本數為0 ```bash PUT /test1/_settings { "refresh_interval": -1, "number_of_replicas": 0 } ``` 執行第三步建立的_reindex ```bash POST /_reindex?scroll=5m&wait_for_completion=false { "max_docs": 100, "conflicts": "proceed", "source": { "remote": { "host": "http://:9200", "socket_timeout": "5m", "connect_timeout": "300s" }, "index": "test1", "_source": ["name", "msg",], "query": { "match": { "name": "小明" } } "size": 5000 }, "dest": { "index": "test1", "op_type": "create" } } ``` 等待資料執行,使用 GET _cat/indices 命令檢視資料執行結果量 使用 GET _tasks?detailed=true&actions=*reindex可以檢視正在執行的_reindex狀態 ```bash GET _cat/indices GET _tasks?detailed=true&actions=*reindex ``` 資料全部執行完後,恢復原本要設定的重新整理間隔和副本數. 擴充套件:關於副本數數量設定,可參考我另一篇引用文章中[**ES的叢集原理**中**二、ES叢集核心原理分析:**](https://www.cnblogs.com/shenlei-blog/p/13367269.html) ```bash PUT /index_paytrade_v1/_settings { "refresh_interval": "30s", "number_of_replicas": 1 } ``` 好了,至此就大功搞定了,可以進行查詢資料測試了。 關於ES資料遷移騰訊雲還有其他3種方式 - elasticsearch-dump - snapshot - logstash 具體可參考騰訊雲的官方文件地址 : https://cloud.tencent.com/document/product/8