本地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的設置

可根據個人業務需求,自行選擇下面需要的配置選項和設置

  • “scroll”: 每次複製5M的數據,一般設置為5-15 M性能較佳,根據伺服器性能自行選擇

  • “wait_for_completion”: false 設置不用前台等待返回結果,後台自動執行

  • “max_docs”: 定義只同步100個文檔

  • “conflicts”,”op_type”:這兩個一般一起使用,op_type to create將導致_reindex僅在目標索引中創建缺少的文檔,但是會報導致版本衝突中止_reindex操作,可以設置 「conflict」:”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": "//: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": "//: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 _cat/indices

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

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

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

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

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

  • elasticsearch-dump
  • snapshot
  • logstash

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

Tags: