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

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