set-up-tsds.asciidoc 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356
  1. [[set-up-tsds]]
  2. === Set up a time series data stream (TSDS)
  3. ++++
  4. <titleabbrev>Set up a TSDS</titleabbrev>
  5. ++++
  6. To set up a <<tsds,time series data stream (TSDS)>>, follow these steps:
  7. . Check the <<tsds-prereqs,prerequisites>>.
  8. . <<tsds-ilm-policy>>.
  9. . <<tsds-create-mappings-component-template>>.
  10. . <<tsds-create-index-settings-component-template>>.
  11. . <<create-tsds-index-template>>.
  12. . <<create-tsds>>.
  13. . <<secure-tsds>>.
  14. [discrete]
  15. [[tsds-prereqs]]
  16. ==== Prerequisites
  17. * Before you create a TSDS, you should be familiar with <<data-streams,data
  18. streams>> and <<tsds,TSDS concepts>>.
  19. * To follow this tutorial, you must have the following permissions:
  20. ** <<privileges-list-cluster,Cluster privileges>>: `manage_ilm` and
  21. `manage_index_templates`.
  22. ** <<privileges-list-indices,Index privileges>>: `create_doc` and `create_index`
  23. for any TSDS you create or convert. To roll over a TSDS, you must have the
  24. `manage` privilege.
  25. [discrete]
  26. [[tsds-ilm-policy]]
  27. ==== Create an index lifecycle policy
  28. While optional, we recommend using {ilm-init} to automate the management of your
  29. TSDS's backing indices. {ilm-init} requires an index lifecycle policy.
  30. We recommend you specify a `max_age` criteria for the `rollover` action in the
  31. policy. This ensures the <<time-bound-indices,`@timestamp` ranges>> for the
  32. TSDS's backing indices are consistent. For example, setting a `max_age` of `1d`
  33. for the `rollover` action ensures your backing indices consistently contain one
  34. day's worth of data.
  35. ////
  36. [source,console]
  37. ----
  38. PUT /_snapshot/found-snapshots
  39. {
  40. "type": "fs",
  41. "settings": {
  42. "location": "my_backup_location"
  43. }
  44. }
  45. ----
  46. // TESTSETUP
  47. ////
  48. [source,console]
  49. ----
  50. PUT _ilm/policy/my-weather-sensor-lifecycle-policy
  51. {
  52. "policy": {
  53. "phases": {
  54. "hot": {
  55. "actions": {
  56. "rollover": {
  57. "max_age": "1d",
  58. "max_primary_shard_size": "50gb"
  59. }
  60. }
  61. },
  62. "warm": {
  63. "min_age": "30d",
  64. "actions": {
  65. "shrink": {
  66. "number_of_shards": 1
  67. },
  68. "forcemerge": {
  69. "max_num_segments": 1
  70. }
  71. }
  72. },
  73. "cold": {
  74. "min_age": "60d",
  75. "actions": {
  76. "searchable_snapshot": {
  77. "snapshot_repository": "found-snapshots"
  78. }
  79. }
  80. },
  81. "frozen": {
  82. "min_age": "90d",
  83. "actions": {
  84. "searchable_snapshot": {
  85. "snapshot_repository": "found-snapshots"
  86. }
  87. }
  88. },
  89. "delete": {
  90. "min_age": "735d",
  91. "actions": {
  92. "delete": {}
  93. }
  94. }
  95. }
  96. }
  97. }
  98. ----
  99. [discrete]
  100. [[tsds-create-mappings-component-template]]
  101. ==== Create a mappings component template
  102. A TSDS requires a matching index template. In most cases, you compose this index
  103. template using one or more component templates. You typically use separate
  104. component templates for mappings and index settings. This lets you reuse the
  105. component templates in multiple index templates.
  106. For a TSDS, the mappings component template must include mappings for:
  107. * One or more <<time-series-dimension,dimension fields>> with a
  108. `time_series_dimension` value of `true`. At least one of these dimensions must
  109. be a plain `keyword` field.
  110. Optionally, the template can also include mappings for:
  111. * One or more <<time-series-metric,metric fields>>, marked using the
  112. `time_series_metric` mapping parameter.
  113. * A `date` or `date_nanos` mapping for the `@timestamp` field. If you don’t
  114. specify a mapping, Elasticsearch maps `@timestamp` as a `date` field with
  115. default options.
  116. [source,console]
  117. ----
  118. PUT _component_template/my-weather-sensor-mappings
  119. {
  120. "template": {
  121. "mappings": {
  122. "properties": {
  123. "sensor_id": {
  124. "type": "keyword",
  125. "time_series_dimension": true
  126. },
  127. "location": {
  128. "type": "keyword",
  129. "time_series_dimension": true
  130. },
  131. "temperature": {
  132. "type": "half_float",
  133. "time_series_metric": "gauge"
  134. },
  135. "humidity": {
  136. "type": "half_float",
  137. "time_series_metric": "gauge"
  138. },
  139. "@timestamp": {
  140. "type": "date",
  141. "format": "strict_date_optional_time"
  142. }
  143. }
  144. }
  145. },
  146. "_meta": {
  147. "description": "Mappings for weather sensor data"
  148. }
  149. }
  150. ----
  151. // TEST[continued]
  152. [discrete]
  153. [[tsds-create-index-settings-component-template]]
  154. ==== Create an index settings component template
  155. Optionally, the index settings component template for a TSDS can include:
  156. * Your lifecycle policy in the `index.lifecycle.name` index setting.
  157. * The <<tsds-look-ahead-time,`index.look_ahead_time`>> index setting.
  158. * Other index settings, such as <<index-codec,`index.codec`>>, for your TSDS's
  159. backing indices.
  160. IMPORTANT: Don't specify the `index.routing_path` index setting in a component
  161. template. If you choose, you can configure `index.routing_path` directly in the
  162. index template, as shown in the following step.
  163. [source,console]
  164. ----
  165. PUT _component_template/my-weather-sensor-settings
  166. {
  167. "template": {
  168. "settings": {
  169. "index.lifecycle.name": "my-lifecycle-policy",
  170. "index.look_ahead_time": "3h",
  171. "index.codec": "best_compression"
  172. }
  173. },
  174. "_meta": {
  175. "description": "Index settings for weather sensor data"
  176. }
  177. }
  178. ----
  179. // TEST[continued]
  180. [discrete]
  181. [[create-tsds-index-template]]
  182. ==== Create an index template
  183. Use your component templates to create an index template. In the index template,
  184. specify:
  185. * One or more index patterns that match the TSDS's name. We recommend
  186. using our {fleet-guide}/data-streams.html#data-streams-naming-scheme[data stream
  187. naming scheme].
  188. * That the template is data stream enabled.
  189. * An `index.mode` object set to `time_series`.
  190. * Optional: The `index.routing_path` index setting. The setting value should
  191. only match plain `keyword` dimension fields and should be set directly in the
  192. index template. When not defined explicitly, the `index.routing_path` setting is
  193. generated from the mapping using all mappings that are set with
  194. `time_series_dimension` set to `true`.
  195. * The component templates containing your mappings and other index settings.
  196. * A priority higher than `200` to avoid collisions with built-in templates.
  197. See <<avoid-index-pattern-collisions>>.
  198. [source,console]
  199. ----
  200. PUT _index_template/my-weather-sensor-index-template
  201. {
  202. "index_patterns": ["metrics-weather_sensors-*"],
  203. "data_stream": { },
  204. "template": {
  205. "settings": {
  206. "index.mode": "time_series",
  207. "index.routing_path": [ "sensor_id", "location" ]
  208. }
  209. },
  210. "composed_of": [ "my-weather-sensor-mappings", "my-weather-sensor-settings" ],
  211. "priority": 500,
  212. "_meta": {
  213. "description": "Template for my weather sensor data"
  214. }
  215. }
  216. ----
  217. // TEST[continued]
  218. ////
  219. [source,console]
  220. ----
  221. DELETE _data_stream/*
  222. DELETE _index_template/*
  223. DELETE _component_template/my-*
  224. DELETE _ilm/policy/my-weather-sensor-lifecycle-policy
  225. ----
  226. // TEST[continued]
  227. ////
  228. [discrete]
  229. [[create-tsds]]
  230. ==== Create the TSDS
  231. <<add-documents-to-a-data-stream,Indexing requests>> add documents to a TSDS.
  232. Documents in a TSDS must include:
  233. * A `@timestamp` field
  234. * One or more dimension fields. At least one dimension must be a `keyword` field
  235. that matches the `index.routing_path` index setting, if specified. If not specified
  236. explicitly, `index.routing_path` is set automatically to whichever mappings have
  237. `time_series_dimension` set to `true`.
  238. To automatically create your TSDS, submit an indexing request that
  239. targets the TSDS's name. This name must match one of your index template's
  240. index patterns.
  241. IMPORTANT: To test the following example, update the timestamps to within three hours of
  242. your current time. Data added to a TSDS must always fall within an
  243. <<tsds-accepted-time-range,accepted time range>>.
  244. [source,console]
  245. ----
  246. PUT metrics-weather_sensors-dev/_bulk
  247. { "create":{ } }
  248. { "@timestamp": "2099-05-06T16:21:15.000Z", "sensor_id": "HAL-000001", "location": "plains", "temperature": 26.7,"humidity": 49.9 }
  249. { "create":{ } }
  250. { "@timestamp": "2099-05-06T16:25:42.000Z", "sensor_id": "SYKENET-000001", "location": "swamp", "temperature": 32.4, "humidity": 88.9 }
  251. POST metrics-weather_sensors-dev/_doc
  252. {
  253. "@timestamp": "2099-05-06T16:21:15.000Z",
  254. "sensor_id": "SYKENET-000001",
  255. "location": "swamp",
  256. "temperature": 32.4,
  257. "humidity": 88.9
  258. }
  259. ----
  260. // TEST[skip: The @timestamp value won't match an accepted range in the TSDS]
  261. You can also manually create the TSDS using the
  262. <<indices-create-data-stream,create data stream API>>. The TSDS's name must
  263. still match one of your template's index patterns.
  264. [source,console]
  265. ----
  266. PUT _data_stream/metrics-weather_sensors-dev
  267. ----
  268. // TEST[setup:tsds_template]
  269. // TEST[teardown:tsds_cleanup]
  270. [discrete]
  271. [[secure-tsds]]
  272. ==== Secure the TSDS
  273. Use <<privileges-list-indices,index privileges>> to control access to a TSDS.
  274. Granting privileges on a TSDS grants the same privileges on its backing indices.
  275. For an example, refer to <<data-stream-privileges>>.
  276. [discrete]
  277. [[convert-existing-data-stream-to-tsds]]
  278. ==== Convert an existing data stream to a TSDS
  279. You can also use the above steps to convert an existing regular data stream to
  280. a TSDS. In this case, you'll want to:
  281. * Edit your existing index lifecycle policy, component templates, and index
  282. templates instead of creating new ones.
  283. * Instead of creating the TSDS, manually roll over its write index. This ensures
  284. the current write index and any new backing indices have an
  285. <<time-series-mode,`index.mode` of `time_series`>>.
  286. +
  287. You can manually roll over the write index using the
  288. <<indices-rollover-index,rollover API>>.
  289. +
  290. [source,console]
  291. ----
  292. POST metrics-weather_sensors-dev/_rollover
  293. ----
  294. // TEST[setup:tsds]
  295. // TEST[teardown:tsds_cleanup]
  296. [discrete]
  297. [[set-up-tsds-whats-next]]
  298. ==== What's next?
  299. Now that you've set up your TSDS, you can manage and use it like a regular
  300. data stream. For more information, refer to:
  301. * <<use-a-data-stream>>
  302. * <<data-streams-change-mappings-and-settings>>
  303. * <<data-stream-apis,data stream APIs>>