本地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

posted on 2020-07-24 11:50  有梦可有为  阅读(1842)  评论(0编辑  收藏  举报