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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. [[set-up-a-data-stream]]
  2. == Set up a data stream
  3. To set up a data stream, follow these steps:
  4. . Check the <<data-stream-prereqs, prerequisites>>.
  5. . <<configure-a-data-stream-ilm-policy>>.
  6. . <<create-a-data-stream-template>>.
  7. . <<create-a-data-stream>>.
  8. After you set up a data stream, you can <<use-a-data-stream, use the data
  9. stream>> for indexing, searches, and other supported operations.
  10. [discrete]
  11. [[data-stream-prereqs]]
  12. === Prerequisites
  13. * {es} data streams are intended for time-series data only. Each document
  14. indexed to a data stream must contain a shared timestamp field.
  15. +
  16. TIP: Data streams work well with most common log formats. While no schema is
  17. required to use data streams, we recommend the {ecs-ref}[Elastic Common Schema
  18. (ECS)].
  19. * Data streams are designed to be <<data-streams-append-only,append-only>>.
  20. While you can index new documents directly to a data stream, you cannot use a
  21. data stream to directly update or delete individual documents. To update or
  22. delete specific documents in a data stream, submit a <<docs-delete,delete>> or
  23. <<docs-update,update>> API request to the backing index containing the document.
  24. [discrete]
  25. [[configure-a-data-stream-ilm-policy]]
  26. === Optional: Configure an {ilm-init} lifecycle policy for a data stream
  27. You can use <<index-lifecycle-management,{ilm} ({ilm-init})>> to automatically
  28. manage a data stream's backing indices. For example, you could use {ilm-init}
  29. to:
  30. * Spin up a new write index for the data stream when the current one reaches a
  31. certain size or age.
  32. * Move older backing indices to slower, less expensive hardware.
  33. * Delete stale backing indices to enforce data retention standards.
  34. To use {ilm-init} with a data stream, you must
  35. <<set-up-lifecycle-policy,configure a lifecycle policy>>. This lifecycle policy
  36. should contain the automated actions to take on backing indices and the
  37. triggers for such actions.
  38. TIP: While optional, we recommend using {ilm-init} to scale data streams in
  39. production.
  40. .*Example*
  41. [%collapsible]
  42. ====
  43. The following <<ilm-put-lifecycle,create lifecycle policy API>> request
  44. configures the `logs_policy` lifecycle policy.
  45. The `logs_policy` policy uses the <<ilm-rollover,`rollover` action>> to create a
  46. new <<data-stream-write-index,write index>> for the data stream when the current
  47. one reaches 25GB in size. The policy also deletes backing indices 30 days after
  48. their rollover.
  49. [source,console]
  50. ----
  51. PUT /_ilm/policy/logs_policy
  52. {
  53. "policy": {
  54. "phases": {
  55. "hot": {
  56. "actions": {
  57. "rollover": {
  58. "max_size": "25GB"
  59. }
  60. }
  61. },
  62. "delete": {
  63. "min_age": "30d",
  64. "actions": {
  65. "delete": {}
  66. }
  67. }
  68. }
  69. }
  70. }
  71. ----
  72. ====
  73. [discrete]
  74. [[create-a-data-stream-template]]
  75. === Create a composable template for a data stream
  76. Each data stream requires a <<indices-templates,composable template>>. The data
  77. stream uses this template to create its backing indices.
  78. Composable templates for data streams must contain:
  79. * A name or wildcard (`*`) pattern for the data stream in the `index_patterns`
  80. property.
  81. * A `data_stream` definition containing the `timestamp_field` property.
  82. This timestamp field must be included in every document indexed to the data
  83. stream.
  84. * A <<date,`date`>> or <<date_nanos,`date_nanos`>> field mapping for the
  85. timestamp field specified in the `timestamp_field` property.
  86. * If you intend to use {ilm-init}, you must specify the
  87. <<configure-a-data-stream-ilm-policy,lifecycle policy>> in the
  88. `index.lifecycle.name` setting.
  89. You can also specify other mappings and settings you'd like to apply to the
  90. stream's backing indices.
  91. .*Example*
  92. [%collapsible]
  93. ====
  94. The following <<indices-templates,put composable template API>> request
  95. configures the `logs_data_stream` template.
  96. [source,console]
  97. ----
  98. PUT /_index_template/logs_data_stream
  99. {
  100. "index_patterns": [ "logs*" ],
  101. "data_stream": {
  102. "timestamp_field": "@timestamp"
  103. },
  104. "template": {
  105. "mappings": {
  106. "properties": {
  107. "@timestamp": {
  108. "type": "date"
  109. }
  110. }
  111. },
  112. "settings": {
  113. "index.lifecycle.name": "logs_policy"
  114. }
  115. }
  116. }
  117. ----
  118. // TEST[continued]
  119. ====
  120. [discrete]
  121. [[create-a-data-stream]]
  122. === Create a data stream
  123. With a composable template, you can create a data stream using one of two
  124. methods:
  125. * Submit an <<add-documents-to-a-data-stream,indexing request>> to a target
  126. matching the name or wildcard pattern defined in the template's `index_patterns`
  127. property.
  128. +
  129. --
  130. If the indexing request's target doesn't exist, {es} creates the data stream and
  131. uses the target name as the name for the stream.
  132. NOTE: Data streams support only specific types of indexing requests. See
  133. <<add-documents-to-a-data-stream>>.
  134. .*Example: Index documents to create a data stream*
  135. [%collapsible]
  136. ====
  137. The following <<docs-index_,index API>> request targets `logs`, which matches
  138. the wildcard pattern for the `logs_data_stream` template. Because no existing
  139. index or data stream uses this name, this request creates the `logs` data stream
  140. and indexes the document to it.
  141. [source,console]
  142. ----
  143. POST /logs/_doc/
  144. {
  145. "@timestamp": "2020-12-06T11:04:05.000Z",
  146. "user": {
  147. "id": "vlb44hny"
  148. },
  149. "message": "Login attempt failed"
  150. }
  151. ----
  152. // TEST[continued]
  153. The API returns the following response. Note the `_index` property contains
  154. `.ds-logs-000001`, indicating the document was indexed to the write index of the
  155. new `logs` data stream.
  156. [source,console-result]
  157. ----
  158. {
  159. "_index": ".ds-logs-000001",
  160. "_id": "qecQmXIBT4jB8tq1nG0j",
  161. "_version": 1,
  162. "result": "created",
  163. "_shards": {
  164. "total": 2,
  165. "successful": 1,
  166. "failed": 0
  167. },
  168. "_seq_no": 0,
  169. "_primary_term": 1
  170. }
  171. ----
  172. // TESTRESPONSE[s/"_id": "qecQmXIBT4jB8tq1nG0j"/"_id": $body._id/]
  173. ====
  174. --
  175. * Use the <<indices-create-data-stream,create data stream API>> to manually
  176. create a data stream. The name of the data stream must match the
  177. name or wildcard pattern defined in the template's `index_patterns` property.
  178. +
  179. --
  180. .*Example: Manually create a data stream*
  181. [%collapsible]
  182. ====
  183. The following <<indices-create-data-stream,create data stream API>> request
  184. targets `logs_alt`, which matches the wildcard pattern for the
  185. `logs_data_stream` template. Because no existing index or data stream uses this
  186. name, this request creates the `logs_alt` data stream.
  187. [source,console]
  188. ----
  189. PUT /_data_stream/logs_alt
  190. ----
  191. // TEST[continued]
  192. ====
  193. --
  194. ////
  195. [source,console]
  196. ----
  197. DELETE /_data_stream/logs
  198. DELETE /_data_stream/logs_alt
  199. DELETE /_index_template/logs_data_stream
  200. DELETE /_ilm/policy/logs_policy
  201. ----
  202. // TEST[continued]
  203. ////