本地ES集群數據通過_reindex方式遷移到騰訊雲服務器(親測有效)


本地ES集群數據通過_reindex方式遷移到騰訊雲服務器(親測有效)

隨着業務量的增加,本地的ES集群服務器性能和磁盤空間有點不夠使用,項目組考慮使用騰訊雲服務器,以下是我測試的使用_reindex方式遷移ES數據的具體步驟。

1.在騰訊雲的ES上建立新索引

可根據業務需求,自行刪減mappings無用的字段,更改字段類型和settings的設置,重新設置新索引。

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和端口也要換成外網的

#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" 騰訊雲要接受數據的索引,第一步創建的那個

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

PUT /test1/_settings
{
   "refresh_interval": -1,
   "number_of_replicas": 0
}

執行第三步創建的_reindex

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狀態

GET _cat/indices

GET _tasks?detailed=true&actions=*reindex

數據全部執行完后,恢復原本要設置的刷新間隔和副本數.

擴展:關於副本數數量設置,可參考我另一篇引用文章中ES的集群原理二、ES集群核心原理分析:

PUT /index_paytrade_v1/_settings
{
   "refresh_interval": "30s",
   "number_of_replicas": 1
}

好了,至此就大功搞定了,可以進行查詢數據測試了。

關於ES數據遷移騰訊雲還有其他3種方式

  • elasticsearch-dump
  • snapshot
  • logstash

具體可參考騰訊雲的官方文檔地址 : https://cloud.tencent.com/document/product/845/35568


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM