文档文档

检查值是否存在

此页面记录了早期版本的 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 Core 和 Enterprise

快速启动。更快扩展。

获取更新

InfluxDB 3 Core 是一个开源、高速、最新数据引擎,可实时收集和处理数据,并将其持久化到本地磁盘或对象存储。InfluxDB 3 Enterprise 构建于 Core 的基础上,增加了高可用性、读取副本、增强的安全性以及数据压缩,以实现更快的查询和优化的存储。InfluxDB 3 Enterprise 的免费层级可供非商业家庭或业余爱好者使用。

有关更多信息,请查看