set-up-tsds.asciidoc 8.7 KB

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