加载中

设置时间序列数据流

本页面向您展示如何手动设置时序数据流 (TSDS)。

注意

如果您正在处理 OpenTelemetry 数据,请尝试 OpenTelemetry 快速入门

  1. 创建索引生命周期策略(可选)

    在大多数情况下,您可以使用数据流生命周期来管理您的时序数据流。如果您在 Elastic Stack 中使用数据层,则可以使用索引生命周期管理

  2. 创建索引模板

    时序数据流的结构由索引模板定义。请创建包含以下必需元素和设置的索引模板

    • 索引模式 (Index patterns):一个或多个与您的 TSDS 名称匹配的通配符模式,例如 weather-sensors-*。为获得最佳效果,请使用数据流命名方案
    • 数据流对象 (Data stream object):模板必须包含 "data_stream": {}
    • 时序模式 (Time series mode):设置 index.mode: time_series
    • 字段映射 (Field mappings):至少定义一个维度字段,通常还需定义一个或多个指标字段
      • 维度 (Dimensions):要定义维度,请将 time_series_dimension 设置为 true。有关详细信息,请参阅维度
      • 指标 (Metrics):要定义指标,请使用 time_series_metric 映射参数。有关详细信息,请参阅指标
      • 时间戳 (Timestamp)(可选):为 @timestamp 字段定义 datedate_nanos 映射。如果您未指定映射,Elasticsearch 会将 @timestamp 映射为带有默认选项的 date 字段。
      • 生命周期管理 (Lifecycle management):如果您正在使用 Elastic Stack,请定义生命周期以启用自动滚动更新 (rollover),并防止索引增长过大。
        • 添加一个 lifecycle 对象并指定 "enabled": true
        • 如果您在第 1 步中创建了 ILM 策略,请在设置中使用 index.lifecycle.name 引用它。
      • 设置 (Settings)(可选):为数据流的支持索引定义任何相关的索引设置,例如 index.number_of_replicas
    • 优先级 (Priority):将优先级设置为高于 200,以避免与内置模板发生冲突

    索引模板 PUT 请求示例

    				PUT _index_template/my-weather-sensor-index-template
    					{
      "index_patterns": ["metrics-weather_sensors-*"],
      "data_stream": { },
      "template": {
        "settings": {
          "index.mode": "time_series",
          "index.lifecycle.name": "my-lifecycle-policy"
        },
        "lifecycle": { 
          "enabled": true
        },
        "mappings": {
          "properties": {
            "sensor_id": {
              "type": "keyword",
              "time_series_dimension": true
            },
            "location": {
              "type": "keyword",
              "time_series_dimension": true
            },
            "temperature": {
              "type": "half_float",
              "time_series_metric": "gauge"
            },
            "humidity": {
              "type": "half_float",
              "time_series_metric": "gauge"
            },
            "@timestamp": {
              "type": "date"
            }
          }
        }
      },
      "priority": 500,
      "_meta": {
        "description": "Template for my weather sensor data"
      }
    }
    		
    1. 仅限 Elastic Stack
    2. 仅限 Elastic Stack
    重要

    如果未启用生命周期管理,时序数据流可能会增长为永不滚动更新的超大索引。这会导致性能问题。请务必为 Elastic Stack 生产部署配置生命周期管理。

  3. 创建时序数据流并添加数据

    创建索引模板后,您可以通过索引文档来创建时序数据流。只要索引名称与索引模板模式匹配,TSDS 就会在您索引第一个文档时自动创建。您可以使用批量 API 请求或 POST 请求。

    重要提示

    要测试以下 _bulk 示例,请将时间戳更新为您当前时间的两小时内。添加到 TSDS 的数据必须符合接受的时间范围

    				PUT metrics-weather-sensors/_bulk
    					{ "create":{ } }
    { "@timestamp": "2099-05-06T16:21:15.000Z", "sensor_id": "SENSOR-001", "location": "warehouse-A", "temperature": 26.7,"humidity": 49.9 }
    { "create":{ } }
    { "@timestamp": "2099-05-06T16:25:42.000Z", "sensor_id": "SENSOR-002", "location": "warehouse-B", "temperature": 32.4, "humidity": 88.9 }
    		
    				POST metrics-weather-sensors/_doc
    					{
      "@timestamp": "2099-05-06T16:21:15.000Z",
      "sensor_id": "SENSOR-00002",
      "location": "warehouse-B",
      "temperature": 32.4,
      "humidity": 88.9
    }
    		
  4. 验证设置

    为确保您的时序数据流正常工作,请尝试一些 GET 请求。

    查看数据流详细信息

    				GET _data_stream/metrics-prod 
    		

    检查时序数据流中的文档计数

    				GET metrics-prod/_count 
    		

    查询时序数据

    				GET metrics-prod/_search 
    					{
      "size": 5,
      "sort": ["@timestamp"]
    }
    		
  5. 您可以将现有的常规数据流转换为 TSDS。请按照以下步骤操作

    1. 更新您现有的索引模板和组件模板(如果有),以包含时序设置。对于 Elastic Stack,请配置生命周期管理。
    2. 使用滚动更新 API (rollover API) 手动滚动更新现有数据流的写入索引,以应用您在第 1 步中所做的更改
    				POST metrics-weather-sensors/_rollover
    		
    注意

    滚动更新后,新的支持索引将具有时序功能。现有的支持索引不受滚动更新影响(因为它们的 index.mode 无法更改)。

    要控制对 TSDS 的访问,请使用索引权限。在 TSDS 上设置的权限也适用于支持索引。

    有关示例,请参阅数据流权限

    现在您已经设置了时序数据流,可以像管理常规数据流一样管理和使用它。有关详细信息,请参阅

© . This website operates independently and is not affiliated with or endorsed by Elasticsearch B.V. All brand names, logos, and trademarks are the property of their respective owners.