InfluxDB Flux-过滤字段匹配值的地方

问题描述

我正在将InfluxDBGrafana一起使用,并且有一个名为items的度量,其中带有一些标签,并且有一个字段名为itemType。我需要过滤itemType是某个字符串的行。以下InfluxQL查询完全可以满足我的需求:

SELECT * FROM "items" WHERE "itemType" = 'example'

如何在Flux中做同样的事情?

我目前有以下查询,除了按字段过滤外,它执行所有操作:

from(bucket: "dbname/autogen")
    |> range(start: 2020-10-12T01:56:34Z,stop: 2020-10-12T02:54:10Z)
    |> filter(fn:(r) => r._measurement == "items")
    |> aggregateWindow(every: 5m,fn: count)

但是用filter替换filter(fn:(r) => r._measurement == "items" and r.itemType == "example")函数不会返回任何结果,即使上面的InfluxQL查询在InfluxDB CLI中使用时确实返回了数据。

解决方法

如果 itemType 是标签,则您指定的流量查询将起作用,但是,由于它是一个字段,因此使查询起作用的方法之一是通过将字段名称及其值设置为如下:

from(bucket: "dbname/autogen")
    |> range(start: 2020-10-12T01:56:34Z,stop: 2020-10-12T02:54:10Z)
    |> filter(fn:(r) => r._measurement == "items" and r._field == "itemType" and r._value == "example")
    |> aggregateWindow(every: 5m,fn: count)