文档文档

检查值是否存在

此页面记录了早期版本的 InfluxDB OSS。InfluxDB OSS v2 是最新的稳定版本。请参阅等效的 InfluxDB v2 文档: 检查值是否存在

使用 exists 运算符检查行记录是否包含列,或者列的值是否为空值

(r) => exists r.column

如果您刚开始使用 Flux 查询,请查看以下内容

  • Flux 入门,了解 Flux 的概念概述和 Flux 查询的组成部分。
  • 执行查询,了解运行查询的各种方法。

exists 与行函数( filter()map()reduce())结合使用,以检查行是否包含列,或者该列的值是否为空值

过滤空值

from(bucket: "example-bucket")
    |> range(start: -5m)
    |> filter(fn: (r) => exists r._value)

根据存在性映射值

from(bucket: "default")
    |> range(start: -30s)
    |> map(
        fn: (r) => ({r with
            human_readable: if exists r._value then
                "${r._field} is ${string(v: r._value)}."
            else
                "${r._field} has no value.",
        }),
    )

在自定义聚合函数中忽略空值

customSumProduct = (tables=<-) => tables
    |> reduce(
        identity: {sum: 0.0, product: 1.0},
        fn: (r, accumulator) => ({r with
            sum: if exists r._value then
                r._value + accumulator.sum
            else
                accumulator.sum,
            product: if exists r._value then
                r.value * accumulator.product
            else
                accumulator.product,
        }),
    )

检查静态定义的记录是否包含键

当您使用 记录字面量语法 静态定义记录时,Flux 知道记录类型以及要预期的键。

  • 如果键存在于静态记录中,则 exists 返回 true
  • 如果键在静态记录中不存在,因为记录类型是静态已知的,则 exists 返回错误。
import "internal/debug"

p = {
    firstName: "John",
    lastName: "Doe",
    age: 42,
}

exists p.firstName
// Returns true

exists p.height
// Returns "error: record is missing label height"

此页面是否对您有帮助?

感谢您的反馈!


Flux 的未来

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

阅读更多

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

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

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

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

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