set-up-tsds.asciidoc 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357
  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. * The <<tsds-look-back-time,`index.look_back_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. * That the template is data stream enabled.
  190. * An `index.mode` object set to `time_series`.
  191. * Optional: The `index.routing_path` index setting. The setting value should
  192. only match plain `keyword` dimension fields and should be set directly in the
  193. index template. When not defined explicitly, the `index.routing_path` setting is
  194. generated from the mapping using all mappings that are set with
  195. `time_series_dimension` set to `true`.
  196. * The component templates containing your mappings and other index settings.
  197. * A priority higher than `200` to avoid collisions with built-in templates.
  198. See <<avoid-index-pattern-collisions>>.
  199. [source,console]
  200. ----
  201. PUT _index_template/my-weather-sensor-index-template
  202. {
  203. "index_patterns": ["metrics-weather_sensors-*"],
  204. "data_stream": { },
  205. "template": {
  206. "settings": {
  207. "index.mode": "time_series",
  208. "index.routing_path": [ "sensor_id", "location" ]
  209. }
  210. },
  211. "composed_of": [ "my-weather-sensor-mappings", "my-weather-sensor-settings" ],
  212. "priority": 500,
  213. "_meta": {
  214. "description": "Template for my weather sensor data"
  215. }
  216. }
  217. ----
  218. // TEST[continued]
  219. ////
  220. [source,console]
  221. ----
  222. DELETE _data_stream/*
  223. DELETE _index_template/*
  224. DELETE _component_template/my-*
  225. DELETE _ilm/policy/my-weather-sensor-lifecycle-policy
  226. ----
  227. // TEST[continued]
  228. ////
  229. [discrete]
  230. [[create-tsds]]
  231. ==== Create the TSDS
  232. <<add-documents-to-a-data-stream,Indexing requests>> add documents to a TSDS.
  233. Documents in a TSDS must include:
  234. * A `@timestamp` field
  235. * One or more dimension fields. At least one dimension must be a `keyword` field
  236. that matches the `index.routing_path` index setting, if specified. If not specified
  237. explicitly, `index.routing_path` is set automatically to whichever mappings have
  238. `time_series_dimension` set to `true`.
  239. To automatically create your TSDS, submit an indexing request that
  240. targets the TSDS's name. This name must match one of your index template's
  241. index patterns.
  242. IMPORTANT: To test the following example, update the timestamps to within three hours of
  243. your current time. Data added to a TSDS must always fall within an
  244. <<tsds-accepted-time-range,accepted time range>>.
  245. [source,console]
  246. ----
  247. PUT metrics-weather_sensors-dev/_bulk
  248. { "create":{ } }
  249. { "@timestamp": "2099-05-06T16:21:15.000Z", "sensor_id": "HAL-000001", "location": "plains", "temperature": 26.7,"humidity": 49.9 }
  250. { "create":{ } }
  251. { "@timestamp": "2099-05-06T16:25:42.000Z", "sensor_id": "SYKENET-000001", "location": "swamp", "temperature": 32.4, "humidity": 88.9 }
  252. POST metrics-weather_sensors-dev/_doc
  253. {
  254. "@timestamp": "2099-05-06T16:21:15.000Z",
  255. "sensor_id": "SYKENET-000001",
  256. "location": "swamp",
  257. "temperature": 32.4,
  258. "humidity": 88.9
  259. }
  260. ----
  261. // TEST[skip: The @timestamp value won't match an accepted range in the TSDS]
  262. You can also manually create the TSDS using the
  263. <<indices-create-data-stream,create data stream API>>. The TSDS's name must
  264. still match one of your template's index patterns.
  265. [source,console]
  266. ----
  267. PUT _data_stream/metrics-weather_sensors-dev
  268. ----
  269. // TEST[setup:tsds_template]
  270. // TEST[teardown:tsds_cleanup]
  271. [discrete]
  272. [[secure-tsds]]
  273. ==== Secure the TSDS
  274. Use <<privileges-list-indices,index privileges>> to control access to a TSDS.
  275. Granting privileges on a TSDS grants the same privileges on its backing indices.
  276. For an example, refer to <<data-stream-privileges>>.
  277. [discrete]
  278. [[convert-existing-data-stream-to-tsds]]
  279. ==== Convert an existing data stream to a TSDS
  280. You can also use the above steps to convert an existing regular data stream to
  281. a TSDS. In this case, you'll want to:
  282. * Edit your existing index lifecycle policy, component templates, and index
  283. templates instead of creating new ones.
  284. * Instead of creating the TSDS, manually roll over its write index. This ensures
  285. the current write index and any new backing indices have an
  286. <<time-series-mode,`index.mode` of `time_series`>>.
  287. +
  288. You can manually roll over the write index using the
  289. <<indices-rollover-index,rollover API>>.
  290. +
  291. [source,console]
  292. ----
  293. POST metrics-weather_sensors-dev/_rollover
  294. ----
  295. // TEST[setup:tsds]
  296. // TEST[teardown:tsds_cleanup]
  297. [discrete]
  298. [[set-up-tsds-whats-next]]
  299. ==== What's next?
  300. Now that you've set up your TSDS, you can manage and use it like a regular
  301. data stream. For more information, refer to:
  302. * <<use-a-data-stream>>
  303. * <<data-streams-change-mappings-and-settings>>
  304. * <<data-stream-apis,data stream APIs>>