使用条件逻辑查询
此页面记录了早期版本的 InfluxDB OSS。 InfluxDB OSS v2 是最新的稳定版本。 请参阅等效的 InfluxDB v2 文档: 使用条件逻辑查询。
Flux 提供了 if
、then
和 else
条件表达式,这些表达式允许进行强大而灵活的 Flux 查询。
条件表达式语法
// Pattern
if <condition> then <action> else <alternative-action>
// Example
if color == "green" then "008000" else "ffffff"
条件表达式在以下上下文中最为有用
评估条件表达式
Flux 按顺序评估语句,并在条件匹配时停止评估。
例如,给定以下语句
if r._value > 95.0000001 and r._value <= 100.0 then "critical"
else if r._value > 85.0000001 and r._value <= 95.0 then "warning"
else if r._value > 70.0000001 and r._value <= 85.0 then "high"
else "normal"
当 r._value
为 96 时,输出为 “critical”,其余条件将不再评估。
示例
有条件地设置变量的值
以下示例根据 dueDate
变量与 now()
的关系设置 overdue
变量。
dueDate = 2019-05-01
overdue = if dueDate < now() then true else false
创建条件过滤器
以下示例使用示例 metric
变量来更改查询过滤数据的方式。metric
有三个可能的值
- Memory
- CPU
- Disk
metric = "Memory"
from(bucket: "telegraf/autogen")
|> range(start: -1h)
|> filter(fn: (r) =>
if v.metric == "Memory"
then r._measurement == "mem" and r._field == "used_percent"
else if v.metric == "CPU"
then r._measurement == "cpu" and r._field == "usage_user"
else if v.metric == "Disk"
then r._measurement == "disk" and r._field == "used_percent"
else r._measurement != ""
)
使用 map() 有条件地转换列值
以下示例使用 map()
函数 有条件地转换列值。它根据 _value
列将 level
列设置为特定的字符串。
from(bucket: "telegraf/autogen")
|> range(start: -5m)
|> filter(fn: (r) => r._measurement == "mem" and r._field == "used_percent" )
|> map(fn: (r) => ({
r with
level:
if r._value >= 95.0000001 and r._value <= 100.0 then "critical"
else if r._value >= 85.0000001 and r._value <= 95.0 then "warning"
else if r._value >= 70.0000001 and r._value <= 85.0 then "high"
else "normal"
})
)
from(bucket: "telegraf/autogen")
|> range(start: -5m)
|> filter(fn: (r) => r._measurement == "mem" and r._field == "used_percent" )
|> map(fn: (r) => ({
// Retain all existing columns in the mapped row
r with
// Set the level column value based on the _value column
level:
if r._value >= 95.0000001 and r._value <= 100.0 then "critical"
else if r._value >= 85.0000001 and r._value <= 95.0 then "warning"
else if r._value >= 70.0000001 and r._value <= 85.0 then "high"
else "normal"
})
)
使用 reduce() 有条件地递增计数
以下示例使用 aggregateWindow()
和 reduce()
函数来计算每五分钟窗口中超过定义阈值的记录数。
threshold = 65.0
from(bucket: "telegraf/autogen")
|> range(start: -1h)
|> filter(fn: (r) => r._measurement == "mem" and r._field == "used_percent" )
|> aggregateWindow(
every: 5m,
fn: (column, tables=<-) => tables |> reduce(
identity: {above_threshold_count: 0.0},
fn: (r, accumulator) => ({
above_threshold_count:
if r._value >= threshold then accumulator.above_threshold_count + 1.0
else accumulator.above_threshold_count + 0.0
})
)
)
threshold = 65.0
from(bucket: "telegraf/autogen")
|> range(start: -1h)
|> filter(fn: (r) => r._measurement == "mem" and r._field == "used_percent" )
// Aggregate data into 5 minute windows using a custom reduce() function
|> aggregateWindow(
every: 5m,
// Use a custom function in the fn parameter.
// The aggregateWindow fn parameter requires 'column' and 'tables' parameters.
fn: (column, tables=<-) => tables |> reduce(
identity: {above_threshold_count: 0.0},
fn: (r, accumulator) => ({
// Conditionally increment above_threshold_count if
// r.value exceeds the threshold
above_threshold_count:
if r._value >= threshold then accumulator.above_threshold_count + 1.0
else accumulator.above_threshold_count + 0.0
})
)
)
此页面是否对您有帮助?
感谢您的反馈!
支持和反馈
感谢您成为我们社区的一份子!我们欢迎并鼓励您提供关于 InfluxDB 和本文档的反馈和错误报告。要获得支持,请使用以下资源
拥有年度或支持合同的客户 可以 联系 InfluxData 支持。