美文网首页
【es】从es5 reindex数据到es7

【es】从es5 reindex数据到es7

作者: 放纵不基 | 来源:发表于2022-11-06 10:59 被阅读0次

环境信息

es5集群: xx.xx.xx.xx:9200,xx.xx.xx.xx:9200,xx.xx.xx.xx:9200
es7集群: xx.xx.xx.xx:9211,xx.xx.xx.xx:9211,xx.xx.xx.xx:9211

将 es5集群地址加入到 es7集群的白名单配置中:

$ cat  /path/to/es7/config/elasticsearch.yml 
reindex.remote.whitelist: xx.xx.xx.xx:9200,xx.xx.xx.xx:9200,xx.xx.xx.xx:9200
  1. 查看es7集群状态
$ curl  http://127.0.0.1:9211/_cat/indices
$ curl --user  username:password  "http://xx.xx.xx.xx:9211/_cluster/health?pretty"

{
  "cluster_name" : "test",
  "status" : "green",
  "timed_out" : false,
  "number_of_nodes" : 3,
  "number_of_data_nodes" : 3,
  "active_primary_shards" : 4,
  "active_shards" : 11,
  "relocating_shards" : 0,
  "initializing_shards" : 0,
  "unassigned_shards" : 0,
  "delayed_unassigned_shards" : 0,
  "number_of_pending_tasks" : 0,
  "number_of_in_flight_fetch" : 0,
  "task_max_waiting_in_queue_millis" : 0,
  "active_shards_percent_as_number" : 100.0
}
  1. reindex前,先提前在新集群创建好目标业务索引

为什么这很很重要?
因为一开始,在备份数据的场景下,以为dest index不用创建,最后测试发现如果dest index不创建,对于非text类
型数据,可能会变成text类型(不建议这样做,所以最好创建好dest index),而且默认为 1主分片1副本。

3.查看原es5集群中哪些索引有 "__routing" 字段

$ cat check_routing.sh

#!/bin/bash
 
indices=`curl -s http://127.0.0.1:9200/_cat/indices | awk '{print $3}'`
 
for i in ${indices}
do
  if  curl  -s  -X GET http://127.0.0.1:9211/${i}/doc/_search?pretty  -H 'content-type: application/json' -d '{ "size": 1, "query": {"match_all": {}}}' | grep "routing"  2> /dev/null; then
    echo  "$i"
  fi
done
  1. 修改索引设置

如果某个索引数据量较大,可以先将目标索引副本设置为0,并暂时关闭 refresh ,可以加快迁移速度
当数据添加到索引后并不能马上被查询到,等到索引刷新后才会被查询到,refresh_interval 配置的刷新间隔,默认为1s

curl -XPUT 'http://xx.xx.xx.xx:9211/XXX/_settings' -d '{
    "index" : {
               "number_of_replicas": 0,
               "refresh_interval": -1
    }
}'

索引 reindex 完成后,恢复原索引设置:

curl -XPUT  'http://xx.xx.xx.xx:9211/XXX/_settings' -d '{
    "index" : {
               "number_of_replicas": 1,
              "refresh_interval": "1s"
    }
}'

5.迁移 无 routing 索引

$ cat reindex_no_routing.sh

#!/bin/bash
indexName="XXX"
newClusterUser="username"
newClusterPass="password"
newClusterHost="xx.xx.xx.xx:9211"
oldClusterUser=""
oldClusterPass=""
# 旧集群host必须是[scheme]://[host]:[port],例如http://xx.xx.xx.xx:9200
oldClusterHost="http://xx.xx.xx.xx:9200"
curl -u ${newClusterUser}:${newClusterPass} -XPOST "http://${newClusterHost}/_reindex?pretty" -H "Content-Type: application/json" -d'{
    "source": {
        "size": 5000, 
        "remote": {
            "host": "'${oldClusterHost}'",
            "username": "'${oldClusterUser}'",
            "password": "'${oldClusterPass}'"
        },
        "index": "'${indexName}'",
        "query": {
            "match_all": {}
        }
    },
    "dest": {
       "index": "'${indexName}'"
    }
}'

注: 每次修改 indexName的值,然后执行脚本,一个个索引 reindex

  1. 迁移有 routing 索引

$ cat reindex_using_routing.sh

#!/bin/bash
indexName="XXX"
rounting="XXX"
newClusterUser="username"
newClusterPass="password"
newClusterHost="xx.xx.xx.xx:9211"
oldClusterUser=""
oldClusterPass=""
# 旧集群host必须是[scheme]://[host]:[port],例如http://xx.xx.xx.xx:9200
oldClusterHost="http://xx.xx.xx.xx:9200"
curl -u ${newClusterUser}:${newClusterPass} -XPOST "http://${newClusterHost}/_reindex?pretty" -H "Content-Type: application/json" -d'{
    "source": {
        "size": 5000,
        "remote": {
            "host": "'${oldClusterHost}'",
            "username": "'${oldClusterUser}'",
            "password": "'${oldClusterPass}'"
        },
        "index": "'${indexName}'",
        "query": {
            "match_all": {}
        }
    },
    "dest": {
       "index": "'${indexName}'"
    },
    "script": {
    "inline": "ctx._routing = ctx._source.'${rounting}'",
    "lang": "painless"
  }
 
}'

注: 每次修改 indexName值和对应 rounting 值,然后执行脚本,一个个索引 reindex

查看索引中数据是否带 "__routing" 字段:

$ curl  -X GET --user  username:password   http://127.0.0.1:9211/XXX/_search?pretty  -H  'content-type: application/json'  -d   '{ "size": 1, "query": {"match_all": {}}}'
 
{
  "took" : 4,
  "timed_out" : false,
  "_shards" : {
    "total" : 6,
    "successful" : 6,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 5627,
      "relation" : "eq"
    },
    "max_score" : 1.0,
    "hits" : [
      {
        "_index" : "XXX",
        "_type" : "_doc",
        "_id" : "5860ba83e4b0ec7cf3a9f587_5704c34ae4b0cbf7756725a0",
        "_score" : 1.0,
        "_routing" : "5860ba83e4b0ec7cf3a9f587",
        "_source" : {
          "recvUserName" : "Bogon",
          "chatCount" : 1,
          "receiver" : "5704c34ae4b0cbf7756725a0",
          "sender" : "5860ba83e4b0ec7cf3a9f587",
          "id" : "5860ba83e4b0ec7cf3a9f587_5704c34ae4b0cbf7756725a0",
          "todayChatCount" : 1,
          "sendTime" : "2021-05-31T00:00:00Z"
        }
      }
    ]
  }
}

从输出中可以看到 "_routing" : "5860ba83e4b0ec7cf3a9f587"

参考

es5中文文档
http://doc.codingdict.com/elasticsearch

es5索引mapping的写入、查看与修改
https://www.cnblogs.com/sandea/p/10557125.html

es7索引基本操作之查看、创建、修改和删除
https://www.codenong.com/cs106251120

从底层存储结构带你分析Elasticsearch7.x为什么把type给干掉了?
https://blog.51cto.com/u_12132623/3027241
https://www.elastic.co/guide/en/elasticsearch/reference/current/removal-of-types.html#_why_are_mapping_types_being_removed

相关文章

  • 【es】从es5 reindex数据到es7

    环境信息 es5集群: xx.xx.xx.xx:9200,xx.xx.xx.xx:9200,xx.xx.xx....

  • 从技术看世界

    从2015年到今天,前端技术不断在进步。从angular到react到vue,从es5到es6到es7,都可以看...

  • JS

    1.JS的作用 2.ECMAScript(标准、核心部分) ES3 ES5 ES6 ES7 3.JavaScrip...

  • ES:reindex中的坑

    吐槽一个ES-reindex迁移的大坑 在做ES跨集群迁移的时候,用到了ES的reindex进行数据迁移,查了很多...

  • Babel解析原理剖析

    背景 Javascript属于web编程语言,本身语法设计存在缺陷,随着ES5、ES6、ES7等各个版本的推出,语...

  • typescript学习笔记

      随着前端的发展,js越来越火,但是js有许多不合理或者说不完美的地方   尽管从es5,es6,es7等版本升...

  • ES6第五课、async-await入门

    关于异步处理,ES5的回调使我们陷入地狱,ES6的Promise使我们脱离魔障,终于、ES7的async-awai...

  • webpack4-babel转换

    目前浏览器可识别的js是es5部分可识别es6所以为了兼容大部分浏览器,需要将es6 es7 es8转换成es5那...

  • 关于 ECMASCRIPT 发展史和现状

    在阅读 JavaScript 相关内容时,不可避免地会看到以下其中一些术语: ES5,ES6,ES7,ES2015...

  • 2017/05/09

    今天要记录的是如何使用babel将es6和es7转化为es5,使用yarn而不是npm,因为yarn的速度更快,下...

网友评论

      本文标题:【es】从es5 reindex数据到es7

      本文链接:https://www.haomeiwen.com/subject/ubamtdtx.html