Documentation

在 Flux 中提取标量值

使用 Flux 动态查询函数 从 Flux 查询输出中提取标量值。例如,这使你可以使用查询结果动态设置变量。

从输出中提取标量值

  1. 从输入流中提取列 从输入流中提取行
  2. 使用返回的数组或记录来引用标量值。

此页面上的示例使用下面提供的示例数据

输出标量值

InfluxDB /api/v2/query HTTP API 端点以及使用它的所有客户端(InfluxDB UI、influx CLI 等)仅支持返回表流的查询。此端点不支持原始标量输出。

要将标量值作为表流的一部分输出

  1. 导入 array
  2. 使用 array.from()display() 将标量值的字面表示形式包装在表流中,并将其作为输出返回。

查看示例

表提取

Flux 将查询结果格式化为表流。findColumn()findRecord() 都提取表流中的第一个表,其分组键值与 fn 谓词函数匹配。

提取正确的表

Flux 函数不保证表顺序。findColumn()findRecord() 仅提取与 fn 谓词匹配的 第一个 表。要提取正确的表,请使用 fn 谓词函数专门标识要提取的表,或过滤和转换你的数据,以最大程度地减少管道传输到函数中的表数量。

提取列

使用 findColumn() 函数输出从提取表中特定列的值数组。

请参阅下面的示例数据

sampleData
    |> findColumn(
        fn: (key) => key._field == "temp" and key.location == "sfo",
        column: "_value",
    )

// Returns [65.1, 66.2, 66.3, 66.8]
  • Copy
  • Fill window

要从 InfluxDB 查询 API、InfluxDB UI 或 influx CLI 返回此值,请参阅输出标量值

使用提取的列值

使用变量来存储值数组。在下面的示例中,SFOTemps 代表值数组。引用数组中的特定索引(从 0 开始的整数)以返回该索引处的值。

请参阅下面的示例数据

SFOTemps = sampleData
    |> findColumn(
        fn: (key) => key._field == "temp" and key.location == "sfo",
        column: "_value",
    )

SFOTemps
// Returns [65.1, 66.2, 66.3, 66.8]

SFOTemps[0]
// Returns 65.1

SFOTemps[2]
// Returns 66.3
  • Copy
  • Fill window

要从 InfluxDB 查询 API、InfluxDB UI 或 influx CLI 返回此值,请参阅输出标量值

提取行

使用 findRecord() 函数输出从提取表中单行的数据。使用 idx 参数指定要输出的行的索引。该函数输出一个记录,其中包含每列的键值对。

sampleData
    |> findRecord(
        fn: (key) => key._field == "temp" and key.location == "sfo",
        idx: 0,
    )

// Returns {
//   _time:2019-11-11T12:00:00Z,
//   _field:"temp",
//   location:"sfo",
//   _value: 65.1
// }
  • Copy
  • Fill window

要从 InfluxDB 查询 API、InfluxDB UI 或 influx CLI 返回此值,请参阅输出标量值

使用提取的行记录

使用变量来存储提取的行记录。在下面的示例中,tempInfo 代表提取的行。使用 点或方括号表示法来引用记录中的键。

tempInfo = sampleData
    |> findRecord(
        fn: (key) => key._field == "temp" and key.location == "sfo",
        idx: 0,
    )

tempInfo
// Returns {
//   _time:2019-11-11T12:00:00Z,
//   _field:"temp",
//   location:"sfo",
//   _value: 65.1
// }

tempInfo._time
// Returns 2019-11-11T12:00:00Z

tempInfo.location
// Returns sfo
  • Copy
  • Fill window

要从 InfluxDB 查询 API、InfluxDB UI 或 influx CLI 返回此值,请参阅输出标量值

示例辅助函数

创建自定义辅助函数以从查询输出中提取标量值。

提取标量字段值
// Define a helper function to extract field values
getFieldValue = (tables=<-, field) => {
    extract = tables
        |> findColumn(fn: (key) => key._field == field, column: "_value")

    return extract[0]
}

// Use the helper function to define a variable
lastJFKTemp = sampleData
    |> filter(fn: (r) => r.location == "kjfk")
    |> last()
    |> getFieldValue(field: "temp")

lastJFKTemp
// Returns 71.2
  • Copy
  • Fill window

要从 InfluxDB 查询 API、InfluxDB UI 或 influx CLI 返回此值,请参阅输出标量值

提取标量行数据
// Define a helper function to extract a row as a record
getRow = (tables=<-, field, idx=0) => {
    extract = tables
        |> findRecord(fn: (key) => true, idx: idx)

    return extract
}

// Use the helper function to define a variable
lastReported = sampleData
    |> last()
    |> getRow(field: "temp")

"The last location to report was ${lastReported.location}.
The temperature was ${string(v: lastReported._value)}°F."

// Returns:
// The last location to report was kord.
// The temperature was 38.9°F.
  • Copy
  • Fill window

要从 InfluxDB 查询 API、InfluxDB UI 或 influx CLI 返回此值,请参阅输出标量值


示例数据

以下示例数据集表示从三个位置收集的虚构温度指标。它被格式化为 Flux 记录数组,并使用 array.from() 函数构造为稳定流。

将以下内容放在查询的开头以使用示例数据

import "array"

sampleData =
    array.from(
        rows: [
            {_time: 2019-11-01T12:00:00Z, location: "sfo", _field: "temp", _value: 65.1},
            {_time: 2019-11-01T13:00:00Z, location: "sfo", _field: "temp", _value: 66.2},
            {_time: 2019-11-01T14:00:00Z, location: "sfo", _field: "temp", _value: 66.3},
            {_time: 2019-11-01T15:00:00Z, location: "sfo", _field: "temp", _value: 66.8},
            {_time: 2019-11-01T12:00:00Z, location: "kjfk", _field: "temp", _value: 69.4},
            {_time: 2019-11-01T13:00:00Z, location: "kjfk", _field: "temp", _value: 69.9},
            {_time: 2019-11-01T14:00:00Z, location: "kjfk", _field: "temp", _value: 71.0},
            {_time: 2019-11-01T15:00:00Z, location: "kjfk", _field: "temp", _value: 71.2},
            {_time: 2019-11-01T12:00:00Z, location: "kord", _field: "temp", _value: 46.4},
            {_time: 2019-11-01T13:00:00Z, location: "kord", _field: "temp", _value: 46.3},
            {_time: 2019-11-01T14:00:00Z, location: "kord", _field: "temp", _value: 42.7},
            {_time: 2019-11-01T15:00:00Z, location: "kord", _field: "temp", _value: 38.9},
        ],
    )
    |> group(columns: ["location", "_field"])
  • Copy
  • Fill window

此页面是否对您有帮助?

感谢您的反馈!


Flux 的未来

Flux 即将进入维护模式。您可以继续像现在一样使用它,而无需对您的代码进行任何更改。

阅读更多

InfluxDB 3 开源版现已公开发布 Alpha 版本

InfluxDB 3 开源版现已可用于 Alpha 测试,根据 MIT 或 Apache 2 许可授权。

我们正在发布两个作为 Alpha 版本一部分的产品。

InfluxDB 3 Core 是我们新的开源产品。它是用于时间序列和事件数据的最新数据引擎。InfluxDB 3 Enterprise 是一个商业版本,它建立在 Core 的基础上,增加了历史查询功能、读取副本、高可用性、可扩展性和细粒度的安全性。

有关如何开始使用的更多信息,请查看