检查值是否存在
此页面记录了早期版本的 InfluxDB OSS。InfluxDB OSS v2 是最新的稳定版本。请参阅等效的 InfluxDB v2 文档: 检查值是否存在。
使用 exists
运算符检查行记录是否包含列,或者列的值是否为空值。
(r) => exists r.column
如果您刚开始使用 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"
此页面是否对您有帮助?
感谢您的反馈!
支持和反馈
感谢您成为我们社区的一份子!我们欢迎并鼓励您提供关于 InfluxDB 和本文档的反馈和错误报告。要获得支持,请使用以下资源
拥有年度合同或支持合同的客户 可以联系 InfluxData 支持。