| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385 | [role="xpack"][[set-up-a-data-stream]]== Set up a data streamTo set up a data stream, follow these steps:. Check the <<data-stream-prereqs, prerequisites>>.. <<configure-a-data-stream-ilm-policy>>.. <<create-a-data-stream-template>>.. <<create-a-data-stream>>.. <<get-info-about-a-data-stream>> to verify it exists.. <<secure-a-data-stream>>.After you set up a data stream, you can <<use-a-data-stream, use the datastream>> for indexing, searches, and other supported operations.If you no longer need it, you can <<delete-a-data-stream,delete a data stream>>and its backing indices.[discrete][[data-stream-prereqs]]=== Prerequisites* {es} data streams are intended for time-series data only. Each documentindexed to a data stream must contain the `@timestamp` field. This field must bemapped as a <<date,`date`>> or <<date_nanos,`date_nanos`>> field data type.* Data streams are best suited for time-based,<<data-streams-append-only,append-only>> use cases. If you frequently need toupdate or delete existing documents, we recommend using an index alias and anindex template instead.[discrete][[configure-a-data-stream-ilm-policy]]=== Optional: Configure an {ilm-init} lifecycle policy for a data streamYou can use <<index-lifecycle-management,{ilm} ({ilm-init})>> to automaticallymanage a data stream's backing indices. For example, you could use {ilm-init}to:* Spin up a new write index for the data stream when the current one reaches a  certain size or age.* Move older backing indices to slower, less expensive hardware.* Delete stale backing indices to enforce data retention standards.To use {ilm-init} with a data stream, you must<<set-up-lifecycle-policy,configure a lifecycle policy>>. This lifecycle policyshould contain the automated actions to take on backing indices and thetriggers for such actions.TIP: While optional, we recommend using {ilm-init} to scale data streams inproduction..*Example*[%collapsible]====The following <<ilm-put-lifecycle,create lifecycle policy API>> requestconfigures the `logs_policy` lifecycle policy.The `logs_policy` policy uses the <<ilm-rollover,`rollover` action>> to create anew <<data-stream-write-index,write index>> for the data stream when the currentone reaches 25GB in size. The policy also deletes backing indices 30 days aftertheir rollover.[source,console]----PUT /_ilm/policy/logs_policy{  "policy": {    "phases": {      "hot": {        "actions": {          "rollover": {            "max_size": "25GB"          }        }      },      "delete": {        "min_age": "30d",        "actions": {          "delete": {}        }      }    }  }}----====[discrete][[create-a-data-stream-template]]=== Create an index template for a data streamEach data stream requires an <<indices-templates,index template>>. The datastream uses this template to create its backing indices.An index template for a data stream must contain:* A name or wildcard (`*`) pattern for the data stream in the `index_patterns`property.+You can use the resolve index API to check if the name or patternmatches any existing indices, index aliases, or data streams. If so, you shouldconsider using another name or pattern.+.*Example*[%collapsible]====The following resolve index API request checks for any existing indices, indexaliases, or data streams that start with `logs`. If not, the `logs*`wildcard pattern can be used to create a new data stream.[source,console]----GET /_resolve/index/logs*----// TEST[continued]The API returns the following response, indicating no existing targets matchthis pattern.[source,console-result]----{  "indices" : [ ],  "aliases" : [ ],  "data_streams" : [ ]}----====* A `data_stream` object with an empty body (`{ }`).The template can also contain:* An optional field mapping for the `@timestamp` field. Both the <<date,`date`>> and<<date_nanos,`date_nanos`>> field data types are supported. If no mapping is specified,a <<date,`date`>> field data type with default options is used.+This mapping can include other <<mapping-params,mapping parameters>>, such as<<mapping-date-format,`format`>>.+IMPORTANT: Carefully consider the `@timestamp` field's mapping, includingits <<mapping-params,mapping parameters>>.Once the stream is created, you can only update the `@timestamp` field's mappingby reindexing the data stream. See<<data-streams-use-reindex-to-change-mappings-settings>>.* If you intend to use {ilm-init}, the  <<configure-a-data-stream-ilm-policy,lifecycle policy>> in the  `index.lifecycle.name` setting.You can also specify other mappings and settings you'd like to apply to thestream's backing indices.TIP: We recommend you carefully consider which mappings and settings to includein this template before creating a data stream. Later changes to the mappings orsettings of a stream's backing indices may require reindexing. See<<data-streams-change-mappings-and-settings>>..*Example*[%collapsible]====The following <<indices-templates,put index template API>> requestconfigures the `logs_data_stream` template.[source,console]----PUT /_index_template/logs_data_stream{  "index_patterns": [ "logs*" ],  "data_stream": {},  "template": {    "settings": {      "index.lifecycle.name": "logs_policy"    }  }}----// TEST[continued]====NOTE: You cannot delete an index template that's in use by a data stream.This would prevent the data stream from creating new backing indices.[discrete][[create-a-data-stream]]=== Create a data streamWith an index template, you can create a data stream using one of twomethods:* Submit an <<add-documents-to-a-data-stream,indexing request>> to a targetmatching the name or wildcard pattern defined in the template's `index_patterns`property.+--If the indexing request's target doesn't exist, {es} creates the data stream anduses the target name as the name for the stream.NOTE: Data streams support only specific types of indexing requests. See<<add-documents-to-a-data-stream>>.[[index-documents-to-create-a-data-stream]].*Example: Index documents to create a data stream*[%collapsible]====The following <<docs-index_,index API>> request targets `logs`, which matchesthe wildcard pattern for the `logs_data_stream` template. Because no existingindex or data stream uses this name, this request creates the `logs` data streamand indexes the document to it.[source,console]----POST /logs/_doc/{  "@timestamp": "2020-12-06T11:04:05.000Z",  "user": {    "id": "vlb44hny"  },  "message": "Login attempt failed"}----// TEST[continued]The API returns the following response. Note the `_index` property contains`.ds-logs-000001`, indicating the document was indexed to the write index of thenew `logs` data stream.[source,console-result]----{  "_index": ".ds-logs-000001",  "_id": "qecQmXIBT4jB8tq1nG0j",  "_version": 1,  "result": "created",  "_shards": {    "total": 2,    "successful": 1,    "failed": 0  },  "_seq_no": 0,  "_primary_term": 1}----// TESTRESPONSE[s/"_id": "qecQmXIBT4jB8tq1nG0j"/"_id": $body._id/]====--* Use the <<indices-create-data-stream,create data stream API>> to manuallycreate a data stream. The name of the data stream must match thename or wildcard pattern defined in the template's `index_patterns` property.+--.*Example: Manually create a data stream*[%collapsible]====The following <<indices-create-data-stream,create data stream API>> requesttargets `logs_alt`, which matches the wildcard pattern for the`logs_data_stream` template. Because no existing index or data stream uses thisname, this request creates the `logs_alt` data stream.[source,console]----PUT /_data_stream/logs_alt----// TEST[continued]====--[discrete][[get-info-about-a-data-stream]]=== Get information about a data streamYou can use the <<indices-get-data-stream,get data stream API>> to getinformation about one or more data streams, including:* The timestamp field* The current backing indices, which is returned as an array. The last item in  the array contains information about the stream's current write index.* The current generation* The data stream's health status* The index template used to create the stream's backing indices* The current {ilm-init} lifecycle policy in the stream's matching indextemplateThis is also handy way to verify that a recently created data stream exists..*Example*[%collapsible]====The following get data stream API request retrieves information about the`logs` data stream.////[source,console]----POST /logs/_rollover/----// TEST[continued]////[source,console]----GET /_data_stream/logs----// TEST[continued]The API returns the following response. Note the `indices` property contains anarray of the stream's current backing indices. The last item in this arraycontains information about the stream's write index, `.ds-logs-000002`.[source,console-result]----{  "data_streams": [    {      "name": "logs",      "timestamp_field": {        "name": "@timestamp"      },      "indices": [        {          "index_name": ".ds-logs-000001",          "index_uuid": "krR78LfvTOe6gr5dj2_1xQ"        },        {          "index_name": ".ds-logs-000002",        <1>          "index_uuid": "C6LWyNJHQWmA08aQGvqRkA"        }      ],      "generation": 2,      "status": "GREEN",      "template": "logs_data_stream",      "ilm_policy": "logs_policy"    }  ]}----// TESTRESPONSE[s/"index_uuid": "krR78LfvTOe6gr5dj2_1xQ"/"index_uuid": $body.data_streams.0.indices.0.index_uuid/]// TESTRESPONSE[s/"index_uuid": "C6LWyNJHQWmA08aQGvqRkA"/"index_uuid": $body.data_streams.0.indices.1.index_uuid/]// TESTRESPONSE[s/"status": "GREEN"/"status": "YELLOW"/]<1> Last item in the `indices` array for the `logs` data stream. This itemcontains information about the stream's current write index, `.ds-logs-000002`.====[discrete][[secure-a-data-stream]]=== Secure a data streamYou can use {es} {security-features} to control access to a data stream and itsdata. See <<data-stream-privileges>>.[discrete][[delete-a-data-stream]]=== Delete a data streamYou can use the <<indices-delete-data-stream,delete data stream API>> to deletea data stream and its backing indices..*Example*[%collapsible]====The following delete data stream API request deletes the `logs` data stream. Thisrequest also deletes the stream's backing indices and any data they contain.[source,console]----DELETE /_data_stream/logs----// TEST[continued]====////[source,console]----DELETE /_data_stream/*DELETE /_index_template/*DELETE /_ilm/policy/logs_policy----// TEST[continued]////
 |