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

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