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

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