《Elastic Stack 实战手册》——三、产品能力——3.4.入门篇——3.4.2.Elasticsearch基础应用——3.4.2.13.Rollover API(5) https://developer.aliyun.com/article/1230205
创建一个模板,模版中引用刚才定义的生命周期策略,并指定滚动的别名。
关于索引模板更多信息请参看 Simulate multi-component templates 。
# 这个是 legacy 的 index template,适用于 7.8 版本以前, # 但在7.10版本,仍然可以用 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
创作人简介:
杨松柏,目前就职于好未来教育科技集团,任数据平台资深研发工程师。 长期关注ELK、TiDB、clickhouse 等分布式存储技术,对于 Elasticsearch 和 TiDB 都有深入的理解。