环境信息
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
- 查看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
}
- 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
- 修改索引设置
如果某个索引数据量较大,可以先将目标索引副本设置为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
- 迁移有 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
网友评论