- 介绍
- 版本说明
- 首先就需要定义生命周期
- 定义索引的模版
- 这里需要手动设置一下第一创建的索引,目的是为了设置is_write_index属性
- 修改ES的定期检测时间
- 插入数据
- 通过索引数据
- 通过别名查看索引定义
- 查看索引滚动计划
- 对已存在的索引设置生命周期
- 参考文献

当需要存储的数据量特别大的时候,我们会希望ES能够通过预先设定的阈值,去自动创建索引,并将过期的索引删除掉。这里就需要用到ES的生命周期。
ES的生命周期分为4个阶段:HOT->WARM->COLD->DELETE。
elasticsearch version 7.9.3
kibana version 7.9.3
其中的 HOT 阶段是必须配置的,其他三个阶段可选。rollover下包含三个属性,可以从三个维度控制文档的滚动阈值。只要达到其中的任意一个条件及会生成新的索引
# DELETE _ilm/policy/message_policy
PUT _ilm/policy/message_policy
{"policy": {"phases": {  "hot": {"actions": {  "rollover": {"max_docs" : 10, # 当索引的中文档数达到10时,触发滚动策略
			"max_size": "30GB", # 当索引的数据容量达到30GB时,触发滚动策略
			"max_age": "1d" # 每天生成一个新的索引
          }
        }
      }, 
      "delete": {"min_age": "5s", # 当索引的创建时间超过5s,索引就会被自动清理掉
        "actions": {  "delete": {}
        }
      }
    }
  }
}我们可以通过定义模版,将匹配正则表达式message_info-*的所有新创建的索引设置成模板中设定的属性。还需要将我们之前定义好的生命周期message_policy设置到模版中index.lifecycle.name属性中。index.lifecycle.rollover_alias属性的含义是,以后通过生命周期管理新创建出来的索引,都可以通过这个属性中配置的别名message_alias访问到。
# DELETE _template/message_template
PUT _template/message_template
{"order": 1,
  "index_patterns": [
    "message_info-*"
  ],
  "settings": {"number_of_shards": 3,
    "number_of_replicas": 1,
    "index.lifecycle.name": "message_policy", 
    "index.lifecycle.rollover_alias": "message_alias"
  },
  "mappings": {"properties": {  "deviceId": {"type": "keyword"
      },
      "messageId": {"type": "keyword"
      }
    }
  }
}is_write_index:表示能够通过别名插入数据,默认情况下是不能通过别名插入的
这样的话,以后滚动生成的索引都会包含这个属性,这样就可以通过别名往新的索引中写入数据。%3Cmessage_info_%7Bnow%2Fd%7D-000001%3E这个配置是想创建的索引名称中包含日期,穿件出来的索引就是这个样子的message_info_2022.12.16-000001。
需要注意一点是,索引的名字必须是以横杠(-)加 数字结尾,也就是索引的名字必须匹配^.*-\d+$这个正则表达式。否则通过索引的生命周期是无法创建新的索引的,ES在滚动创建索引的时候会报错。
# DELETE /message_info-*
PUT %3Cmessage_info_%7Bnow%2Fd%7D-000001%3E
{"aliases": {"message_alias": {  "is_write_index": true 
    }
  }
}ES会根据设置时间定期检测,默认为10分钟,为了更快的看到效果,可以改为10秒。
PUT /_cluster/settings
{"transient": {"indices.lifecycle.poll_interval": "10s" 
  }
}POST /message_alias/_bulk
{"create":{"_type":"_doc"}}
{"deviceId":"张三","messageId":"1212"}通过别名查看
GET /message_alias/_search主要关注setting中的生命周期的配置是否正确
GET /message_alias{"message_info_2022.12.16-000005" : {"aliases" : {  "message_alias" : {"is_write_index" : true
      }
    },
    "mappings" : {  "properties" : {"deviceId" : {  "type" : "keyword"
        },
        "messageId" : {  "type" : "keyword"
        }
      }
    },
    "settings" : {  "index" : {"lifecycle" : {  "name" : "message_policy",
          "rollover_alias" : "message_alias"
        },
        "number_of_shards" : "3",
        "provided_name" : "",
        "creation_date" : "1671157454338",
        "number_of_replicas" : "1",
        "uuid" : "snU_zflXTOW7CjqWqd63ug",
        "version" : {  "created" : "7090399"
        }
      }
    }
  }
}
 可以看到索引的生命周期信息
GET message_info-*/_ilm/explain{"indices" : {"message_info_2022.12.16-000005" : {  "index" : "message_info_2022.12.16-000005",
      "managed" : true,
      "policy" : "message_policy",
      "lifecycle_date_millis" : 1671157454338,
      "age" : "16.13m",
      "phase" : "hot",
      "phase_time_millis" : 1671157454438,
      "action" : "rollover",
      "action_time_millis" : 1671157464410,
      "step" : "check-rollover-ready",
      "step_time_millis" : 1671157464410,
      "phase_execution" : {"policy" : "message_policy",
        "phase_definition" : {  "min_age" : "0ms",
          "actions" : {"rollover" : {  "max_size" : "30gb",
              "max_age" : "1d",
              "max_docs" : 10
            }
          }
        },
        "version" : 4,
        "modified_date_in_millis" : 1671111430428
      }
    }
  }
}
PUT jidu_iot_message_handle_device_message_info_*/_settings
{"index": {"lifecycle": {  "name": "jidu_iot_message_handle_device_message_policy"
    }
  }
}https://www.elastic.co/guide/en/elasticsearch/reference/7.9/set-up-lifecycle-policy.html#ilm-create-policy
https://www.elastic.co/guide/en/elasticsearch/reference/master/indices-rollover-index.html#roll-over-index-alias-with-write-index
你是否还在寻找稳定的海外服务器提供商?创新互联www.cdcxhl.cn海外机房具备T级流量清洗系统配攻击溯源,准确流量调度确保服务器高可用性,企业级服务器适合批量采购,新人活动首月15元起,快前往官网查看详情吧
当前名称:elasticsearch的生命周期管理-创新互联
分享URL:http://www.cqwzjz.cn/article/dsopph.html

 建站
建站
 咨询
咨询 售后
售后
 建站咨询
建站咨询 
 