当前位置:主页 > 查看内容

Rollover API

发布时间:2021-05-17 00:00| 位朋友查看

简介:如果当前的写数据索引满足条件集中的任何一个条件;则滚动目标将创建一个新的backing索引my-data-stream-000002,且新的backing索引将作为写入数据的索引。 上面代码块的 Rollover API 返回值如下: { "acknowledged" : false, "shards_acknowledged" : fals……

如果当前的写数据索引满足条件集中的任何一个条件;则滚动目标将创建一个新的backing索引my-data-stream-000002,且新的backing索引将作为写入数据的索引。

上面代码块的 Rollover API 返回值如下:

{
 "acknowledged" : false,
 "shards_acknowledged" : false,
 "old_index" : ".ds-my-data-stream-000001",#数据流之前对应的写数据索引
 "new_index" : ".ds-my-data-stream-000002",,#数据流新对应的写数据索引
 "rolled_over" : true,#索引是否执行滚动
 "dry_run" : false, # 是否为 dry_run 模式
 "conditions" : { # 滚动条件集,集触发情况
 "[max_size: 5gb]" : false,
 "[max_docs: 2]" : true,
 "[max_age: 7d]" : false
}

再次查看数据流

{
 "data_streams" : [
 "name" : "my-data-stream", # 数据流名称
 "timestamp_field" : {
 "name" : "@timestamp"
 "indices" : [
 # backing索引
 "index_name" : ".ds-my-data-stream-000001",
 "index_uuid" : "Vir6yRm4S42k8n22mZ5YBw"
 "index_name" : ".ds-my-data-stream-000002",
 "index_uuid" : "WcctdbIqSMqk3MmefRdfDQ"
 "generation" : 2, #当前是那一个版本的后备索引作为写索引
 "status" : "GREEN",
 "template" : "my-index-template", # 适配的索引模板
 "ilm_policy" : "my-lifecycle-policy"

也可以通过 Kibana 进行查看

目标索引设定 setting

新索引的setting、mapping和aliases可以取自索引名匹配的任何索引模板。如果 rollover-target 为索引别名,你可以在 rollover API的请求体中指定settings, mappings和aliases,与创建索引的API(create index API)类似。如果请求体指定的值优先于匹配的索引模板。

如下示例重写了 index.number_of_shards setting:

PUT /logs-000001
 "aliases": {
 "logs_write": {}
POST /logs_write/_rollover
 "conditions" : {
 "max_age": "7d",
 "max_docs": 2,
 "max_size": "5gb"
 "settings": {
 "index.number_of_shards": 2
}
指定目标索引名称

如果 rollover-target 是索引别名,并且绑定的索引名称以- number 结尾;如果不指定目标索引,发生滚动时生成的新索引名称为 indexName-6 位数字,每滚动一次数字自增一次。

例如索引为logs-1(或者logs-000001),执行滚动后新的索引名字为logs-000002,6位数字低位自增高位补 0。

如果旧的索引名称不满足indexName- number 结尾,则执行滚动必须指定新索引名字;

示例如下:

# my_alias绑定的索引索引名称不满足indexName- number 格式
#必须指定索引my_new_index_name
POST /my_alias/_rollover/my_new_index_name
 "conditions": {
 "max_age": "7d",
 "max_docs": 2,
 "max_size": "5gb"
}
基于 date math 滚动

如果 rollover-target 是索引别名,使用 date math 根据索引滚动的当前日期来命名滚动索引;在某些场景中是非常有用的,比如定时按天删除索引、按天查询数据等。rollover API 支持 date math,但是要求索引名称必须以日期- number 结尾,

例如:logstash-2021.05.06-1;以这种格式命名的主要是方便在任何时候执行滚动,索引名称自增。

示例如下:

# PUT / logs-{now/d}-1 with URI encoding:
PUT /%3Clogs_%7Bnow%2Fd%7D-1%3E #备注1 
 "aliases": {
 "logs_write": {}
PUT logs_write/_doc/1
 "message": "a dummy log"
POST logs_write/_refresh
# 立即执行 _rollover 
POST /logs_write/_rollover #备注2
 "conditions": {
 "max_docs": 1,
 "max_age": "1d",
 "max_size": "5gb"
}

创建一个索引取今天的日期,logs_2021.05.06-1执行滚动操作,如果立即执行,则新索引名为logs_2021.05.06-000002;如果等到24小时之后执行索引名称为logs_2021.05.07-000002

你可以按照 date math 文档的说明去使用这些索引。

例如,你需要搜索过去三天索引的数据,你可以按照如下的书写方式:

# GET / logs-{now/d}-* , logs-{now/d-1d}-* , logs-{now/d-2d}-* /_search
GET /%3Clogs-%7Bnow%2Fd%7D-*%3E%2C%3Clogs-%7Bnow%2Fd-1d%7D-*%3E%2C%3Clogs-%7Bnow%2Fd-2d%7D-*%3E/_search
Dry run

rollover API支持dry_run模式,该模式主要用于条件检测。比如想检测索引是否满足设定的条件完成滚动。

示例如下:

POST /logs_write/_rollover?dry_run
 "conditions" : {
 "max_age": "7d",
 "max_docs": 1000,
 "max_size": "5gb"
}

返回值如下:

{
 "acknowledged" : false,
 "shards_acknowledged" : false,
 "old_index" : "logs-000002", #当前索引
 "new_index" : "logs-000003",# 将要新生成的索引
 "rolled_over" : false, #是否完成滚动
 "dry_run" : true, # 是否为检测模式
 "conditions" : {
 "[max_size: 5gb]" : false,
 "[max_docs: 1000]" : false,
 "[max_age: 7d]" : false
}
基于 write index 滚动

基于write index滚动主要是为了解决,使用别名查询能够获取到之前创建的索引数据,以及同一别名绑定多个索引滚动切换歧义问题。同一个别名具有查询和写入两种特性。

示例如下

#创建一个索引,绑定别名 logs,并标注 is_write_index 为 true,即通过别名写入数据,实际是写入到 my_logs_index-000001 表中
PUT my_logs_index-000001
 "aliases": {
 "logs": { "is_write_index": true } 
#写入一个文档
PUT logs/_doc/1
 "message": "a dummy log"
#刷新,生成一个 segements,使文档变得可搜索
POST logs/_refresh
#执行滚动操作
POST /logs/_rollover #备注1
 "aliases": {"search_all": {}}, #备注2
 "conditions": {
 "max_docs": "1"
#再次基于别名写入
PUT logs/_doc/2 
 "message": "a newer log"
}

执行滚动操作write index切换成为新创建的索引,之后写入的数据将写入到新索引中,滚动时新产生的索引添加别名 search_all

上述代码块执行_rollover的返回值如下

{
 "_index" : "my_logs_index-000002",
 "_type" : "_doc",
 "_id" : "2",
 "_version" : 1,
 "result" : "created",
 "_shards" : {
 "total" : 2,
 "successful" : 1,
 "failed" : 0
 "_seq_no" : 0,
 "_primary_term" : 1
}

在完成滚动之后,GET _alias/logs,查看别名元信息,可以发现新索引已经作为write index。

{
 "my_logs_index-000002": {
 "aliases": {
 "logs": { "is_write_index": true }
 "my_logs_index-000001": {
 "aliases": {
 "logs": { "is_write_index" : false }
基于 ILM 实现自动 Rollover

首先在 Kibana ,定义生命周期策略(也可通过 API 方式);

下图规定了 Hot 阶段重新生成索引的规则,这部分功能其实就是对于rollover API的封装;下图中我们定义了一个名称为rollover-test-all的生命周期管理策略,其中滚动条件为max_size,为 20gb,max_age为 1 天,max_docs为 2;

我们可以通过 API,查看GET _ilm/policy/rollover-test-all。返回值如下:

{
 "rollover-test-all" : {
 "version" : 1,
 "modified_date" : "2021-05-11T15:23:27.155Z",
 "policy" : {
 "phases" : {
 "hot" : {
 "min_age" : "0ms",
 "actions" : {
 "rollover" : {
 "max_size" : "20gb",
 "max_age" : "1d",
 "max_docs" : 2
 "set_priority" : {
 "priority" : 100
}

创建一个模板,模版中引用刚才定义的生命周期策略,并指定滚动的别名。

关于索引模板更多信息请参看 Simulate multi-component templates
# 这个是 legacy 的 index template,适用于 7.8 版本以前,
# 但在7.11版本,仍然可以用
PUT _template/iml-rollover_template
 "index_patterns": [
 "iml-rollover*"
 "aliases": {
 "iml-rollover_alias": {}
 "settings": {
 "index": {
 "lifecycle": {
 "name": "rollover-test-all",
 "rollover_alias" : "iml-rollover_write_alias"
 "refresh_interval": "2s",
 "number_of_shards": "1",
 "number_of_replicas": "0"
 "mappings": {
 "properties": {
 "name": {
 "type": "keyword"
# 7.8 版本之后,可以使用 _index_template 定义模板;
PUT _index_template/iml-rollover_template
 "index_patterns": ["iml-rollover*"],
 "template": {
 "settings": {
 "lifecycle": {
 "name": "rollover-test-all",
 "rollover_alias" : "iml-rollover_write_alias"
 "refresh_interval": "2s",
 "number_of_shards": "1",
 "number_of_replicas": "0"
 "mappings": {
 "_source": {
 "enabled": true
 "properties": {
 "name": {
 "type": "keyword"
 "aliases": {
 "iml-rollover_alias": { }
 "priority": 500,
 "_meta": {
 "description": "my custom rollover test"

创建索引

PUT iml-rollover-000001
 "aliases": {
 "iml-rollover_write_alias": { "is_write_index": true }
}

插入数据

PUT iml-rollover_write_alias/_bulk?refresh=true
{"index":{}}
{"name":"kimchy"}
{"index":{}}
{"name":"tom"}

查看索引情况GET _cat/indices/iml-rollover-*?v h=health,index,docs.count。

结果如下

health index docs.count
green iml-rollover-000001 2
green iml-rollover-000002 0

iml-rollover-000001 的文档个数为 2,并且 iml-rollover-000002 索引已经被创建。查看别名的关联关系GET _cat/aliases/iml-rollover_*?format=json。索引 iml-rollover-000002 的别名 iml-rollover_write_alias 被标记为具有写权限。

[
 "alias" : "iml-rollover_alias",
 "index" : "iml-rollover-000001",
 "filter" : "-",
 "routing.index" : "-",
 "routing.search" : "-",
 "is_write_index" : "-"
 "alias" : "iml-rollover_write_alias",
 "index" : "iml-rollover-000001",
 "filter" : "-",
 "routing.index" : "-",
 "routing.search" : "-",
 "is_write_index" : "false"
 "alias" : "iml-rollover_alias",
 "index" : "iml-rollover-000002",
 "filter" : "-",
 "routing.index" : "-",
 "routing.search" : "-",
 "is_write_index" : "-"
 "alias" : "iml-rollover_write_alias",
 "index" : "iml-rollover-000002",
 "filter" : "-",
 "routing.index" : "-",
 "routing.search" : "-",
 "is_write_index" : "true"
]

和预期是一致的,正确的完成滚动。这时通过iml-rollover_write_alias写入数据,数据被写入到iml-rollover-000002索引中。再次执行插入数据语句,然后查看索引文档。

结果如下

health index docs.count
green iml-rollover-000001 2
green iml-rollover-000002 2

本文转自网络,原文链接:https://developer.aliyun.com/article/784088
本站部分内容转载于网络,版权归原作者所有,转载之目的在于传播更多优秀技术内容,如有侵权请联系QQ/微信:153890879删除,谢谢!
上一篇:分页搜索 下一篇:Reindex API

推荐图文


随机推荐