• 66.零停机下reindex


    主要知识点:

    • 理解reindex的使用场景和必要性
    • 学会reindex

       

       

    一、理解reindex的使用场景和必要性

    假设:在某一个index中依靠dynamic mapping插入数据,但是不小心有些数据是2017-01-01这种日期格式的,所以title这个field被插入2017-01-01这条数据之后就被es自动映射为了date类型,实际上它应该是string类型的。如果后面有"hello word"这个格式的数据插入时就会报错,在这种情况下,是不能修改原index下的mapping的,只能是重建正确的索引,然后把原索引的数据放入新索引中。具体做法如下:

    因为一个field的设置是不能被修改的,如果要修改一个Field,那么应该重新按照新的mapping,建立一个index,然后将数据批量查询出来,重新用bulk api写入index中。批量查询的时候,建议采用scroll api,并且采用多线程并发的方式来reindex数据,每次scoll就查询指定日期的一段数据,交给一个线程即可。

       

    二、零停机下reindex实验

    1、插入格式错误的数据

       

    PUT /my_index/my_type/3

    {

    "title": "2017-01-03"

    }

       

    查看fieldmappings

    语句:GET /my_index/_mapping/my_type

    执行结果:

    {

    "my_index": {

    "mappings": {

    "my_type": {

    "properties": {

    "title": {

    "type": "date"

    }

    }

    }

    }

    }

    }

       

    2、插入string类型的值

    当后期向索引中加入string类型的title值的时候,就会报错

       

    PUT /my_index/my_type/4

    {

    "title": "my first article"

    }

       

    {

    "error": {

    "root_cause": [

    {

    "type": "mapper_parsing_exception",

    "reason": "failed to parse [title]"

    }

    ],

    "type": "mapper_parsing_exception",

    "reason": "failed to parse [title]",

    "caused_by": {

    "type": "illegal_argument_exception",

    "reason": "Invalid format: "my first article""

    }

    },

    "status": 400

    }

       

    3、测试修改titlemapping类型

    如果此时想修改title的类型,是不可能的

       

    PUT /my_index/_mapping/my_type

    {

    "properties": {

    "title": {

    "type": "text"

    }

    }

    }

    执行结果

    {

    "error": {

    "root_cause": [

    {

    "type": "illegal_argument_exception",

    "reason": "mapper [title] of different type, current_type [date], merged_type [text]"

    }

    ],

    "type": "illegal_argument_exception",

    "reason": "mapper [title] of different type, current_type [date], merged_type [text]"

    },

    "status": 400

    }

       

    4、把原index使用别名

    此时,唯一的办法,就是进行reindex,也就是说重新建立一个索引,将旧索引的数据查询出来,再导入新索引。如果说旧索引的名字是old_index,新索引的名字是new_index,终端javapython应用,已经在使用old_index在操作了,f如果停止终端应用,修改使用的indexnew_index,再重新启动终端应用,这个过程就会导致终端应用停机,使es可用性降低。解决办法就是,给终端应用一个别名,这个别名是指向旧索引的,终端应用先用着这个别名,例如:终端应用先用goods_index alias来进行es操作,此时实际指向的是旧的my_index。语法如下:

       

    PUT /my_index/_alias/goods_index

       

    5、新建一个index,调整其title的类型为string

       

    PUT /my_index_new

    {

    "mappings": {

    "my_type": {

    "properties": {

    "title": {

    "type": "text"

    }

    }

    }

    }

    }

       

    6、使用scroll api将数据批量查询出来

       

    GET /my_index/_search?scroll=1m

    {

    "query": {

    "match_all": {}

    },

    "sort": ["_doc"],

    "size": 1

    }

    执行结果(部分):

    {

    "_scroll_id": "DnF1ZXJ5VGhlbkZldGNoBQAAAAAAADpAFjRvbnNUWVZaVGpHdklqOV9zcFd6MncAAAAAAAA6QRY0b25zVFlWWlRqR3ZJajlfc3BXejJ3AAAAAAAAOkIWNG9uc1RZVlpUakd2SWo5X3NwV3oydwAAAAAAADpDFjRvbnNUWVZaVGpHdklqOV9zcFd6MncAAAAAAAA6RBY0b25zVFlWWlRqR3ZJajlfc3BXejJ3",

    "took": 1,

    "timed_out": false,

    "_shards": {

    "total": 5,

    "successful": 5,

    "failed": 0

    },

     

    7、采用bulk apiscoll查出来的一批数据,批量写入新索引

       

    POST /_bulk

    { "index": { "_index": "my_index_new", "_type": "my_type", "_id": "2" }}

    { "title": "2017-01-02" }

       

    8、反复循环6~7步骤

    查询一批又一批的数据出来,采取bulk api将每一批数据批量写入新索引中。

       

    9、切换别名

    goods_index alias切换到my_index_new上去,终端应用会直接通过index别名使用新的索引中的数据,java应用程序不需要停机,从而达到零停机高可用的目的。

       

    POST /_aliases

    {

    "actions": [

    { "remove": { "index": "my_index", "alias": "goods_index" }},

    { "add": { "index": "my_index_new", "alias": "goods_index" }}

    ]

    }

       

    10、验证别名

    直接通过goods_index别名来查询,是否能进行正确的查询。

       

    GET /goods_index/my_type/_search

  • 相关阅读:
    斜率dp+cdq分治
    踢罐子 [几何+乱搞]
    [HDU3710] Battle Over Cities [树链剖分+线段树+并查集+kruskal+思维]
    [xsy1129] flow [树链剖分和线段树一起优化网络流][我也不知道这是什么鬼标签]
    [CF666E] Forensic Examination [广义后缀自动机+线段树合并]
    省选算法学习-BSGS与exBSGS与二次剩余
    省选算法学习-回文自动机 && 回文树
    省选算法学习-后缀数组+后缀自动机+后缀树
    Password [分块]
    随机过程——泊松过程
  • 原文地址:https://www.cnblogs.com/liuqianli/p/8476897.html
Copyright © 2020-2023  润新知