(转)Elasticsearch 的坑爹事——记录一次mapping field修改过程
Elasticsearch 的坑爹事
本文记录一次Elasticsearch mapping field修改过程
团队使用Elasticsearch做日志的分类检索分析服务,使用了类似如下的_mapping
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
{ "settings" : { "number_of_shards" : 20 }, "mappings" : { "client" : { "properties" : { "ip" : { "type" : "long" }, "cost" : { "type" : "long" }, } |
现在问题来了,日志中输出的"127.0.0.1"这类的IP地址在Elasticsearch中是不能转化为long的(报错Java.lang.NumberFormatException),所以我们必须将字段改为string型或者ip型(Elasticsearch支持,
数据类型可见mapping-core-types)才能达到理想的效果.
目标明确了,就是改掉mapping的ip的field type即可.
elasticsearch.org找了一圈 嘿嘿, update一下即可
1
2
3
4
5
6
7
8
|
curl -XPUT localhost:8301/store/client/_mapping -d ' { "client" : { "properties" : { "local_ip" : { "type" : "string" , "store" : "yes" } } } } |
报错结果
1
|
{ "error" : "MergeMappingException[Merge , "status" :400} |
尼玛 真逗 我long想转一下string 居然失败(elasticsearch产品层面理应支持这种无损转化) 无果
Google了一下类似的案例 (案例)
在一个帖子中得到的elasticsearch开发人员的准确答复
"You can't change existing mapping type, you need to create a new index with the correct mapping and index the data again."
想想 略坑啊 我不管是因为elasticsearch还是因为底层Lucene的原因,修改一个field需要对所有已有数据的所有field进行reindex,这本身就是一个逆天的思路,但是elasticsearch的研发人员还觉得这没有什么不合理的.
在Elasticsearch上游逛了一圈,上面这样写到
(http://www.elasticsearch.org/blog/changing-mapping-with-zero-downtime/)
the problem — why you can’t change mappings
You
can only find that which is stored in your index. In order to make your
data searchable, your database needs to know what type of data each
field contains and how it should be indexed. If you switch a field type
from e.g. a string to a date, all of the data for that field that you
already have indexed becomes useless. One way or another, you need to
reindex that field.
...
OK,这一段话很合理,我改了一个field的类型 需要对这个field进行reindex,如论哪种数据库都需要这么做,没错.
我们再继续往下看看,reindexing your data, 尼玛一看,弱爆了,他的reindexing your data不是对修改的filed进行reindex,而是创建了一个新的index,对所有的filed进行reindexing, 太逆天了。
吐槽归吐槽,这个事情逃不了,那我就按他的来吧.
首先创建一个新的索引
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
curl -XPUT localhost:8305/store_v2 -d ' { "settings" : { "number_of_shards" : 20 }, "mappings" : { "client" : { "properties" : { "ip" : { "type" : "string" }, "cost" : { "type" : "long" }, } |
等等,我创建了新索引,client往Elasticsearch的代码不会需要修改吧,瞅了一眼,有解决方案,建立一个alias(别名,和C++引用差不多),通过alias来实现对后面索引数据的解耦合,看到这,舒了一口气。
现在的问题是 这是一个线上服务,不能停服务,所以我需要一个倒数据到我的新索引的一个方案
Elasticsearch官网写到
pull
the documents in from your old index, using a scrolled search and index
them into the new index using the bulk API. Many of the client APIs
provide a reindex() method which will do all of this for you. Once you
are done, you can delete the old index.
第一句,看起来很美好,找了一圈,尼玛无图无真相,Google都没有例子,你让我怎么导数据?
第二句 client APIS, 看起来只有这个方法可搞了
python用起来比较熟,所以我就直接选 pyes了,装了一大堆破依赖库之后,终于可以run起来了
1
2
3
4
5
6
7
8
|
import pyes conn = pyes.es.ES( "http://10.xx.xx.xx:8305/" ) search = pyes.query.MatchAllQuery().search(bulk_read=1000) hits = conn.search(search, 'store_v1' , 'client' , scan=True, scroll= "30m" , model=lambda _,hit: hit) for hit in hits: #print hit conn.index(hit[ '_source' ], 'store_v2' , 'client' , hit[ '_id' ], bulk=True) conn.flush() |
花了大概一个多小时,新的索引基本和老索引数据一致了,对于线上完成瞬间的增量,这里没心思关注了,数据准确性要求没那么高,得过且过。
接下来修改alias别名的指向(如果你之前没有用alias来改mapping,纳尼就等着哭吧)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
curl -XPOST localhost:8305/_aliases -d ' { "actions" : [ { "remove" : { "alias" : "store" , "index" : "store_v1" }}, { "add" : { "alias" : "store" , "index" : "store_v2" }} ] } ' |
啷啷锵锵,正在追数据中
等新索引的数据已经追上时
将老的索引删掉
1
|
curl -XDELETE localhost:8303/store_v1 |
至此完成!
一件如此简单的事情,Elasticsearch居然能让他变得如此复杂,真是牛逼啊...
转自:http://www.cnblogs.com/Creator/p/3722408.html
(转)Elasticsearch 的坑爹事——记录一次mapping field修改过程的更多相关文章
- Elasticsearch 的坑爹事——记录一次mapping field修改过程
Elasticsearch 的坑爹事 本文记录一次Elasticsearch mapping field修改过程 团队使用Elasticsearch做日志的分类检索分析服务,使用了类似如下的_mapp ...
- Elasticsearch 的坑爹事——记录一次mapping field修改过程(转)
原文:http://www.cnblogs.com/Creator/p/3722408.html 本文记录一次Elasticsearch mapping field修改过程 团队使用Elasticse ...
- Elasticsearch学习总结 (Centos7下Elasticsearch集群部署记录)
一. ElasticSearch简单介绍 ElasticSearch是一个基于Lucene的搜索服务器.它提供了一个分布式多用户能力的全文搜索引擎,基于RESTful web接口.Elasticse ...
- 记录一次k8s环境尝试过程(初始方案,现在已经做过很多完善,例如普罗米修斯)
记录一次Team k8s环境搭建过程(初始方案,现在已经做过很多完善,例如普罗米修斯) span::selection, .CodeMirror-line > span > span::s ...
- 记录一些在用wcf的过程中走过的泥巴路 【第一篇】
自从转移战场之后,比以前忙多了,博客也没能及时跟上,原本准备继续mvc系列,但是在那边技术比较陈旧还没能用得上,话说有3年没接触这玩意了,东西也 都忘了差不多了,既然再次接触,我也就继续温习温习,记录 ...
- ElasticSearch reindex报错:the final mapping would have more than 1 type
ElasticSearch reindex报错:the final mapping would have more than 1 type 学习了:https://blog.csdn.net/qq_2 ...
- 记录一次追踪@AutoWired的过程
目录 记录一次追踪@AutoWired的过程 前言 疑惑:依赖究竟是怎么自动注入的 AutoWiredAnnotationBeanPostProcessor中探究 自动注入debug流程追踪 dete ...
- 关于Design Complier/Library Compiler的跌坑(坑爹)记录
最近需要用DC做一些事,然后需要转库,中午偷个闲,特来记录一下中间的一些坎坷. 1.首先是要转库.我们只有.lib文件的格式,所以需要把.lib文件转换成.db格式.然后坑来了!!!DC2015及以后 ...
- hazelcast的坑爹事
转载自 http://blog.csdn.net/hengyunabc/article/details/18514563 简介 开源中国的简介: Hazelcast是一个高度可扩展的数据分发和集群平台 ...
随机推荐
- iframe内点击a标签禁止滚动到顶部
在iframe内加载的表中,操作下的按钮用a标签布局,但是会出现一个非常不好的体验,就是当页面有滚动条的时候,点击a标签,列表会自动滚动到顶部. 首先看我的a标签: <a href=" ...
- LeetCode 41 First Missing Positive(找到数组中第一个丢失的正数)
题目链接: https://leetcode.com/problems/first-missing-positive/?tab=Description 给出一个未排序的数组,求出第一个丢失的正数. ...
- php 函数合并 array_merge 与 + 的区别
array_merge()是PHP语言中的一个函数,作用是将两个或多个数组的单元合并起来,一个数组中的值附加在前一个数组的后面.返回作为结果的数组. 如果输入的数组中有相同的字符串键名,该键的键值为最 ...
- vue生成路由实例
一.vue路由https://router.vuejs.org/zh-cn/1.bower下载vue-routervue的里的链接 <router-link to="/home&quo ...
- Laya 位图字体制作(失败...)
参考: 官网教程-位图字体的制作与使用 一.下载字体并安装字体 从站长字体下载了液晶数字字体,将TTF文件拖入C盘windows/Font文件夹,则字体会自动安装 二.下载字体制作工具 Bitmap ...
- Ubuntu 16.04系统下apt-get和dpkg区别
两者的区别是dpkg绕过apt包管理数据库对软件包进行操作,所以你用dpkg安装过的软件包用apt可以再安装一遍,系统不知道之前安装过了,将会覆盖之前dpkg的安装.1.dpkg是用来安装.deb文件 ...
- 关于JavaScript转义字符('、 " 、\" 、\')【原创】
先插入一条广告,博主新开了一家淘宝店,经营自己纯手工做的发饰,新店开业,只为信誉!需要的亲们可以光顾一下!谢谢大家的支持!店名: 小鱼尼莫手工饰品店经营: 发饰.头花.发夹.耳环等(手工制作)网店: ...
- Spring Framework框架容器核心源码逐步剖析
目录 构建Spring环境 Spring 版本 5.1.3.RELEASE 测试类 Spring 配置文件 测试方法Main 快速进入Debug查看IOC容器构建源码 Spring IOC源码步骤分析 ...
- 洞察行业领先者的前沿思想——第五届TOP100全球软件案例研究峰会精彩谢幕
(第五届TOP100summit开幕式现场) 12月09日-12日,由msup主办的第五届TOP100全球软件案例研究峰会(以下简称TOP100summit)在北京国家会议中心举行,作为互联网行业最有 ...
- POJ 2398 - Toy Storage - [计算几何基础题][同POJ2318]
题目链接:http://poj.org/problem?id=2398 Time Limit: 1000MS Memory Limit: 65536K Description Mom and dad ...