tutorial-manage-new-data-stream.asciidoc 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. [role="xpack"]
  2. [[tutorial-manage-new-data-stream]]
  3. === Tutorial: Create a data stream with a lifecycle
  4. preview::[]
  5. To create a data stream with a built-in lifecycle, follow these steps:
  6. . <<create-index-template-with-lifecycle>>
  7. . <<create-data-stream-with-lifecycle>>
  8. . <<retrieve-lifecycle-information>>
  9. [discrete]
  10. [[create-index-template-with-lifecycle]]
  11. ==== Create an index template
  12. A data stream requires a matching <<index-templates,index template>>. You can configure the data stream lifecycle by
  13. setting the `lifecycle` field in the index template the same as you do for mappings and index settings. You can define an
  14. index template that sets a lifecycle as follows:
  15. * Include the `data_stream` object to enable data streams.
  16. * Define the lifecycle in the template section or include a composable template that defines the lifecycle.
  17. * Use a priority higher than `200` to avoid collisions with built-in templates.
  18. See <<avoid-index-pattern-collisions>>.
  19. You can use the <<indices-put-template,create index template API>>.
  20. [source,console]
  21. --------------------------------------------------
  22. PUT _index_template/my-index-template
  23. {
  24. "index_patterns": ["my-data-stream*"],
  25. "data_stream": { },
  26. "priority": 500,
  27. "template": {
  28. "lifecycle": {
  29. "data_retention": "7d"
  30. }
  31. },
  32. "_meta": {
  33. "description": "Template with data stream lifecycle"
  34. }
  35. }
  36. --------------------------------------------------
  37. [discrete]
  38. [[create-data-stream-with-lifecycle]]
  39. ==== Create a data stream
  40. You can create a data stream in two ways:
  41. . By manually creating the stream using the <<indices-create-data-stream,create data stream API>>. The stream's name must
  42. still match one of your template's index patterns.
  43. +
  44. [source,console]
  45. --------------------------------------------------
  46. PUT _data_stream/my-data-stream
  47. --------------------------------------------------
  48. // TEST[continued]
  49. . By <<add-documents-to-a-data-stream,indexing requests>> that
  50. target the stream's name. This name must match one of your index template's index patterns.
  51. +
  52. [source,console]
  53. --------------------------------------------------
  54. PUT my-data-stream/_bulk
  55. { "create":{ } }
  56. { "@timestamp": "2099-05-06T16:21:15.000Z", "message": "192.0.2.42 - - [06/May/2099:16:21:15 +0000] \"GET /images/bg.jpg HTTP/1.0\" 200 24736" }
  57. { "create":{ } }
  58. { "@timestamp": "2099-05-06T16:25:42.000Z", "message": "192.0.2.255 - - [06/May/2099:16:25:42 +0000] \"GET /favicon.ico HTTP/1.0\" 200 3638" }
  59. --------------------------------------------------
  60. // TEST[continued]
  61. [discrete]
  62. [[retrieve-lifecycle-information]]
  63. ==== Retrieve lifecycle information
  64. You can use the <<data-streams-get-lifecycle,get data stream lifecycle API>> to see the data stream lifecycle of your data stream and
  65. the <<data-streams-explain-lifecycle,explain data stream lifecycle API>> to see the exact state of each backing index.
  66. [source,console]
  67. --------------------------------------------------
  68. GET _data_stream/my-data-stream/_lifecycle
  69. --------------------------------------------------
  70. // TEST[continued]
  71. The result will look like this:
  72. [source,console-result]
  73. --------------------------------------------------
  74. {
  75. "data_streams": [
  76. {
  77. "name": "my-data-stream", <1>
  78. "lifecycle": {
  79. "enabled": true, <2>
  80. "data_retention": "7d", <3>
  81. "effective_retention": "7d", <4>
  82. "retention_determined_by": "data_stream_configuration" <5>
  83. }
  84. }
  85. ]
  86. }
  87. --------------------------------------------------
  88. <1> The name of your data stream.
  89. <2> Shows if the data stream lifecycle is enabled for this data stream.
  90. <3> The desired retention period of the data indexed in this data stream, this means that if there are no other limitations
  91. the data for this data stream will be preserved for at least 7 days.
  92. <4> The effective retention, this means that the data in this data stream will
  93. be kept at least for 7 days. After that {es} can delete it at its own discretion.
  94. <5> The configuration that determined the effective retention.
  95. If you want to see more information about how the data stream lifecycle is applied on individual backing indices use the
  96. <<data-streams-explain-lifecycle,explain data stream lifecycle API>>:
  97. [source,console]
  98. --------------------------------------------------
  99. GET .ds-my-data-stream-*/_lifecycle/explain
  100. --------------------------------------------------
  101. // TEST[continued]
  102. The result will look like this:
  103. [source,console-result]
  104. --------------------------------------------------
  105. {
  106. "indices": {
  107. ".ds-my-data-stream-2023.04.19-000001": {
  108. "index": ".ds-my-data-stream-2023.04.19-000001", <1>
  109. "managed_by_lifecycle": true, <2>
  110. "index_creation_date_millis": 1681918009501,
  111. "time_since_index_creation": "1.6m", <3>
  112. "lifecycle": { <4>
  113. "enabled": true,
  114. "data_retention": "7d",
  115. "effective_retention": "7d",
  116. "retention_determined_by": "data_stream_configuration"
  117. }
  118. }
  119. }
  120. }
  121. --------------------------------------------------
  122. // TESTRESPONSE[skip:the result is for illustrating purposes only]
  123. <1> The name of the backing index.
  124. <2> If it is managed by the built-in data stream lifecycle.
  125. <3> Time since the index was created.
  126. <4> The lifecycle configuration that is applied on this backing index.
  127. //////////////////////////
  128. [source,console]
  129. --------------------------------------------------
  130. DELETE _data_stream/my-data-stream
  131. DELETE _index_template/my-index-template
  132. --------------------------------------------------
  133. // TEST[continued]
  134. //////////////////////////