通过Reindex API迁移数据

Reindex API用于将全量数据或者满足特定条件的数据从一个索引复制到另一个索引,这个过程可以发生在同一个集群内,也可以跨集群。本文向您介绍如何通过Reindex API将数据从一个集群迁移到另一个集群。

使用限制

  • 两个集群在相同地域和可用区下。

  • 部署管控模式:支持将v2管控模式下的集群数据迁移到v3、v2集群之间迁移数据、v3集群之间迁移数据。

    集群有两种管控部署模式,分别为云原生新管控(v3)、基础管控(v2)。

    image

  • 集群版本:支持同一个大版本之间(将低版本中的数据迁移至高版本)迁移数据(如将8.15.1的数据迁移到8.15.1),不建议跨大版本之间迁移数据(如将7.7.1中的数据迁移到8.15.1)。

准备工作

本文示例中ES_1、ES_2两个集群均为8.15.1向量增强版,目标是将ES_2中的数据通过Reindex API迁移到ES_1中,迁移之前需完成以下准备工作。

准备测试数据

  • ES_2中创建索引并插入测试数据:

    PUT /product_info
    {
      "settings": {
        "number_of_shards": 5,
        "number_of_replicas": 1
      },
      "mappings": {
          "properties": {
            "productName": {
              "type": "text",
              "analyzer": "ik_smart"
            },
            "annual_rate":{
              "type":"keyword"
            },
            "describe": {
              "type": "text",
              "analyzer": "ik_smart"
            }
        }
      }
    }

    以上示例创建了一个名称为product_info的索引,包含productNameannual_ratedescribe字段。创建成功将返回以下结果。

    {
      "acknowledged" : true,
      "shards_acknowledged" : true,
      "index" : "product_info"
    }

    插入6条测试数据:

    POST /product_info/_bulk
    {"index":{}}
    {"productName":"理财产品A","annual_rate":"3.2200%","describe":"180天定期理财,最低20000起投,收益稳定,可以自助选择消息推送"}
    {"index":{}}
    {"productName":"理财产品B","annual_rate":"3.1100%","describe":"90天定投产品,最低10000起投,每天收益到账消息推送"}
    {"index":{}}
    {"productName":"理财产品C","annual_rate":"3.3500%","describe":"270天定投产品,最低40000起投,每天收益立即到账消息推送"}
    {"index":{}}
    {"productName":"理财产品D","annual_rate":"3.1200%","describe":"90天定投产品,最低12000起投,每天收益到账消息推送"}
    {"index":{}}
    {"productName":"理财产品E","annual_rate":"3.0100%","describe":"30天定投产品推荐,最低8000起投,每天收益会消息推送"}
    {"index":{}}
    {"productName":"理财产品F","annual_rate":"2.7500%","describe":"热门短期产品,3天短期,无须任何手续费用,最低500起投,通过短信提示获取收益消息"}
  • ES_1中创建索引,用于存储从ES_2中迁移过来的数据:

    PUT dest
    {
      "settings": {
        "number_of_shards": 5,
        "number_of_replicas": 1
      }
    }

通过NLBPrivateLink实现集群间私网连接

为增强集群的安全性,同一个VPC或者两个不同VPC下的集群间网络隔离,需要通过NLBPrivateLink实现集群间私网(VPC)连接。

如下图所示,两个ES集群部署在同一个VPC下,借助用户VPC创建终端节点服务,然后通过在ES_1中配置集群间私网连接得到终端节点,最后将终端节点与终端节点服务相关联,实现两个集群间私网连接。

终端节点服务:终端节点服务是可以被其他VPC通过创建终端节点建立私网连接的服务,需要您手动创建相关的服务资源。
终端节点:终端节点与终端节点服务相关联,可以建立通过VPC私网访问外部服务的网络连接,阿里云ES配置实例私网连接将自动在ES所处的网络环境下创建终端节点。

88

配置步骤请参考通过NLBPrivateLink实现阿里云ES集群间私网互通,必须完成步骤一、步骤二和步骤三。

配置成功后界面如下:

image

配置Reindex API白名单

为确保跨集群数据迁移的安全性,需将ES_2集群的私网连接地址和通信端口号加入ES_1Reindex API白名单。

  1. 进入ES_1配置实例私网连接页面,单击目标终端节点ID

    image

  2. 终端节点连接页签,单击终端节点ID展开符图标,查看终端节点对应的域名。

    重要

    需要从域名中去掉可用区信息后,再配置Reindex API白名单。

    如完整域名为“ep-bp1****************-cn-hangzhou-i.epsrv-bp1****************.cn-hangzhou.privatelink.aliyuncs.com”,需去掉可用区信息,即去掉“-cn-hangzhou-i”,得到最终域名“ep-bp1bp1****************.epsrv-bp1****************.cn-hangzhou.privatelink.aliyuncs.com”。

    image

  3. ES_1YML文件中配置Reindex API白名单,白名单为终端节点的域名和通信端口。

    reindex:
      remote:
        whitelist: >-
          ep-bp1bp1****************.epsrv-bp1****************.cn-hangzhou.privatelink.aliyuncs.com:9200

    image

调用Reindex API迁移数据

  1. ES_1登录Kibana控制台

  2. Dev Tools>Console中调用Reindex API迁移数据。

    POST _reindex
    {
      "source": {
        "remote": {
          "host": "http://ep-bp1bp1****************.epsrv-bp1****************.cn-hangzhou.privatelink.aliyuncs.com:9200",
          "username": "elastic",
          "password": "xxx-xxxx123!"
        },
        "index": "product_info",
        "query": {
          "match": {
            "productName": "理财产品"
          }
        }
      },
      "dest": {
        "index": "dest"
      }
    }

    类别

    参数

    说明

    source

    remote

    表示远程集群,本示例为ES_2。

    host

    ES_2集群的访问地址,包含:

    • 访问协议:在集群基本信息页查看当前使用的访问协议。

      重要

      为确保安全性,请使用HTTPS协议,避免连接集群时以文本形式传输password。请参考HTTPS协议文档,启用HTTPS协议。

    • 域名:ES_2集群的私网连接地址,与配置到Reindex白名单中的域名一致。

    • 通信端口:固定为9200。

    username

    集群的默认用户名为elastic。

    password

    用户名对应的密码。

    密码在创建集群时设定,如密码已遗忘,可重置密码

    index

    远程集群中的源索引。

    query

    通过查询语法指定待迁移的数据。

    本示例中将ES_2集群索引中productName中带有“理财产品”的数据迁移到ES_1集群。

    dest

    index

    目标集群中用于存储迁移数据的索引。

    执行成功后,预期结果:

    {
      "took": 211,
      "timed_out": false,
      "total": 6,
      "updated": 6,
      "created": 0,
      "deleted": 0,
      "batches": 1,
      "version_conflicts": 0,
      "noops": 0,
      "retries": {
        "bulk": 0,
        "search": 0
      },
      "throttled_millis": 0,
      "requests_per_second": -1,
      "throttled_until_millis": 0,
      "failures": []
    }
  3. 调用_search API看迁移结果。

    GET dest/_search

    预期结果:

    {
      "took": 6,
      "timed_out": false,
      "_shards": {
        "total": 5,
        "successful": 5,
        "skipped": 0,
        "failed": 0
      },
      "hits": {
        "total": {
          "value": 6,
          "relation": "eq"
        },
        "max_score": 1,
        "hits": [
          {
            "_index": "dest",
            "_id": "n9kyqpcBCRuDZhswJCpH",
            "_score": 1,
            "_source": {
              "productName": "理财产品D",
              "annual_rate": "3.1200%",
              "describe": "90天定投产品,最低12000起投,每天收益到账消息推送"
            }
          },
          {
            "_index": "dest",
            "_id": "nNkyqpcBCRuDZhswJCpG",
            "_score": 1,
            "_source": {
              "productName": "理财产品A",
              "annual_rate": "3.2200%",
              "describe": "180天定期理财,最低20000起投,收益稳定,可以自助选择消息推送"
            }
          },
          {
            "_index": "dest",
            "_id": "ndkyqpcBCRuDZhswJCpG",
            "_score": 1,
            "_source": {
              "productName": "理财产品B",
              "annual_rate": "3.1100%",
              "describe": "90天定投产品,最低10000起投,每天收益到账消息推送"
            }
          },
          {
            "_index": "dest",
            "_id": "ntkyqpcBCRuDZhswJCpH",
            "_score": 1,
            "_source": {
              "productName": "理财产品C",
              "annual_rate": "3.3500%",
              "describe": "270天定投产品,最低40000起投,每天收益立即到账消息推送"
            }
          },
          {
            "_index": "dest",
            "_id": "oNkyqpcBCRuDZhswJCpH",
            "_score": 1,
            "_source": {
              "productName": "理财产品E",
              "annual_rate": "3.0100%",
              "describe": "30天定投产品推荐,最低8000起投,每天收益会消息推送"
            }
          },
          {
            "_index": "dest",
            "_id": "odkyqpcBCRuDZhswJCpH",
            "_score": 1,
            "_source": {
              "productName": "理财产品F",
              "annual_rate": "2.7500%",
              "describe": "热门短期产品,3天短期,无须任何手续费用,最低500起投,通过短信提示获取收益消息"
            }
          }
        ]
      }
    }

常见问题

在调用Reindex API时,可以根据待迁移文档大小调整单次文档读取上限和超时时间。

  • 调整单次文档读取上限

    系统默认单次读取文档的最大值为100 MB,如果索引中包含大文档,可调适当调小该值。

    以下示例中,通过size设置批量值为10,单位MB。

    POST _reindex
    {
      "source": {
        "remote": {
          "host": "http://otherhost:9200"
        },
        "index": "source",
        "size": 10,
        "query": {
          "match": {
            "test": "data"
          }
        }
      },
      "dest": {
        "index": "dest"
      }
    }
  • 调整超时时间

    socket_timeout用于设置socket读取超时时间,默认为30秒;connect_timeout用于设置集群连接超时时间,默认为1秒。

    以下示例中,设置socket读取超时时间为1分钟,连接超时时间为10秒。

    POST _reindex
    {
      "source": {
        "remote": {
          "host": "http://otherhost:9200",
          "socket_timeout": "1m",
          "connect_timeout": "10s"
        },
        "index": "source",
        "query": {
          "match": {
            "test": "data"
          }
        }
      },
      "dest": {
        "index": "dest"
      }
    }