set-up-tsds.asciidoc 9.6 KB

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