文档文档

使用条件逻辑查询

此页面记录了早期版本的 InfluxDB OSS。 InfluxDB OSS v2 是最新的稳定版本。 请参阅等效的 InfluxDB v2 文档: 使用条件逻辑查询

Flux 提供了 ifthenelse 条件表达式,这些表达式允许进行强大而灵活的 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
            })
        )
    )

此页面是否对您有帮助?

感谢您的反馈!


Flux 的未来

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

阅读更多

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

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

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

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

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