文档文档

在 Flux 中提取标量值

此页面记录了早期版本的 InfluxDB OSS。InfluxDB OSS v2 是最新的稳定版本。请参阅等效的 InfluxDB v2 文档: 在 Flux 中提取标量值

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

从输出中提取标量值

  1. 提取表.
  2. 从表中提取列 从表中提取行

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

当前限制

  • InfluxDB 用户界面 (UI) 当前不支持原始标量输出。使用 map() 将标量值添加到输出数据。
  • Flux REPL 当前不支持 Flux 流和表函数(也称为“动态查询”)。请参阅 #15321

提取表

Flux 将查询结果格式化为表流。要从表流中提取标量值,您必须首先提取单个表。

从表流中提取单个表。

如果查询结果仅包含一个表,它仍然格式化为表流。您仍然必须从流中提取该表。

使用 tableFind() 提取其组键值与 fn 谓词函数匹配的第一个表。谓词函数需要一个 key 记录,该记录表示每个表的组键。

sampleData
  |> tableFind(fn: (key) =>
      key._field == "temp" and
      key.location == "sfo"
  )

上面的示例返回一个表

_timelocation_field_value
2019-11-01T12:00:00Zsfotemp65.1
2019-11-01T13:00:00Zsfotemp66.2
2019-11-01T14:00:00Zsfotemp66.3
2019-11-01T15:00:00Zsfotemp66.8

提取正确的表

Flux 函数不保证表顺序,并且 tableFind() 仅返回与 fn 谓词匹配的第一个表。要提取包含您实际想要的数据的表,请在您的谓词函数中非常具体,或者筛选和转换您的数据,以最大限度地减少管道传输到 tableFind() 中的表数量。

从表中提取列

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

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

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

使用提取的列值

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

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

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

SFOTemps[0]
// Returns 65.1

SFOTemps[2]
// Returns 66.3

从表中提取行

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

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

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

使用提取的行记录

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

tempInfo = sampleData
  |> tableFind(fn: (key) =>
      key._field == "temp" and
      key.location == "sfo"
  )
  |> getRecord(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

示例辅助函数

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

提取标量字段值
// Define a helper function to extract field values
getFieldValue = (tables=<-, field) => {
  extract = tables
    |> tableFind(fn: (key) => key._field == field)
    |> getColumn(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
提取标量行数据
// Define a helper function to extract a row as a record
getRow = (tables=<-, field, idx=0) => {
  extract = tables
    |> tableFind(fn: (key) => true)
    |> getRecord(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.

示例数据

以下示例数据集表示从三个位置收集的虚构温度指标。它以 带注释的 CSV 格式化,并使用 csv.from() 函数 导入到 Flux 查询中。

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

import "csv"

sampleData = csv.from(csv: "
#datatype,string,long,dateTime:RFC3339,string,string,double
#group,false,true,false,true,true,false
#default,,,,,,
,result,table,_time,location,_field,_value
,,0,2019-11-01T12:00:00Z,sfo,temp,65.1
,,0,2019-11-01T13:00:00Z,sfo,temp,66.2
,,0,2019-11-01T14:00:00Z,sfo,temp,66.3
,,0,2019-11-01T15:00:00Z,sfo,temp,66.8
,,1,2019-11-01T12:00:00Z,kjfk,temp,69.4
,,1,2019-11-01T13:00:00Z,kjfk,temp,69.9
,,1,2019-11-01T14:00:00Z,kjfk,temp,71.0
,,1,2019-11-01T15:00:00Z,kjfk,temp,71.2
,,2,2019-11-01T12:00:00Z,kord,temp,46.4
,,2,2019-11-01T13:00:00Z,kord,temp,46.3
,,2,2019-11-01T14:00:00Z,kord,temp,42.7
,,2,2019-11-01T15:00:00Z,kord,temp,38.9
")

此页内容是否对您有帮助?

感谢您的反馈!


Flux 的未来

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

阅读更多

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

InfluxDB 3 开源版本现在可用于 alpha 测试,根据 MIT 或 Apache 2 许可获得许可。

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

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

有关如何入门的更多信息,请查看