使用 InfluxDB CLI、API 和 Chronograf Data Explorer 执行 Flux 查询。
使用 filter()
函数根据字段、标签或任何其他列值查询数据。 filter()
执行的操作类似于 InfluxQL 和其他类 SQL 查询语言中的 SELECT
语句和 WHERE
子句。
from(bucket: "db/rp")
|> range(start: -1h)
|> filter(fn: (r) =>
r._measurement == "example-measurement" and
r._field == "example-field" and
r.tag == "example-tag"
)
使用 group()
函数按特定列中具有共同值的数据进行分组。
data
|> group(columns: ["host"], mode: "by")
输入
_time | host | _value |
---|
2020-01-01T00:01:00Z | host1 | 1.0 |
2020-01-01T00:01:00Z | host2 | 2.0 |
2020-01-01T00:02:00Z | host1 | 1.0 |
2020-01-01T00:02:00Z | host2 | 3.0 |
输出
_time | host | _value |
---|
2020-01-01T00:01:00Z | host1 | 1.0 |
2020-01-01T00:02:00Z | host1 | 1.0 |
_time | host | _value |
---|
2020-01-01T00:01:00Z | host2 | 2.0 |
2020-01-01T00:02:00Z | host2 | 3.0 |
使用 sort()
函数按特定列对每个表中的记录进行排序,并使用 limit()
函数将输出表中的记录数限制为固定数字 n
。
data
|> sort(columns: ["host", "_value"])
|> limit(n: 4)
输入
_time | host | _value |
---|
2020-01-01T00:01:00Z | A | 1.0 |
2020-01-01T00:02:00Z | B | 1.2 |
2020-01-01T00:03:00Z | A | 1.8 |
2020-01-01T00:04:00Z | B | 0.9 |
2020-01-01T00:05:00Z | B | 1.4 |
2020-01-01T00:06:00Z | B | 2.0 |
输出
_time | host | _value |
---|
2020-01-01T00:03:00Z | A | 1.8 |
2020-01-01T00:01:00Z | A | 1.0 |
2020-01-01T00:06:00Z | B | 2.0 |
2020-01-01T00:05:00Z | B | 1.4 |
本指南介绍了如何使用 Flux 对数据进行窗口化和聚合,并概述了它在此过程中如何塑造您的数据。
data
|> aggregateWindow(every: 20m, fn: mean)
输入
_time | _value |
---|
2020-01-01T00:00:00Z | 250 |
2020-01-01T00:04:00Z | 160 |
2020-01-01T00:12:00Z | 150 |
2020-01-01T00:19:00Z | 220 |
2020-01-01T00:32:00Z | 200 |
2020-01-01T00:51:00Z | 290 |
2020-01-01T01:00:00Z | 340 |
输出
_time | _value |
---|
2020-01-01T00:20:00Z | 195 |
2020-01-01T00:40:00Z | 200 |
2020-01-01T01:00:00Z | 290 |
2020-01-01T01:20:00Z | 340 |
使用 map()
函数重新映射列值并应用数学运算。
data
|> map(fn: (r) => ({ r with _value: r._value * r._value }))
输入
_time | _value |
---|
2020-01-01T00:01:00Z | 2 |
2020-01-01T00:02:00Z | 4 |
2020-01-01T00:03:00Z | 3 |
2020-01-01T00:04:00Z | 5 |
输出
_time | _value |
---|
2020-01-01T00:01:00Z | 4 |
2020-01-01T00:02:00Z | 16 |
2020-01-01T00:03:00Z | 9 |
2020-01-01T00:04:00Z | 25 |
使用 pivot()
或 join()
和 map()
函数将操作数值对齐到行中并计算百分比。
data
|> pivot(rowKey:["_time"], columnKey: ["_field"], valueColumn: "_value")
|> map(
fn: (r) => ({
_time: r._time,
_field: "used_percent",
_value: float(v: r.used) / float(v: r.total) * 100.0,
}),
)
输入
_time | _field | _value |
---|
2020-01-01T00:00:00Z | used | 2.5 |
2020-01-01T00:00:10Z | used | 3.1 |
2020-01-01T00:00:20Z | used | 4.2 |
_time | _field | _value |
---|
2020-01-01T00:00:00Z | total | 8.0 |
2020-01-01T00:00:10Z | total | 8.0 |
2020-01-01T00:00:20Z | total | 8.0 |
输出
_time | _field | _value |
---|
2020-01-01T00:00:00Z | used_percent | 31.25 |
2020-01-01T00:00:10Z | used_percent | 38.75 |
2020-01-01T00:00:20Z | used_percent | 52.50 |
使用 increase()
函数跟踪表中多列的增加情况。 当跟踪随时间推移或定期重置而回绕的计数器值时,此函数特别有用。
输入
_time | _value |
---|
2020-01-01T00:01:00Z | 1 |
2020-01-01T00:02:00Z | 2 |
2020-01-01T00:03:00Z | 8 |
2020-01-01T00:04:00Z | 10 |
2020-01-01T00:05:00Z | 0 |
2020-01-01T00:06:00Z | 4 |
输出
_time | _value |
---|
2020-01-01T00:02:00Z | 1 |
2020-01-01T00:03:00Z | 7 |
2020-01-01T00:04:00Z | 9 |
2020-01-01T00:05:00Z | 9 |
2020-01-01T00:06:00Z | 13 |
使用 movingAverage()
或 timedMovingAverage()
函数返回数据的移动平均值。
data
|> movingAverage(n: 3)
输入
_time | _value |
---|
2020-01-01T00:01:00Z | 1.0 |
2020-01-01T00:02:00Z | 1.2 |
2020-01-01T00:03:00Z | 1.8 |
2020-01-01T00:04:00Z | 0.9 |
2020-01-01T00:05:00Z | 1.4 |
2020-01-01T00:06:00Z | 2.0 |
输出
_time | _value |
---|
2020-01-01T00:03:00Z | 1.33 |
2020-01-01T00:04:00Z | 1.30 |
2020-01-01T00:05:00Z | 1.36 |
2020-01-01T00:06:00Z | 1.43 |
data
|> timedMovingAverage(every: 2m, period: 4m)
输入
_time | _value |
---|
2020-01-01T00:01:00Z | 1.0 |
2020-01-01T00:02:00Z | 1.2 |
2020-01-01T00:03:00Z | 1.8 |
2020-01-01T00:04:00Z | 0.9 |
2020-01-01T00:05:00Z | 1.4 |
2020-01-01T00:06:00Z | 2.0 |
输出
_time | _value |
---|
2020-01-01T00:02:00Z | 1.000 |
2020-01-01T00:04:00Z | 1.333 |
2020-01-01T00:06:00Z | 1.325 |
2020-01-01T00:06:00Z | 1.150 |
使用 derivative()
函数计算后续值之间的变化率,或使用 aggregate.rate()
函数计算每个时间窗口的平均变化率。 如果点之间的时间间隔不同,这些函数会将点标准化为公共时间间隔,从而使值易于比较。
data
|> derivative(unit: 1m, nonNegative: true)
输入
_time | _value |
---|
2020-01-01T00:00:00Z | 250 |
2020-01-01T00:04:00Z | 160 |
2020-01-01T00:12:00Z | 150 |
2020-01-01T00:19:00Z | 220 |
2020-01-01T00:32:00Z | 200 |
2020-01-01T00:51:00Z | 290 |
2020-01-01T01:00:00Z | 340 |
输出
_time | _value |
---|
2020-01-01T00:04:00Z | |
2020-01-01T00:12:00Z | |
2020-01-01T00:19:00Z | 10.0 |
2020-01-01T00:32:00Z | |
2020-01-01T00:51:00Z | 4.74 |
2020-01-01T01:00:00Z | 5.56 |
import "experimental/aggregate"
data
|> aggregate.rate(every: 20m, unit: 1m)
输入
_time | _value |
---|
2020-01-01T00:00:00Z | 250 |
2020-01-01T00:04:00Z | 160 |
2020-01-01T00:12:00Z | 150 |
2020-01-01T00:19:00Z | 220 |
2020-01-01T00:32:00Z | 200 |
2020-01-01T00:51:00Z | 290 |
2020-01-01T01:00:00Z | 340 |
输出
_time | _value |
---|
2020-01-01T00:20:00Z | |
2020-01-01T00:40:00Z | 10.0 |
2020-01-01T01:00:00Z | 4.74 |
2020-01-01T01:20:00Z | 5.56 |
使用 histogram()
函数使用 Flux 创建累积直方图。
data
|> histogram(
column: "_value",
upperBoundColumn: "le",
countColumn: "_value",
bins: [100.0, 200.0, 300.0, 400.0],
)
输入
_time | _value |
---|
2020-01-01T00:00:00Z | 250.0 |
2020-01-01T00:01:00Z | 160.0 |
2020-01-01T00:02:00Z | 150.0 |
2020-01-01T00:03:00Z | 220.0 |
2020-01-01T00:04:00Z | 200.0 |
2020-01-01T00:05:00Z | 290.0 |
2020-01-01T01:00:00Z | 340.0 |
输出
le | _value |
---|
100.0 | 0.0 |
200.0 | 3.0 |
300.0 | 6.0 |
400.0 | 7.0 |
使用 fill()
函数替换空值。
data
|> fill(usePrevious: true)
输入
_time | _value |
---|
2020-01-01T00:01:00Z | null |
2020-01-01T00:02:00Z | 0.8 |
2020-01-01T00:03:00Z | null |
2020-01-01T00:04:00Z | null |
2020-01-01T00:05:00Z | 1.4 |
输出
_time | _value |
---|
2020-01-01T00:01:00Z | null |
2020-01-01T00:02:00Z | 0.8 |
2020-01-01T00:03:00Z | 0.8 |
2020-01-01T00:04:00Z | 0.8 |
2020-01-01T00:05:00Z | 1.4 |
输入
_time | _value |
---|
2020-01-01T00:01:00Z | null |
2020-01-01T00:02:00Z | 0.8 |
2020-01-01T00:03:00Z | null |
2020-01-01T00:04:00Z | null |
2020-01-01T00:05:00Z | 1.4 |
输出
_time | _value |
---|
2020-01-01T00:01:00Z | 0.0 |
2020-01-01T00:02:00Z | 0.8 |
2020-01-01T00:03:00Z | 0.0 |
2020-01-01T00:04:00Z | 0.0 |
2020-01-01T00:05:00Z | 1.4 |
使用 median()
函数返回表示输入数据的 0.5
分位数(第 50 个百分位数)或中位数的值。
输入
_time | _value |
---|
2020-01-01T00:01:00Z | 1.0 |
2020-01-01T00:02:00Z | 1.0 |
2020-01-01T00:03:00Z | 2.0 |
2020-01-01T00:04:00Z | 3.0 |
使用 quantile()
函数返回输入数据的 q
分位数或百分位数内的所有值。
data
|> quantile(q: 0.99, method: "estimate_tdigest")
输入
_time | _value |
---|
2020-01-01T00:01:00Z | 1.0 |
2020-01-01T00:02:00Z | 1.0 |
2020-01-01T00:03:00Z | 2.0 |
2020-01-01T00:04:00Z | 3.0 |
本指南介绍了如何使用 Flux 连接数据,并概述了它在此过程中如何塑造您的数据。
t1 = from(bucket: "example-bucket")
|> range(start: 2020-01-01T00:00:00Z)
|> filter(fn: (r) => r.m == "foo")
t2 = from(bucket: "example-bucket")
|> range(start: 2020-01-01T00:00:00Z)
|> filter(fn: (r) => r.m == "bar")
join(tables: {t1: t1, t2: t2}, on: ["_time"])
输入
t1
_time | _value |
---|
2020-01-01T00:01:00Z | 1 |
2020-01-01T00:02:00Z | 2 |
2020-01-01T00:03:00Z | 1 |
2020-01-01T00:04:00Z | 3 |
t2
_time | _value |
---|
2020-01-01T00:01:00Z | 5 |
2020-01-01T00:02:00Z | 2 |
2020-01-01T00:03:00Z | 3 |
2020-01-01T00:04:00Z | 4 |
输出
_time | _value_t1 | _value_t2 |
---|
2020-01-01T00:01:00Z | 1 | 5 |
2020-01-01T00:02:00Z | 2 | 2 |
2020-01-01T00:03:00Z | 1 | 3 |
2020-01-01T00:04:00Z | 3 | 4 |
使用 cumulativeSum()
函数计算值的运行总计。
输入
_time | _value |
---|
2020-01-01T00:01:00Z | 1 |
2020-01-01T00:02:00Z | 2 |
2020-01-01T00:03:00Z | 1 |
2020-01-01T00:04:00Z | 3 |
输出
_time | _value |
---|
2020-01-01T00:01:00Z | 1 |
2020-01-01T00:02:00Z | 3 |
2020-01-01T00:03:00Z | 4 |
2020-01-01T00:04:00Z | 7 |
使用 first()
或 last()
函数返回输入表中的第一个或最后一个点。
输入
_time | _value |
---|
2020-01-01T00:01:00Z | 1.0 |
2020-01-01T00:02:00Z | 1.0 |
2020-01-01T00:03:00Z | 2.0 |
2020-01-01T00:04:00Z | 3.0 |
输出
_time | _value |
---|
2020-01-01T00:01:00Z | 1.0 |
输入
_time | _value |
---|
2020-01-01T00:01:00Z | 1.0 |
2020-01-01T00:02:00Z | 1.0 |
2020-01-01T00:03:00Z | 2.0 |
2020-01-01T00:04:00Z | 3.0 |
输出
_time | _value |
---|
2020-01-01T00:04:00Z | 3.0 |
使用 exists
运算符检查行记录是否包含列或列的值是否为空。
过滤空值
data
|> filter(fn: (r) => exists r._value)
使用 Flux 流和表函数从 Flux 查询输出中提取标量值。 例如,这使您可以使用查询结果动态设置变量。
scalarValue = {
_record =
data
|> tableFind(fn: key => true)
|> getRecord(idx: 0)
return _record._value
}
使用 Flux 处理和操作时间戳。
Flux 提供了多个函数来帮助监控数据中的状态和状态变化。
Flux sql
包提供了用于处理 SQL 数据源的函数。 使用 sql.from()
查询 PostgreSQL 和 MySQL 等 SQL 数据库
import "sql"
sql.from(
driverName: "postgres",
dataSourceName: "postgresql://user:password@localhost",
query: "SELECT * FROM example_table"
)
本指南介绍了如何使用 Flux 条件表达式(例如 if
、else
和 then
)来查询和转换数据。 Flux 从左到右评估语句,并在条件匹配后停止评估。
if color == "green" then "008000" else "ffffff"
本指南介绍了如何在 Flux 函数的评估逻辑中使用正则表达式。
data
|> filter(fn: (r) => r.tag =~ /^foo[1-3]/)
输入
_time | tag | _value |
---|
2020-01-01T00:01:00Z | foo1 | 1.0 |
2020-01-01T00:02:00Z | foo5 | 1.2 |
2020-01-01T00:03:00Z | bar3 | 1.8 |
2020-01-01T00:04:00Z | foo3 | 0.9 |
2020-01-01T00:05:00Z | foo2 | 1.4 |
2020-01-01T00:06:00Z | bar1 | 2.0 |
输出
_time | tag | _value |
---|
2020-01-01T00:01:00Z | foo1 | 1.0 |
2020-01-01T00:04:00Z | foo3 | 0.9 |
2020-01-01T00:05:00Z | foo2 | 1.4 |
使用 Flux Geo 包过滤地理时序数据,并按地理位置或轨迹分组。
import "experimental/geo"
sampleGeoData
|> geo.filterRows(region: {lat: 30.04, lon: 31.23, radius: 200.0})
|> geo.groupByArea(newColumn: "geoArea", level: 5)
优化您的 Flux 查询,以减少其内存和计算 (CPU) 要求。
本指南介绍了如何在 Chronograf 仪表板单元格中使用 Flux 查询,哪些模板变量可用以及如何使用它们。