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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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. }
  82. }
  83. ]
  84. }
  85. --------------------------------------------------
  86. <1> The name of your data stream.
  87. <2> Shows if the data stream lifecycle is enabled for this data stream.
  88. <3> The retention period of the data indexed in this data stream, this means that the data in this data stream will
  89. be kept at least for 7 days. After that {es} can delete it at its own discretion.
  90. If you want to see more information about how the data stream lifecycle is applied on individual backing indices use the
  91. <<data-streams-explain-lifecycle,explain data stream lifecycle API>>:
  92. [source,console]
  93. --------------------------------------------------
  94. GET .ds-my-data-stream-*/_lifecycle/explain
  95. --------------------------------------------------
  96. // TEST[continued]
  97. The result will look like this:
  98. [source,console-result]
  99. --------------------------------------------------
  100. {
  101. "indices": {
  102. ".ds-my-data-stream-2023.04.19-000001": {
  103. "index": ".ds-my-data-stream-2023.04.19-000001", <1>
  104. "managed_by_lifecycle": true, <2>
  105. "index_creation_date_millis": 1681918009501,
  106. "time_since_index_creation": "1.6m", <3>
  107. "lifecycle": { <4>
  108. "enabled": true,
  109. "data_retention": "7d"
  110. }
  111. }
  112. }
  113. }
  114. --------------------------------------------------
  115. // TESTRESPONSE[skip:the result is for illustrating purposes only]
  116. <1> The name of the backing index.
  117. <2> If it is managed by the built-in data stream lifecycle.
  118. <3> Time since the index was created.
  119. <4> The lifecycle configuration that is applied on this backing index.
  120. //////////////////////////
  121. [source,console]
  122. --------------------------------------------------
  123. DELETE _data_stream/my-data-stream
  124. DELETE _index_template/my-index-template
  125. --------------------------------------------------
  126. // TEST[continued]
  127. //////////////////////////