Lab 7.2: Data streams
Objective:
In this lab, you will define an index template that creates a new data stream.
-
EXAM PREP: Start by creating a new index template that is a clone of the existing
my-metrics-templatebut has the following settings:- the name of the template is
my-metrics-ds-template - the index pattern is
my_metrics-*-* - creates a data stream
- all the other settings of
my-metrics-templateremain the same
Solution
- In Index Management click the Index Templates tab. Locate your
my-metrics-templateand use the three dots under Actions to select Clone - Change the name of template to "my-metrics-ds-template"
- The index pattern should be the data type name, in this case
my_metrics-*-* - The
Create data streamswitch should be on - No other changes are needed, click Next until you get to the Review page
- Click Create template
You can also complete this task by running the following command in Console:
PUT _index_template/my-metrics-ds-template { "priority": 500, "index_patterns": [ "my_metrics-*-*" ], "data_stream": {}, "composed_of": [ "time-series-mappings", "time-series-settings" ] } - the name of the template is
-
In Console, run the following command to create the data stream:
The response should be something like:POST my_metrics-service.status-dev/_doc { "@timestamp": "2021-07-04", "status": "UP", "message": "Service is running." }{ "_index" : ".ds-my_metrics-service.status-dev-2021.07.04-000001", "_type" : "_doc", "_id" : "C9BS2XkBJ3Q40ZYI1zLs", "_version" : 1, "result" : "created", "_shards" : { "total" : 3, "successful" : 1, "failed" : 0 }, "_seq_no" : 0, "_primary_term" : 1 } -
Let's change the data stream by adding a new field. Open the Index Management page and click Component Templates tab.
-
Edit the
time-series-mappingsby adding a field nameddata_stream.type, which is aconstant_keyword. Save your changes to the template.Solution
- In Index Management click the Component Templates tab. Locate your
time-series-mappingsand use the three dots under Actions to select Edit - Skip to Mappings and Add field with a name of
data_stream.typeand type of Constant keyword. - No other changes are needed, click Next until you get to the Review page
- Click Save component template
You can also solve this task by running the following command in Console:
PUT _component_template/time-series-mappings { "template": { "mappings": { "properties": { "@timestamp": { "type": "date" }, "message": { "type": "text" }, "status": { "type": "keyword" }, "data_stream.type": { "type": "constant_keyword" } } } } } - In Index Management click the Component Templates tab. Locate your
-
Return to Console and view the mappings for all the backing indices:
Notice that the mapping of the initial backup index has not been updated.GET my_metrics-service.status-dev/_mapping -
Manually roll over the data stream.
Solution
POST my_metrics-service.status-dev/_rollover/ -
Index the following document into the data stream:
This document will be indexed in a different index than the first document.POST my_metrics-service.status-dev/_doc { "@timestamp": "2021-07-05", "status": "UP", "message": "Service is running.", "data_stream.type": "my_metrics" } -
View the mappings for all the backing indices again:
GET my_metrics-service.status-dev/_mapping -
Notice there is now a
valuefordata_stream.type. This is the static value for any future documents indexed to this backing index. Future backing indices will have a blankvalue, which will also be set by the first document indexed to that backing index. This is not the behaviour we want, as all backing indices for this data stream should have the same value already set. Update thetime-series-mappingscomponent template once again to addmy_metricsas the default value for thedata_stream.typefield.Solution
- In Index Management click the Component Templates tab. Locate your
time-series-mappingsand use the three dots under Actions to select Edit - Skip to Mappings and click the pencil icon next to
data_stream.type. - Toggle the Set value switch, enter a value of
my_metricsand Update. - No other changes are needed, click Next until you get to the Review page
- Click Create template
You can also solve this task by running the following command in Console:
PUT _component_template/time-series-mappings { "template": { "mappings": { "properties": { "@timestamp": { "type": "date" }, "message": { "type": "text" }, "status": { "type": "keyword" }, "data_stream.type": { "type": "constant_keyword", "value": "my_metrics" } } } } } - In Index Management click the Component Templates tab. Locate your
-
Manually roll over the data stream.
Solution
POST my_metrics-service.status-dev/_rollover/ -
Index the following document into the data stream:
Notice that there is noPOST my_metrics-service.status-dev/_doc { "@timestamp": "2021-07-06", "status": "UP", "message": "Service is running." }data_stream.typefield. -
Perform a simple search request on the data stream:
You should get three documents.GET my_metrics-service.status-dev/_search -
Search for the documents where
data_stream.typeis equal tomy_metrics:You should get two documentsGET my_metrics-service.status-dev/_search { "query": { "match": { "data_stream.type" : "my_metrics" } } } -
OPTIONAL: Convert the
my-metricsalias to a data stream.Solution
The key is to run
POST _data_stream/_migrate/my-metrics. However, this initially produces an error, stating:{ "error" : { "root_cause" : [ { "type" : "illegal_argument_exception", "reason" : "no matching index template found for data stream [my-metrics]" } ], "type" : "illegal_argument_exception", "reason" : "no matching index template found for data stream [my-metrics]" }, "status" : 400To solve this, clone the
my-metrics-templateindex template and set the index pattern tomy-metrics. Ensure the new template sets the data stream flag and has a high priority (say, 900 or so) to avoid conflicts. An example solution might look like this:PUT _index_template/my-metrics-template-copy { "priority": 900, "template": { "settings": { "index": { "lifecycle": { "name": "my-metrics-policy" } } } }, "index_patterns": ["my-metrics"], "data_stream": {}, "composed_of": [ "time-series-mappings", "time-series-settings" ] }
Summary:
In this lab, you learned how to create and modify data streams.