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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397
  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. . <<get-info-about-a-data-stream>> to verify it exists.
  9. After you set up a data stream, you can <<use-a-data-stream, use the data
  10. stream>> for indexing, searches, and other supported operations.
  11. If you no longer need it, you can <<delete-a-data-stream,delete a data stream>>
  12. and its backing indices.
  13. [discrete]
  14. [[data-stream-prereqs]]
  15. === Prerequisites
  16. * {es} data streams are intended for time-series data only. Each document
  17. indexed to a data stream must contain a shared timestamp field.
  18. +
  19. TIP: Data streams work well with most common log formats. While no schema is
  20. required to use data streams, we recommend the {ecs-ref}[Elastic Common Schema
  21. (ECS)].
  22. * Data streams are best suited for time-based,
  23. <<data-streams-append-only,append-only>> use cases. If you frequently need to
  24. update or delete existing documents, we recommend using an index alias and an
  25. index template instead.
  26. [discrete]
  27. [[configure-a-data-stream-ilm-policy]]
  28. === Optional: Configure an {ilm-init} lifecycle policy for a data stream
  29. You can use <<index-lifecycle-management,{ilm} ({ilm-init})>> to automatically
  30. manage a data stream's backing indices. For example, you could use {ilm-init}
  31. to:
  32. * Spin up a new write index for the data stream when the current one reaches a
  33. certain size or age.
  34. * Move older backing indices to slower, less expensive hardware.
  35. * Delete stale backing indices to enforce data retention standards.
  36. To use {ilm-init} with a data stream, you must
  37. <<set-up-lifecycle-policy,configure a lifecycle policy>>. This lifecycle policy
  38. should contain the automated actions to take on backing indices and the
  39. triggers for such actions.
  40. TIP: While optional, we recommend using {ilm-init} to scale data streams in
  41. production.
  42. .*Example*
  43. [%collapsible]
  44. ====
  45. The following <<ilm-put-lifecycle,create lifecycle policy API>> request
  46. configures the `logs_policy` lifecycle policy.
  47. The `logs_policy` policy uses the <<ilm-rollover,`rollover` action>> to create a
  48. new <<data-stream-write-index,write index>> for the data stream when the current
  49. one reaches 25GB in size. The policy also deletes backing indices 30 days after
  50. their rollover.
  51. [source,console]
  52. ----
  53. PUT /_ilm/policy/logs_policy
  54. {
  55. "policy": {
  56. "phases": {
  57. "hot": {
  58. "actions": {
  59. "rollover": {
  60. "max_size": "25GB"
  61. }
  62. }
  63. },
  64. "delete": {
  65. "min_age": "30d",
  66. "actions": {
  67. "delete": {}
  68. }
  69. }
  70. }
  71. }
  72. }
  73. ----
  74. ====
  75. [discrete]
  76. [[create-a-data-stream-template]]
  77. === Create an index template for a data stream
  78. Each data stream requires an <<indices-templates,index template>>. The data
  79. stream uses this template to create its backing indices.
  80. Index templates for data streams must contain:
  81. * A name or wildcard (`*`) pattern for the data stream in the `index_patterns`
  82. property.
  83. +
  84. You can use the resolve index API to check if the name or pattern
  85. matches any existing indices, index aliases, or data streams. If so, you should
  86. consider using another name or pattern.
  87. +
  88. .*Example*
  89. [%collapsible]
  90. ====
  91. The following resolve index API request checks for any existing indices, index
  92. aliases, or data streams that start with `logs`. If not, the `logs*`
  93. wildcard pattern can be used to create a new data stream.
  94. [source,console]
  95. ----
  96. GET /_resolve/index/logs*
  97. ----
  98. // TEST[continued]
  99. The API returns the following response, indicating no existing targets match
  100. this pattern.
  101. [source,console-result]
  102. ----
  103. {
  104. "indices" : [ ],
  105. "aliases" : [ ],
  106. "data_streams" : [ ]
  107. }
  108. ----
  109. ====
  110. * A `data_stream` definition containing the `timestamp_field` property.
  111. This timestamp field must be included in every document indexed to the data
  112. stream.
  113. * A <<date,`date`>> or <<date_nanos,`date_nanos`>> field mapping for the
  114. timestamp field specified in the `timestamp_field` property.
  115. +
  116. IMPORTANT: Carefully consider the timestamp field's mapping, including
  117. <<mapping-params,mapping parameters>> such as <<mapping-date-format,`format`>>.
  118. Once the stream is created, you can only update the timestamp field's mapping by
  119. reindexing the data stream. See
  120. <<data-streams-use-reindex-to-change-mappings-settings>>.
  121. * If you intend to use {ilm-init}, you must specify the
  122. <<configure-a-data-stream-ilm-policy,lifecycle policy>> in the
  123. `index.lifecycle.name` setting.
  124. You can also specify other mappings and settings you'd like to apply to the
  125. stream's backing indices.
  126. TIP: We recommend you carefully consider which mappings and settings to include
  127. in this template before creating a data stream. Later changes to the mappings or
  128. settings of a stream's backing indices may require reindexing. See
  129. <<data-streams-change-mappings-and-settings>>.
  130. .*Example*
  131. [%collapsible]
  132. ====
  133. The following <<indices-templates,put index template API>> request
  134. configures the `logs_data_stream` template.
  135. [source,console]
  136. ----
  137. PUT /_index_template/logs_data_stream
  138. {
  139. "index_patterns": [ "logs*" ],
  140. "data_stream": {
  141. "timestamp_field": "@timestamp"
  142. },
  143. "template": {
  144. "mappings": {
  145. "properties": {
  146. "@timestamp": {
  147. "type": "date"
  148. }
  149. }
  150. },
  151. "settings": {
  152. "index.lifecycle.name": "logs_policy"
  153. }
  154. }
  155. }
  156. ----
  157. // TEST[continued]
  158. ====
  159. NOTE: You cannot delete an index template that's in use by a data stream.
  160. This would prevent the data stream from creating new backing indices.
  161. [discrete]
  162. [[create-a-data-stream]]
  163. === Create a data stream
  164. With an index template, you can create a data stream using one of two
  165. methods:
  166. * Submit an <<add-documents-to-a-data-stream,indexing request>> to a target
  167. matching the name or wildcard pattern defined in the template's `index_patterns`
  168. property.
  169. +
  170. --
  171. If the indexing request's target doesn't exist, {es} creates the data stream and
  172. uses the target name as the name for the stream.
  173. NOTE: Data streams support only specific types of indexing requests. See
  174. <<add-documents-to-a-data-stream>>.
  175. [[index-documents-to-create-a-data-stream]]
  176. .*Example: Index documents to create a data stream*
  177. [%collapsible]
  178. ====
  179. The following <<docs-index_,index API>> request targets `logs`, which matches
  180. the wildcard pattern for the `logs_data_stream` template. Because no existing
  181. index or data stream uses this name, this request creates the `logs` data stream
  182. and indexes the document to it.
  183. [source,console]
  184. ----
  185. POST /logs/_doc/
  186. {
  187. "@timestamp": "2020-12-06T11:04:05.000Z",
  188. "user": {
  189. "id": "vlb44hny"
  190. },
  191. "message": "Login attempt failed"
  192. }
  193. ----
  194. // TEST[continued]
  195. The API returns the following response. Note the `_index` property contains
  196. `.ds-logs-000001`, indicating the document was indexed to the write index of the
  197. new `logs` data stream.
  198. [source,console-result]
  199. ----
  200. {
  201. "_index": ".ds-logs-000001",
  202. "_id": "qecQmXIBT4jB8tq1nG0j",
  203. "_version": 1,
  204. "result": "created",
  205. "_shards": {
  206. "total": 2,
  207. "successful": 1,
  208. "failed": 0
  209. },
  210. "_seq_no": 0,
  211. "_primary_term": 1
  212. }
  213. ----
  214. // TESTRESPONSE[s/"_id": "qecQmXIBT4jB8tq1nG0j"/"_id": $body._id/]
  215. ====
  216. --
  217. * Use the <<indices-create-data-stream,create data stream API>> to manually
  218. create a data stream. The name of the data stream must match the
  219. name or wildcard pattern defined in the template's `index_patterns` property.
  220. +
  221. --
  222. .*Example: Manually create a data stream*
  223. [%collapsible]
  224. ====
  225. The following <<indices-create-data-stream,create data stream API>> request
  226. targets `logs_alt`, which matches the wildcard pattern for the
  227. `logs_data_stream` template. Because no existing index or data stream uses this
  228. name, this request creates the `logs_alt` data stream.
  229. [source,console]
  230. ----
  231. PUT /_data_stream/logs_alt
  232. ----
  233. // TEST[continued]
  234. ====
  235. --
  236. ////
  237. [source,console]
  238. ----
  239. DELETE /_data_stream/logs
  240. DELETE /_data_stream/logs_alt
  241. DELETE /_index_template/logs_data_stream
  242. DELETE /_ilm/policy/logs_policy
  243. ----
  244. // TEST[continued]
  245. ////
  246. [discrete]
  247. [[get-info-about-a-data-stream]]
  248. === Get information about a data stream
  249. You can use the <<indices-get-data-stream,get data stream API>> to get
  250. information about one or more data streams, including:
  251. * The timestamp field
  252. * The current backing indices, which is returned as an array. The last item in
  253. the array contains information about the stream's current write index.
  254. * The current generation
  255. This is also handy way to verify that a recently created data stream exists.
  256. .*Example*
  257. [%collapsible]
  258. ====
  259. The following get data stream API request retrieves information about any data
  260. streams starting with `logs`.
  261. [source,console]
  262. ----
  263. GET /_data_stream/logs*
  264. ----
  265. // TEST[skip: shard failures]
  266. The API returns the following response, which includes information about the
  267. `logs` data stream. Note the `indices` property contains an array of the
  268. stream's current backing indices. The last item in this array contains
  269. information for the `logs` stream's write index, `.ds-logs-000002`.
  270. [source,console-result]
  271. ----
  272. [
  273. {
  274. "name": "logs",
  275. "timestamp_field": "@timestamp",
  276. "indices": [
  277. {
  278. "index_name": ".ds-logs-000001",
  279. "index_uuid": "DXAE-xcCQTKF93bMm9iawA"
  280. },
  281. {
  282. "index_name": ".ds-logs-000002",
  283. "index_uuid": "Wzxq0VhsQKyPxHhaK3WYAg"
  284. }
  285. ],
  286. "generation": 2
  287. }
  288. ]
  289. ----
  290. // TESTRESPONSE[skip:unable to assert responses with top level array]
  291. ====
  292. [discrete]
  293. [[delete-a-data-stream]]
  294. === Delete a data stream
  295. You can use the <<indices-delete-data-stream,delete data stream API>> to delete
  296. a data stream and its backing indices.
  297. .*Example*
  298. [%collapsible]
  299. ====
  300. The following delete data stream API request deletes the `logs` data stream. This
  301. request also deletes the stream's backing indices and any data they contain.
  302. ////
  303. [source,console]
  304. ----
  305. PUT /_index_template/logs_data_stream
  306. {
  307. "index_patterns": [ "logs*" ],
  308. "data_stream": {
  309. "timestamp_field": "@timestamp"
  310. },
  311. "template": {
  312. "mappings": {
  313. "properties": {
  314. "@timestamp": {
  315. "type": "date"
  316. }
  317. }
  318. }
  319. }
  320. }
  321. PUT /_data_stream/logs
  322. ----
  323. ////
  324. [source,console]
  325. ----
  326. DELETE /_data_stream/logs
  327. ----
  328. // TEST[continued]
  329. ====
  330. ////
  331. [source,console]
  332. ----
  333. DELETE /_index_template/logs_data_stream
  334. ----
  335. // TEST[continued]
  336. ////