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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  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. . <<configure-a-data-stream-ilm-policy>>.
  6. . <<create-a-data-stream-template>>.
  7. . <<create-a-data-stream>>.
  8. . <<secure-a-data-stream>>.
  9. You can also <<convert-an-index-alias-to-a-data-stream,convert an existing index
  10. alias to a data stream>>.
  11. [discrete]
  12. [[configure-a-data-stream-ilm-policy]]
  13. === Optional: Configure an {ilm-init} lifecycle policy
  14. While optional, we recommend you configure an <<set-up-lifecycle-policy,{ilm}
  15. ({ilm-init}) policy>> to automate the management of your data stream's backing
  16. indices.
  17. In {kib}, open the menu and go to *Stack Management > Index Lifecycle Policies*.
  18. Click *Index Lifecycle Policies*.
  19. [role="screenshot"]
  20. image::images/ilm/create-policy.png[Index Lifecycle Policies page]
  21. [%collapsible]
  22. .API example
  23. ====
  24. Use the <<ilm-put-lifecycle,create lifecycle policy API>> to configure a policy:
  25. [source,console]
  26. ----
  27. PUT /_ilm/policy/my-data-stream-policy
  28. {
  29. "policy": {
  30. "phases": {
  31. "hot": {
  32. "actions": {
  33. "rollover": {
  34. "max_size": "25GB"
  35. }
  36. }
  37. },
  38. "delete": {
  39. "min_age": "30d",
  40. "actions": {
  41. "delete": {}
  42. }
  43. }
  44. }
  45. }
  46. }
  47. ----
  48. ====
  49. [discrete]
  50. [[create-a-data-stream-template]]
  51. === Create an index template
  52. . In {kib}, open the menu and go to *Stack Management > Index Management*.
  53. . In the *Index Templates* tab, click *Create template*.
  54. . In the Create template wizard, use the *Data stream* toggle to indicate the
  55. template is used for data streams.
  56. . Use the wizard to finish defining your template. Specify:
  57. * One or more index patterns that match the data stream's name. +
  58. include::{es-repo-dir}/indices/create-data-stream.asciidoc[tag=data-stream-name]
  59. * Mappings and settings for the stream's backing indices.
  60. * A priority for the index template
  61. +
  62. [IMPORTANT]
  63. ====
  64. {es} has built-in index templates for the `metrics-*-*`, `logs-*-*`, and
  65. `synthetics-*-*` index patterns, each with a priority of `100`.
  66. {fleet-guide}/fleet-overview.html[{agent}] uses these templates to
  67. create data streams.
  68. If you use {agent}, assign your index templates a priority lower than `100` to
  69. avoid overriding the built-in templates. Otherwise, use a non-overlapping index
  70. pattern or assign templates with an overlapping pattern a `priority` higher than
  71. `100`.
  72. For example, if you don't use {agent} and want to create a template for the
  73. `logs-*` index pattern, assign your template a priority of `200`. This ensures
  74. your template is applied instead of the built-in template for `logs-*-*`.
  75. ====
  76. If the index template doesn't specify a mapping for the `@timestamp` field, {es}
  77. maps `@timestamp` as a `date` field with default options.
  78. If using {ilm-init}, specify your lifecycle policy in the `index.lifecycle.name`
  79. setting.
  80. TIP: Carefully consider your template's mappings and settings. Later changes may
  81. require reindexing. See <<data-streams-change-mappings-and-settings>>.
  82. [role="screenshot"]
  83. image::images/data-streams/create-index-template.png[Create template page]
  84. [%collapsible]
  85. .API example
  86. ====
  87. Use the <<indices-put-template,put index template API>> to create an index
  88. template. The template must include a `data_stream` object, indicating
  89. it's used for data streams.
  90. [source,console]
  91. ----
  92. PUT /_index_template/my-data-stream-template
  93. {
  94. "index_patterns": [ "my-data-stream*" ],
  95. "data_stream": { },
  96. "priority": 200,
  97. "template": {
  98. "settings": {
  99. "index.lifecycle.name": "my-data-stream-policy"
  100. }
  101. }
  102. }
  103. ----
  104. // TEST[continued]
  105. ====
  106. [discrete]
  107. [[create-a-data-stream]]
  108. === Create the data stream
  109. To automatically create the data stream, submit an
  110. <<add-documents-to-a-data-stream,indexing request>> to the stream. The stream's
  111. name must match one of your template's index patterns.
  112. [source,console]
  113. ----
  114. POST /my-data-stream/_doc/
  115. {
  116. "@timestamp": "2099-03-07T11:04:05.000Z",
  117. "user": {
  118. "id": "vlb44hny"
  119. },
  120. "message": "Login attempt failed"
  121. }
  122. ----
  123. // TEST[continued]
  124. You can also use the <<indices-create-data-stream,create data stream API>> to
  125. manually create the data stream. The stream's name must match one of your
  126. template's index patterns.
  127. [source,console]
  128. ----
  129. PUT /_data_stream/my-data-stream
  130. ----
  131. // TEST[continued]
  132. // TEST[s/my-data-stream/my-data-stream-alt/]
  133. When you create a data stream, {es} automatically creates a backing index for
  134. the stream. This index also acts as the stream's first write index.
  135. [discrete]
  136. [[convert-an-index-alias-to-a-data-stream]]
  137. === Convert an index alias to a data stream
  138. Prior to {es} 7.9, you would typically use an <<indices-aliases,index alias>>
  139. with a write index to manage time series data. Data streams replace most of
  140. this functionality and usually require less maintenance.
  141. To convert an index alias with a write index to a new data stream with the same
  142. name, use the <<indices-migrate-to-data-stream,migrate to data stream API>>.
  143. During conversion, the alias’s indices become hidden backing indices for the
  144. stream. The alias’s write index becomes the stream’s write index. Note the data
  145. stream still requires a matching <<create-a-data-stream-template,index
  146. template>>.
  147. ////
  148. [source,console]
  149. ----
  150. POST idx1/_doc/
  151. {
  152. "message" : "testing",
  153. "@timestamp" : "2099-01-01"
  154. }
  155. POST idx2/_doc/
  156. {
  157. "message" : "testing2",
  158. "@timestamp" : "2099-01-01"
  159. }
  160. POST /_aliases
  161. {
  162. "actions": [
  163. {
  164. "add": {
  165. "index": "idx1",
  166. "alias": "my-time-series-data",
  167. "is_write_index": true
  168. }
  169. },
  170. {
  171. "add": {
  172. "index": "idx2",
  173. "alias": "my-time-series-data"
  174. }
  175. }
  176. ]
  177. }
  178. PUT /_index_template/template
  179. {
  180. "index_patterns": ["my-time-series-data"],
  181. "data_stream": { }
  182. }
  183. ----
  184. // TEST[continued]
  185. ////
  186. [source,console]
  187. ----
  188. POST /_data_stream/_migrate/my-time-series-data
  189. ----
  190. // TEST[continued]
  191. [discrete]
  192. [[secure-a-data-stream]]
  193. === Secure the data stream
  194. To control access to the data stream and its
  195. data, use <<data-stream-privileges,{es}'s {security-features}>>.
  196. [discrete]
  197. [[get-info-about-a-data-stream]]
  198. === Get information about a data stream
  199. In {kib}, open the menu and go to *Stack Management > Index Management*. In the
  200. *Data Streams* tab, click the data stream's name.
  201. [role="screenshot"]
  202. image::images/data-streams/data-streams-list.png[Data Streams tab]
  203. [%collapsible]
  204. .API example
  205. ====
  206. Use the <<indices-get-data-stream,get data stream API>> to retrieve information
  207. about one or more data streams:
  208. ////
  209. [source,console]
  210. ----
  211. POST /my-data-stream/_rollover/
  212. ----
  213. // TEST[continued]
  214. ////
  215. [source,console]
  216. ----
  217. GET /_data_stream/my-data-stream
  218. ----
  219. // TEST[continued]
  220. ====
  221. [discrete]
  222. [[delete-a-data-stream]]
  223. === Delete a data stream
  224. To delete a data stream and its backing indices, open the {kib} menu and go to
  225. *Stack Management > Index Management*. In the *Data Streams* tab, click the
  226. trash icon. The trash icon only displays if you have the `delete_index`
  227. <<security-privileges, security privilege>> for the data stream.
  228. [role="screenshot"]
  229. image::images/data-streams/data-streams-no-delete.png[Data Streams tab]
  230. [%collapsible]
  231. .API example
  232. ====
  233. Use the <<indices-delete-data-stream,delete data stream API>> to delete a data
  234. stream and its backing indices:
  235. [source,console]
  236. ----
  237. DELETE /_data_stream/my-data-stream
  238. ----
  239. // TEST[continued]
  240. ====
  241. ////
  242. [source,console]
  243. ----
  244. DELETE /_data_stream/*
  245. DELETE /_index_template/*
  246. DELETE /_ilm/policy/my-data-stream-policy
  247. ----
  248. // TEST[continued]
  249. ////