教程:创建具有生命周期的数据流
编辑教程:创建具有生命周期的数据流
编辑要创建具有内置生命周期的数据流,请按照以下步骤操作
创建索引模板
编辑数据流需要匹配的索引模板。您可以通过在索引模板中设置 lifecycle
字段来配置数据流生命周期,就像您对映射和索引设置所做的那样。您可以按如下方式定义设置生命周期的索引模板
- 包含
data_stream
对象以启用数据流。 - 在模板部分定义生命周期,或包含定义生命周期的可组合模板。
- 使用高于
200
的优先级,以避免与内置模板冲突。请参阅 避免索引模式冲突。
您可以使用 创建索引模板 API。
resp = client.indices.put_index_template( name="my-index-template", index_patterns=[ "my-data-stream*" ], data_stream={}, priority=500, template={ "lifecycle": { "data_retention": "7d" } }, meta={ "description": "Template with data stream lifecycle" }, ) print(resp)
response = client.indices.put_index_template( name: 'my-index-template', body: { index_patterns: [ 'my-data-stream*' ], data_stream: {}, priority: 500, template: { lifecycle: { data_retention: '7d' } }, _meta: { description: 'Template with data stream lifecycle' } } ) puts response
const response = await client.indices.putIndexTemplate({ name: "my-index-template", index_patterns: ["my-data-stream*"], data_stream: {}, priority: 500, template: { lifecycle: { data_retention: "7d", }, }, _meta: { description: "Template with data stream lifecycle", }, }); console.log(response);
PUT _index_template/my-index-template { "index_patterns": ["my-data-stream*"], "data_stream": { }, "priority": 500, "template": { "lifecycle": { "data_retention": "7d" } }, "_meta": { "description": "Template with data stream lifecycle" } }
创建数据流
编辑您可以通过两种方式创建数据流
-
通过使用 创建数据流 API 手动创建流。流的名称仍必须与您的模板的索引模式之一匹配。
resp = client.indices.create_data_stream( name="my-data-stream", ) print(resp)
response = client.indices.create_data_stream( name: 'my-data-stream' ) puts response
const response = await client.indices.createDataStream({ name: "my-data-stream", }); console.log(response);
PUT _data_stream/my-data-stream
-
通过将 索引请求定向到流的名称。此名称必须与您的索引模板的索引模式之一匹配。
resp = client.bulk( index="my-data-stream", operations=[ { "create": {} }, { "@timestamp": "2099-05-06T16:21:15.000Z", "message": "192.0.2.42 - - [06/May/2099:16:21:15 +0000] \"GET /images/bg.jpg HTTP/1.0\" 200 24736" }, { "create": {} }, { "@timestamp": "2099-05-06T16:25:42.000Z", "message": "192.0.2.255 - - [06/May/2099:16:25:42 +0000] \"GET /favicon.ico HTTP/1.0\" 200 3638" } ], ) print(resp)
response = client.bulk( index: 'my-data-stream', body: [ { create: {} }, { "@timestamp": '2099-05-06T16:21:15.000Z', message: '192.0.2.42 - - [06/May/2099:16:21:15 +0000] "GET /images/bg.jpg HTTP/1.0" 200 24736' }, { create: {} }, { "@timestamp": '2099-05-06T16:25:42.000Z', message: '192.0.2.255 - - [06/May/2099:16:25:42 +0000] "GET /favicon.ico HTTP/1.0" 200 3638' } ] ) puts response
const response = await client.bulk({ index: "my-data-stream", operations: [ { create: {}, }, { "@timestamp": "2099-05-06T16:21:15.000Z", message: '192.0.2.42 - - [06/May/2099:16:21:15 +0000] "GET /images/bg.jpg HTTP/1.0" 200 24736', }, { create: {}, }, { "@timestamp": "2099-05-06T16:25:42.000Z", message: '192.0.2.255 - - [06/May/2099:16:25:42 +0000] "GET /favicon.ico HTTP/1.0" 200 3638', }, ], }); console.log(response);
PUT my-data-stream/_bulk { "create":{ } } { "@timestamp": "2099-05-06T16:21:15.000Z", "message": "192.0.2.42 - - [06/May/2099:16:21:15 +0000] \"GET /images/bg.jpg HTTP/1.0\" 200 24736" } { "create":{ } } { "@timestamp": "2099-05-06T16:25:42.000Z", "message": "192.0.2.255 - - [06/May/2099:16:25:42 +0000] \"GET /favicon.ico HTTP/1.0\" 200 3638" }
检索生命周期信息
编辑您可以使用 获取数据流生命周期 API 来查看数据流的生命周期,并使用 解释数据流生命周期 API 来查看每个后备索引的确切状态。
resp = client.indices.get_data_lifecycle( name="my-data-stream", ) print(resp)
response = client.indices.get_data_lifecycle( name: 'my-data-stream' ) puts response
const response = await client.indices.getDataLifecycle({ name: "my-data-stream", }); console.log(response);
GET _data_stream/my-data-stream/_lifecycle
结果将如下所示
{ "data_streams": [ { "name": "my-data-stream", "lifecycle": { "enabled": true, "data_retention": "7d", "effective_retention": "7d", "retention_determined_by": "data_stream_configuration" } } ], "global_retention": {} }
您的数据流的名称。 |
|
显示是否为此数据流启用了数据流生命周期。 |
|
用户配置的此数据流中索引数据的保留期。 |
|
数据流生命周期将应用的保留期。这意味着此数据流中的数据将至少保留 7 天。之后,Elasticsearch 可以自行决定删除它。 |
如果您想查看有关数据流生命周期如何在各个后备索引上应用的更多信息,请使用 解释数据流生命周期 API
resp = client.indices.explain_data_lifecycle( index=".ds-my-data-stream-*", ) print(resp)
response = client.indices.explain_data_lifecycle( index: '.ds-my-data-stream-*' ) puts response
const response = await client.indices.explainDataLifecycle({ index: ".ds-my-data-stream-*", }); console.log(response);
GET .ds-my-data-stream-*/_lifecycle/explain
结果将如下所示