set-up-a-data-stream.asciidoc 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406
  1. [role="xpack"]
  2. [[set-up-a-data-stream]]
  3. == Set up a data stream
  4. To set up a data stream, follow these steps:
  5. . Check the <<data-stream-prereqs, prerequisites>>.
  6. . <<configure-a-data-stream-ilm-policy>>.
  7. . <<create-a-data-stream-template>>.
  8. . <<create-a-data-stream>>.
  9. . <<get-info-about-a-data-stream>> to verify it exists.
  10. . <<secure-a-data-stream>>.
  11. After you set up a data stream, you can <<use-a-data-stream, use the data
  12. stream>> for indexing, searches, and other supported operations.
  13. If you no longer need it, you can <<delete-a-data-stream,delete a data stream>>
  14. and its backing indices.
  15. [discrete]
  16. [[data-stream-prereqs]]
  17. === Prerequisites
  18. * {es} data streams are intended for time series data only. Each document
  19. indexed to a data stream must contain the `@timestamp` field. This field must be
  20. mapped as a <<date,`date`>> or <<date_nanos,`date_nanos`>> field data type.
  21. * Data streams are best suited for time-based,
  22. <<data-streams-append-only,append-only>> use cases. If you frequently need to
  23. update or delete existing documents, we recommend using an index alias and an
  24. index template instead.
  25. [discrete]
  26. [[configure-a-data-stream-ilm-policy]]
  27. === Optional: Configure an {ilm-init} lifecycle policy for a data stream
  28. You can use <<index-lifecycle-management,{ilm} ({ilm-init})>> to automatically
  29. manage a data stream's backing indices. For example, you could use {ilm-init}
  30. to:
  31. * Spin up a new write index for the data stream when the current one reaches a
  32. certain size or age.
  33. * Move older backing indices to slower, less expensive hardware.
  34. * Delete stale backing indices to enforce data retention standards.
  35. To use {ilm-init} with a data stream, you must
  36. <<set-up-lifecycle-policy,configure a lifecycle policy>>. This lifecycle policy
  37. should contain the automated actions to take on backing indices and the
  38. triggers for such actions.
  39. TIP: While optional, we recommend using {ilm-init} to manage the backing indices
  40. associated with a data stream.
  41. You can create the policy through the Kibana UI. In Kibana, open the menu and go
  42. to *Stack Management > Index Lifecycle Policies*. Click *Index Lifecycle
  43. Policies*.
  44. [role="screenshot"]
  45. image::images/ilm/create-policy.png[Index Lifecycle Policies page]
  46. You can also create a policy using the <<ilm-put-lifecycle,create lifecycle
  47. policy API>>.
  48. The following request configures the `my-data-stream-policy` lifecycle policy.
  49. The policy uses the <<ilm-rollover,`rollover` action>> to create a
  50. new <<data-stream-write-index,write index>> for the data stream when the current
  51. one reaches 25GB in size. The policy also deletes backing indices 30 days after
  52. their rollover.
  53. [source,console]
  54. ----
  55. PUT /_ilm/policy/my-data-stream-policy
  56. {
  57. "policy": {
  58. "phases": {
  59. "hot": {
  60. "actions": {
  61. "rollover": {
  62. "max_size": "25GB"
  63. }
  64. }
  65. },
  66. "delete": {
  67. "min_age": "30d",
  68. "actions": {
  69. "delete": {}
  70. }
  71. }
  72. }
  73. }
  74. }
  75. ----
  76. [discrete]
  77. [[create-a-data-stream-template]]
  78. === Create an index template for a data stream
  79. A data stream uses an index template to configure its backing indices. A
  80. template for a data stream must specify:
  81. * One or more index patterns that match the name of the stream.
  82. * The mappings and settings for the stream's backing indices.
  83. * That the template is used exclusively for data streams.
  84. * A priority for the template.
  85. [IMPORTANT]
  86. ====
  87. {es} has built-in index templates for the `metrics-*-*`, `logs-*-*`, and `synthetics-*-*` index
  88. patterns, each with a priority of `100`.
  89. {ingest-guide}/ingest-management-overview.html[{agent}] uses these templates to
  90. create data streams. If you use {agent}, assign your index templates a priority
  91. lower than `100` to avoid overriding the built-in templates.
  92. Otherwise, to avoid accidentally applying the built-in templates, use a
  93. non-overlapping index pattern or assign templates with an overlapping pattern a
  94. `priority` higher than `100`.
  95. For example, if you don't use {agent} and want to create a template for the
  96. `logs-*` index pattern, assign your template a priority of `200`. This ensures
  97. your template is applied instead of the built-in template for `logs-*-*`.
  98. ====
  99. Every document indexed to a data stream must have a `@timestamp` field. This
  100. field can be mapped as a <<date,`date`>> or <<date_nanos,`date_nanos`>> field
  101. data type by the stream's index template. This mapping can include other
  102. <<mapping-params,mapping parameters>>, such as <<mapping-date-format,`format`>>.
  103. If the template does not specify a mapping, the `@timestamp` field is mapped as
  104. a `date` field with default options.
  105. We recommend using {ilm-init} to manage a data stream's backing indices. Specify
  106. the name of the lifecycle policy with the `index.lifecycle.name` setting.
  107. TIP: We recommend you carefully consider which mappings and settings to include
  108. in this template before creating a data stream. Later changes to the mappings or
  109. settings of a stream's backing indices may require reindexing. See
  110. <<data-streams-change-mappings-and-settings>>.
  111. You can create an index template through the Kibana UI:
  112. . From Kibana, open the menu and go to *Stack Management > Index Management*.
  113. . In the *Index Templates* tab, click *Create template*.
  114. . In the Create template wizard, use the *Data stream* toggle to indicate the
  115. template is used exclusively for data streams.
  116. [role="screenshot"]
  117. image::images/data-streams/create-index-template.png[Create template page]
  118. You can also create a template using the <<indices-put-template,put index
  119. template API>>. The template must include a `data_stream` object with an empty
  120. body (`{ }`). This object indicates the template is used exclusively for data
  121. streams.
  122. The following request configures the `my-data-stream-template` index template.
  123. Because no field mapping is specified, the `@timestamp` field uses the `date`
  124. field data type by default.
  125. [source,console]
  126. ----
  127. PUT /_index_template/my-data-stream-template
  128. {
  129. "index_patterns": [ "my-data-stream*" ],
  130. "data_stream": { },
  131. "priority": 200,
  132. "template": {
  133. "settings": {
  134. "index.lifecycle.name": "my-data-stream-policy"
  135. }
  136. }
  137. }
  138. ----
  139. // TEST[continued]
  140. Alternatively, the following template maps `@timestamp` as a `date_nanos` field.
  141. [source,console]
  142. ----
  143. PUT /_index_template/my-data-stream-template
  144. {
  145. "index_patterns": [ "my-data-stream*" ],
  146. "data_stream": { },
  147. "priority": 200,
  148. "template": {
  149. "mappings": {
  150. "properties": {
  151. "@timestamp": { "type": "date_nanos" } <1>
  152. }
  153. },
  154. "settings": {
  155. "index.lifecycle.name": "my-data-stream-policy"
  156. }
  157. }
  158. }
  159. ----
  160. // TEST[continued]
  161. <1> Maps `@timestamp` as a `date_nanos` field. You can include other supported
  162. mapping parameters in this field mapping.
  163. NOTE: You cannot delete an index template that's in use by a data stream.
  164. This would prevent the data stream from creating new backing indices.
  165. [discrete]
  166. [[create-a-data-stream]]
  167. === Create a data stream
  168. You can create a data stream using one of two methods:
  169. * <<index-documents-to-create-a-data-stream>>
  170. * <<manually-create-a-data-stream>>
  171. [discrete]
  172. [[index-documents-to-create-a-data-stream]]
  173. ==== Index documents to create a data stream
  174. You can automatically create a data stream using an indexing request. Submit
  175. an <<add-documents-to-a-data-stream,indexing request>> to a target
  176. matching the index pattern defined in the template's `index_patterns`
  177. property.
  178. If the indexing request's target doesn't exist, {es} creates the data stream and
  179. uses the target name as the name for the stream.
  180. NOTE: Data streams support only specific types of indexing requests. See
  181. <<add-documents-to-a-data-stream>>.
  182. The following <<docs-index_,index API>> request targets `my-data-stream`, which
  183. matches the index pattern for `my-data-stream-template`. Because
  184. no existing index or data stream uses this name, this request creates the
  185. `my-data-stream` data stream and indexes the document to it.
  186. [source,console]
  187. ----
  188. POST /my-data-stream/_doc/
  189. {
  190. "@timestamp": "2020-12-06T11:04:05.000Z",
  191. "user": {
  192. "id": "vlb44hny"
  193. },
  194. "message": "Login attempt failed"
  195. }
  196. ----
  197. // TEST[continued]
  198. The API returns the following response. Note the `_index` property contains
  199. `.ds-my-data-stream-000001`, indicating the document was indexed to the write
  200. index of the new data stream.
  201. [source,console-result]
  202. ----
  203. {
  204. "_index": ".ds-my-data-stream-000001",
  205. "_id": "qecQmXIBT4jB8tq1nG0j",
  206. "_version": 1,
  207. "result": "created",
  208. "_shards": {
  209. "total": 2,
  210. "successful": 1,
  211. "failed": 0
  212. },
  213. "_seq_no": 0,
  214. "_primary_term": 1
  215. }
  216. ----
  217. // TESTRESPONSE[s/"_id": "qecQmXIBT4jB8tq1nG0j"/"_id": $body._id/]
  218. [discrete]
  219. [[manually-create-a-data-stream]]
  220. ==== Manually create a data stream
  221. You can use the <<indices-create-data-stream,create data stream API>> to
  222. manually create a data stream. The name of the data stream must match the index
  223. pattern defined in the template's `index_patterns` property.
  224. The following create data stream request targets `my-data-stream-alt`, which
  225. matches the index pattern for `my-data-stream-template`. Because
  226. no existing index or data stream uses this name, this request creates the
  227. `my-data-stream-alt` data stream.
  228. [source,console]
  229. ----
  230. PUT /_data_stream/my-data-stream-alt
  231. ----
  232. // TEST[continued]
  233. [discrete]
  234. [[get-info-about-a-data-stream]]
  235. === Get information about a data stream
  236. To view information about a data stream in Kibana, open the menu and go to
  237. *Stack Management > Index Management*. In the *Data Streams* tab, click a data
  238. stream's name to view information about the stream.
  239. [role="screenshot"]
  240. image::images/data-streams/data-streams-list.png[Data Streams tab]
  241. You can also use the <<indices-get-data-stream,get data stream API>> to retrieve
  242. the following information about one or more data streams:
  243. * The current backing indices, which is returned as an array. The last item in
  244. the array contains information about the stream's current write index.
  245. * The current generation
  246. * The data stream's health status
  247. * The index template used to create the stream's backing indices
  248. * The current {ilm-init} lifecycle policy in the stream's matching index
  249. template
  250. The following get data stream API request retrieves information about
  251. `my-data-stream`.
  252. ////
  253. [source,console]
  254. ----
  255. POST /my-data-stream/_rollover/
  256. ----
  257. // TEST[continued]
  258. ////
  259. [source,console]
  260. ----
  261. GET /_data_stream/my-data-stream
  262. ----
  263. // TEST[continued]
  264. The API returns the following response. Note the `indices` property contains an
  265. array of the stream's current backing indices. The last item in this array
  266. contains information about the stream's write index, `.ds-my-data-stream-000002`.
  267. [source,console-result]
  268. ----
  269. {
  270. "data_streams": [
  271. {
  272. "name": "my-data-stream",
  273. "timestamp_field": {
  274. "name": "@timestamp"
  275. },
  276. "indices": [
  277. {
  278. "index_name": ".ds-my-data-stream-000001",
  279. "index_uuid": "krR78LfvTOe6gr5dj2_1xQ"
  280. },
  281. {
  282. "index_name": ".ds-my-data-stream-000002", <1>
  283. "index_uuid": "C6LWyNJHQWmA08aQGvqRkA"
  284. }
  285. ],
  286. "generation": 2,
  287. "status": "GREEN",
  288. "template": "my-data-stream-template",
  289. "ilm_policy": "my-data-stream-policy"
  290. }
  291. ]
  292. }
  293. ----
  294. // TESTRESPONSE[s/"index_uuid": "krR78LfvTOe6gr5dj2_1xQ"/"index_uuid": $body.data_streams.0.indices.0.index_uuid/]
  295. // TESTRESPONSE[s/"index_uuid": "C6LWyNJHQWmA08aQGvqRkA"/"index_uuid": $body.data_streams.0.indices.1.index_uuid/]
  296. // TESTRESPONSE[s/"status": "GREEN"/"status": "YELLOW"/]
  297. <1> Last item in the `indices` array for `my-data-stream`. This
  298. item contains information about the stream's current write index,
  299. `.ds-my-data-stream-000002`.
  300. [discrete]
  301. [[secure-a-data-stream]]
  302. === Secure a data stream
  303. You can use {es} {security-features} to control access to a data stream and its
  304. data. See <<data-stream-privileges>>.
  305. [discrete]
  306. [[delete-a-data-stream]]
  307. === Delete a data stream
  308. You can use the Kibana UI to delete a data stream and its backing indices. In
  309. Kibana, open the menu and go to *Stack Management > Index Management*. In the
  310. *Data Streams* tab, click the trash can icon to delete a stream and its backing
  311. indices.
  312. [role="screenshot"]
  313. image::images/data-streams/data-streams-list.png[Data Streams tab]
  314. You can also use the the <<indices-delete-data-stream,delete data stream API>>
  315. to delete a data stream. The following delete data stream API request deletes
  316. `my-data-stream`. This request also deletes the stream's backing
  317. indices and any data they contain.
  318. [source,console]
  319. ----
  320. DELETE /_data_stream/my-data-stream
  321. ----
  322. // TEST[continued]
  323. ////
  324. [source,console]
  325. ----
  326. DELETE /_data_stream/*
  327. DELETE /_index_template/*
  328. DELETE /_ilm/policy/my-data-stream-policy
  329. ----
  330. // TEST[continued]
  331. ////