李锋镝的博客

  • 首页
  • 时间轴
  • 留言
  • 插件
  • 左邻右舍
  • 关于我
    • 关于我
    • 另一个网站
  • 知识库
  • 赞助
Destiny
自是人生长恨水长东
  1. 首页
  2. 原创
  3. 正文

Elasticsearch零停机迁移数据

2019年12月11日 19158点热度 0人点赞 0条评论

创建索引

创建一个名为my_index的索引

PUT localhost:9200/my_index

创建映射

PUT localhost:9200/my_index/my_type/_mapping
{
    "properties": {
        "name": {
            "type": "text"
        }
    }
}

添加一个文档

PUT localhost:9200/my_index/my_type/2
{
    "name": "李锋镝"
}

查询:

GET localhost:9200/my_index/my_type/2
{
    "_index": "my_index",
    "_type": "my_type",
    "_id": "2",
    "_version": 1,
    "_seq_no": 0,
    "_primary_term": 1,
    "found": true,
    "_source": {
        "name": "李锋镝"
    }
}

可以发现文档所属的索引是my_index。

设置别名prod_index指向my_index

PUT localhost:9200/my_index/_alias/prod_index

检测别名是否设置成功

使用

GET localhost:9200/*/_alias/prod_index

或者

GET localhost:9200/my_index/_alias/*

如果成功就会都返回如下的结果:

{
    "my_index": {
        "aliases": {
            "prod_index": {}
        }
    }
}

说明别名设置成功。

使用别名查询文档:

GET localhost:9200/prod_index/my_type/2
{
    "_index": "my_index",
    "_type": "my_type",
    "_id": "2",
    "_version": 1,
    "_seq_no": 0,
    "_primary_term": 1,
    "found": true,
    "_source": {
        "name": "李锋镝"
    }
}

嗯...文档所属的索引同样是my_index。

然后可能过了一段时间我们发现my_index中的一个字段的映射不符合我们现今的需求,需要修改这个字段的映射。问题来了,我们不能修改现存的映射,所以我们必须重新索引数据。

重建索引

首先创建一个名为my_index1的索引

PUT localhost:9200/my_index1

创建映射

PUT localhost:9200/my_index1/my_type/_mapping
{
    "properties": {
        "name": {
            "type": "keyword"
        }
    }
}

将数据从my_index中索引到my_index1

POST localhost:9200/_reindex
{
  "source": {
    "index": "my_index"
  },
  "dest": {
    "index": "my_index1"
  }
}

操作成功,返回:

{
    "took": 1159,
    "timed_out": false,
    "total": 2,
    "updated": 0,
    "created": 2,
    "deleted": 0,
    "batches": 1,
    "version_conflicts": 0,
    "noops": 0,
    "retries": {
        "bulk": 0,
        "search": 0
    },
    "throttled_millis": 0,
    "requests_per_second": -1.0,
    "throttled_until_millis": 0,
    "failures": []
}

然后我们查询下数据,看下数据过来了没:

GET localhost:9200/my_index1/my_type/2
{
    "_index": "my_index1",
    "_type": "my_type",
    "_id": "2",
    "_version": 1,
    "_seq_no": 0,
    "_primary_term": 1,
    "found": true,
    "_source": {
        "name": "李锋镝"
    }
}

可以看到通过新的索引已经可以查询到my_index中的数据了。

接下来就是将别名prod_index指到my_index1上,因为一个别名可以指向多个索引,所以我们还需要删除my_index的别名。这个操作需要原子化,所以我们使用_aliases API来操作。

转移索引别名

POST localhost:9200/_aliases
{
    "actions": [
        {
            "remove": {
                "index": "my_index",
                "alias": "prod_index"
            }
        },
        {
            "add": {
                "index": "my_index1",
                "alias": "prod_index"
            }
        }
    ]
}

然后我们使用别名来查询一下我们的数据:

GET localhost:9200/prod_index/my_type/2
{
    "_index": "my_index1",
    "_type": "my_type",
    "_id": "2",
    "_version": 1,
    "_seq_no": 0,
    "_primary_term": 1,
    "found": true,
    "_source": {
        "name": "李锋镝"
    }
}

可以看到我们通过别名查询到的数据的索引已经是my_index1了。

OK,到了这里基本就大功告成,实现了在零停机的情况下从旧索引迁移到新索引了(前提是应用程序里边使用的是别名而不是索引名,这样你就可以在任何时候重建索引,而且别名的开销很小)。

其他

数据量太大,迁移时间很长怎么办?

_reindex API重建索引默认分页大小是1000,可以通过调整这个参数来增加效率。

POST localhost:9200/_reindex
{
  "source": {
    "index": "my_index",
    "size": 5000
  },
  "dest": {
    "index": "my_index1",
    "routing": "=cat"
  }
}

或者使用slices并行处理数据:

POST localhost:9200/_reindex?slices=5&refresh
{
  "source": {
    "index": "my_index"
  },
  "dest": {
    "index": "my_index1"
  }
}
除非注明,否则均为李锋镝的博客原创文章,转载必须以链接形式标明本文链接

本文链接:https://www.lifengdi.com/archives/article/1607

相关文章

  • SpringBoot整合Elasticsearch游标查询(scroll)
  • Elasticsearch的分布式文档存储原理
  • 解决Elasticsearch分页查询窗口数据太大问题
  • Elasticsearch常用查询
  • Elasticsearch中的多索引、多类型搜索
本作品采用 知识共享署名-非商业性使用-相同方式共享 4.0 国际许可协议 进行许可
标签: ElasticSearch 数据迁移
最后更新:2019年12月11日

李锋镝

既然选择了远方,便只顾风雨兼程。

打赏 点赞
< 上一篇
下一篇 >

文章评论

1 2 3 4 5 6 7 8 9 11 12 13 14 15 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 46 47 48 49 50 51 52 53 54 55 57 58 60 61 62 63 64 65 66 67 69 72 74 76 77 78 79 80 81 82 85 86 87 90 92 93 94 95 96 97 98 99
取消回复

去年今日此门中,人面桃花相映红。
人面不知何处去,桃花依旧笑春风。

最新 热点 随机
最新 热点 随机
SpringBoot框架自动配置之spring.factories和AutoConfiguration.imports 应用型负载均衡(ALB)和网络型负载均衡(NLB)区别 什么是Helm? TransmittableThreadLocal介绍与使用 ReentrantLock深度解析 RedisTemplate和Redisson的区别
玩博客的人是不是越来越少了?准备入手个亚太的ECS,友友们有什么建议吗?什么是Helm?2024年11月1号 农历十月初一别再背线程池的七大参数了,现在面试官都这么问URL地址末尾加不加“/”有什么区别
从SQL规范性检查、表结构索引检查着手分析如何优化SQL 我的第一个WordPress插件:Dylan Custom Plugin上线了 金融级JVM深度调优实战的经验和技巧 从零搭建Spring Cloud Gateway网关(二)—— 打印请求响应日志 张爱玲——《红玫瑰与白玫瑰》 数据库事务的一点简单总结
标签聚合
分布式 面试 文学 JAVA ElasticSearch JVM 架构 设计模式 日常 MySQL 数据库 Spring K8s 多线程 教程 IDEA SQL Redis SpringBoot docker
友情链接
  • i架构
  • 临窗旋墨
  • 博友圈
  • 博客录
  • 博客星球
  • 哥斯拉
  • 志文工作室
  • 搬砖日记
  • 旋律的博客
  • 旧时繁华
  • 林羽凡
  • 知向前端
  • 蜗牛工作室
  • 集博栈
  • 韩小韩博客
  • 風の声音

COPYRIGHT © 2025 lifengdi.com. ALL RIGHTS RESERVED.

Theme Kratos Made By Dylan

津ICP备2024022503号-3