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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363
  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. * <<create-index-lifecycle-policy>>
  6. * <<create-component-templates>>
  7. * <<create-index-template>>
  8. * <<create-data-stream>>
  9. * <<secure-data-stream>>
  10. You can also <<convert-index-alias-to-data-stream,convert an index alias to
  11. a data stream>>.
  12. IMPORTANT: If you use {fleet} or {agent}, skip this tutorial. {fleet} and
  13. {agent} set up data streams for you. See {fleet}'s
  14. {fleet-guide}/data-streams.html[data streams] documentation.
  15. [discrete]
  16. [[create-index-lifecycle-policy]]
  17. === Step 1. Create an index lifecycle policy
  18. While optional, we recommend using {ilm-init} to automate the management of your
  19. data stream's backing indices. {ilm-init} requires an index lifecycle policy.
  20. To create an index lifecycle policy in {kib}, open the main menu and go to
  21. *Stack Management > Index Lifecycle Policies*. Click *Create policy*.
  22. You can also use the <<ilm-put-lifecycle,create lifecycle policy API>>.
  23. [source,console]
  24. ----
  25. PUT _ilm/policy/my-lifecycle-policy
  26. {
  27. "policy": {
  28. "phases": {
  29. "hot": {
  30. "actions": {
  31. "rollover": {
  32. "max_age": "30d",
  33. "max_primary_shard_size": "50gb"
  34. }
  35. }
  36. },
  37. "warm": {
  38. "min_age": "30d",
  39. "actions": {
  40. "shrink": {
  41. "number_of_shards": 1
  42. },
  43. "forcemerge": {
  44. "max_num_segments": 1
  45. }
  46. }
  47. },
  48. "cold": {
  49. "min_age": "60d",
  50. "actions": {
  51. "searchable_snapshot": {
  52. "snapshot_repository": "my-snapshot-repo"
  53. }
  54. }
  55. },
  56. "frozen": {
  57. "min_age": "90d",
  58. "actions": {
  59. "searchable_snapshot": {
  60. "snapshot_repository": "my-snapshot-repo"
  61. }
  62. }
  63. },
  64. "delete": {
  65. "min_age": "735d",
  66. "actions": {
  67. "delete": {}
  68. }
  69. }
  70. }
  71. }
  72. }
  73. ----
  74. [discrete]
  75. [[create-component-templates]]
  76. === Step 2. Create component templates
  77. A data stream requires a matching index template. In most cases, you compose
  78. this index template using one or more component templates. You typically use
  79. separate component templates for mappings and index settings. This lets you
  80. reuse the component templates in multiple index templates.
  81. When creating your component templates, include:
  82. * A <<date,`date`>> or <<date_nanos,`date_nanos`>> mapping for the `@timestamp`
  83. field. If you don't specify a mapping, {es} maps `@timestamp` as a `date` field
  84. with default options.
  85. * Your lifecycle policy in the `index.lifecycle.name` index setting.
  86. [TIP]
  87. ====
  88. Use the {ecs-ref}[Elastic Common Schema (ECS)] when mapping your fields. ECS
  89. fields integrate with several {stack} features by default.
  90. If you're unsure how to map your fields, use <<runtime-search-request,runtime
  91. fields>> to extract fields from <<mapping-unstructured-content,unstructured
  92. content>> at search time. For example, you can index a log message to a
  93. `wildcard` field and later extract IP addresses and other data from this field
  94. during a search.
  95. ====
  96. To create a component template in {kib}, open the main menu and go to *Stack
  97. Management > Index Management*. In the *Index Templates* view, click *Create
  98. component template*.
  99. You can also use the <<indices-component-template,create component template
  100. API>>.
  101. [source,console]
  102. ----
  103. # Creates a component template for mappings
  104. PUT _component_template/my-mappings
  105. {
  106. "template": {
  107. "mappings": {
  108. "properties": {
  109. "@timestamp": {
  110. "type": "date",
  111. "format": "date_optional_time||epoch_millis"
  112. },
  113. "message": {
  114. "type": "wildcard"
  115. }
  116. }
  117. }
  118. },
  119. "_meta": {
  120. "description": "Mappings for @timestamp and message fields",
  121. "my-custom-meta-field": "More arbitrary metadata"
  122. }
  123. }
  124. # Creates a component template for index settings
  125. PUT _component_template/my-settings
  126. {
  127. "template": {
  128. "settings": {
  129. "index.lifecycle.name": "my-lifecycle-policy"
  130. }
  131. },
  132. "_meta": {
  133. "description": "Settings for ILM",
  134. "my-custom-meta-field": "More arbitrary metadata"
  135. }
  136. }
  137. ----
  138. // TEST[continued]
  139. [discrete]
  140. [[create-index-template]]
  141. === Step 3. Create an index template
  142. Use your component templates to create an index template. Specify:
  143. * One or more index patterns that match the data stream's name. We recommend
  144. using our {fleet-guide}/data-streams.html#data-streams-naming-scheme[data stream
  145. naming scheme].
  146. * That the template is data stream enabled.
  147. * Any component templates that contain your mappings and index settings.
  148. * A priority higher than `200` to avoid collisions with built-in templates.
  149. See <<avoid-index-pattern-collisions>>.
  150. To create an index template in {kib}, open the main menu and go to *Stack
  151. Management > Index Management*. In the *Index Templates* view, click *Create
  152. template*.
  153. You can also use the <<indices-put-template,create index template API>>.
  154. Include the `data_stream` object to enable data streams.
  155. [source,console]
  156. ----
  157. PUT _index_template/my-index-template
  158. {
  159. "index_patterns": ["my-data-stream*"],
  160. "data_stream": { },
  161. "composed_of": [ "my-mappings", "my-settings" ],
  162. "priority": 500,
  163. "_meta": {
  164. "description": "Template for my time series data",
  165. "my-custom-meta-field": "More arbitrary metadata"
  166. }
  167. }
  168. ----
  169. // TEST[continued]
  170. [discrete]
  171. [[create-data-stream]]
  172. === Step 4. Create the data stream
  173. <<add-documents-to-a-data-stream,Indexing requests>> add documents to a data
  174. stream. These requests must use an `op_type` of `create`. Documents must include
  175. a `@timestamp` field.
  176. To automatically create your data stream, submit an indexing request that
  177. targets the stream's name. This name must match one of your index template's
  178. index patterns.
  179. [source,console]
  180. ----
  181. PUT my-data-stream/_bulk
  182. { "create":{ } }
  183. { "@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" }
  184. { "create":{ } }
  185. { "@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" }
  186. POST my-data-stream/_doc
  187. {
  188. "@timestamp": "2099-05-06T16:21:15.000Z",
  189. "message": "192.0.2.42 - - [06/May/2099:16:21:15 +0000] \"GET /images/bg.jpg HTTP/1.0\" 200 24736"
  190. }
  191. ----
  192. // TEST[continued]
  193. You can also manually create the stream using the
  194. <<indices-create-data-stream,create data stream API>>. The stream's name must
  195. still match one of your template's index patterns.
  196. [source,console]
  197. ----
  198. PUT _data_stream/my-data-stream
  199. ----
  200. // TEST[continued]
  201. // TEST[s/my-data-stream/my-data-stream-alt/]
  202. [discrete]
  203. [[secure-data-stream]]
  204. === Step 5. Secure the data stream
  205. include::{xes-repo-dir}/security/authorization/alias-privileges.asciidoc[tag=data-stream-security]
  206. For an example, see <<data-stream-privileges>>.
  207. [discrete]
  208. [[convert-index-alias-to-data-stream]]
  209. === Convert an index alias to a data stream
  210. // tag::time-series-alias-tip[]
  211. Prior to {es} 7.9, you would typically use an <<indices-aliases,index alias>>
  212. with a write index to manage time series data. Data streams replace this
  213. functionality, require less maintenance, and automatically integrate with
  214. <<data-tiers,data tiers>>.
  215. // end::time-series-alias-tip[]
  216. To convert an index alias with a write index to a data stream with the same
  217. name, use the <<indices-migrate-to-data-stream,migrate to data stream API>>.
  218. During conversion, the alias’s indices become hidden backing indices for the
  219. stream. The alias’s write index becomes the stream’s write index. The stream
  220. still requires a matching index template with data stream enabled.
  221. ////
  222. [source,console]
  223. ----
  224. POST idx1/_doc/
  225. {
  226. "message" : "testing",
  227. "@timestamp" : "2099-01-01"
  228. }
  229. POST idx2/_doc/
  230. {
  231. "message" : "testing2",
  232. "@timestamp" : "2099-01-01"
  233. }
  234. POST _aliases
  235. {
  236. "actions": [
  237. {
  238. "add": {
  239. "index": "idx1",
  240. "alias": "my-time-series-data",
  241. "is_write_index": true
  242. }
  243. },
  244. {
  245. "add": {
  246. "index": "idx2",
  247. "alias": "my-time-series-data"
  248. }
  249. }
  250. ]
  251. }
  252. PUT _index_template/template
  253. {
  254. "index_patterns": ["my-time-series-data"],
  255. "data_stream": { }
  256. }
  257. ----
  258. // TEST[continued]
  259. ////
  260. [source,console]
  261. ----
  262. POST _data_stream/_migrate/my-time-series-data
  263. ----
  264. // TEST[continued]
  265. [discrete]
  266. [[get-info-about-data-stream]]
  267. === Get information about a data stream
  268. To get information about a data stream in {kib}, open the main menu and go to
  269. *Stack Management > Index Management*. In the *Data Streams* view, click the
  270. data stream's name.
  271. You can also use the <<indices-get-data-stream,get data stream API>>.
  272. ////
  273. [source,console]
  274. ----
  275. POST my-data-stream/_rollover/
  276. ----
  277. // TEST[continued]
  278. ////
  279. [source,console]
  280. ----
  281. GET _data_stream/my-data-stream
  282. ----
  283. // TEST[continued]
  284. [discrete]
  285. [[delete-data-stream]]
  286. === Delete a data stream
  287. To delete a data stream and its backing indices in {kib}, open the main menu and
  288. go to *Stack Management > Index Management*. In the *Data Streams* view, click
  289. the trash icon. The icon only displays if you have the `delete_index`
  290. <<security-privileges, security privilege>> for the data stream.
  291. You can also use the <<indices-delete-data-stream,delete data stream API>>.
  292. [source,console]
  293. ----
  294. DELETE _data_stream/my-data-stream
  295. ----
  296. // TEST[continued]
  297. ////
  298. [source,console]
  299. ----
  300. DELETE _data_stream/*
  301. DELETE _index_template/*
  302. DELETE _component_template/my-*
  303. DELETE _ilm/policy/my-lifecycle-policy
  304. ----
  305. // TEST[continued]
  306. ////