InfluxDB API。给定字段不为空的记录

时间:2020-10-13 14:50:30

标签: influxdb

我正在通过从Grafana获得的查询API使用InfluxDB监视。我需要检索所有记录,但记录中的度量值“ NULL”除外。

  SELECT last(usage_idle)  FROM "cpu" WHERE (host = 'myhost' AND cpu = 'cpu-total') AND time >= now() - 6h GROUP BY time(20s), "cpu" fill(null) limit 100

但是查询会不断返回带有空值的记录:

                {
                    "results": [
                        {
                            "statement_id": 0,
                            "series": [
                                {
                                    "name": "cpu",
                                    "tags": {
                                        "cpu": "cpu-total"
                                    },
                                    "columns": [
                                        "time",
                                        "last"
                                    ],
                                    "values": [
                                        [
                                            "2020-10-13T08:44:40Z",
                                            null
                                        ],
                                        [
                                            "2020-10-13T08:45:00Z",
                                            98.95878904
                                        ],
                                        [
                                            "2020-10-13T08:45:20Z",
                                            null
                                        ]
                                    ]
                                }
                            ]
                        }
                    ]
                }

在这种情况下应使用什么语法?

1 个答案:

答案 0 :(得分:1)

无论何时使用选择器功能(例如 last GROUP BY time(...)子句),influxdb都将返回 null 值在未找到值的分组时间间隔内。

在这种特殊情况下,可以通过以下方式将主查询重构为子查询来实现预期的结果:

SELECT "last" FROM ( SELECT last(usage_idle)  FROM "cpu" WHERE (host = 'myhost' AND cpu = 'cpu-total') AND time >= now() - 6h GROUP BY time(20s), "cpu" fill(null) ) limit 100

使用测试数据和 Grafana查询检查器,通过修改后的查询可获得以下输出:

q:";SELECT "last" FROM ( SELECT last(usage_idle)  FROM "cpu" WHERE (host = 'myhost' AND cpu = 'cpu-total') AND time >= now() - 6h GROUP BY time(20s), "cpu" fill(null) ) limit 100 "
epoch:"ms"
data:null
precision:"ms"
hideFromInspector:false
response:Object
results:Array[1]
0:Object
statement_id:0
series:Array[1]
0:Object
name:"cpu"
columns:Array[2]
0:"time"
1:"last"
values:Array[2]
0:Array[1602794560000,98.95878904]
1:Array[1602798140000,99.95878904]
..

..使用原始查询显示结果,如下所示:

q:"SELECT last(usage_idle)  FROM "cpu" WHERE (host = 'myhost' AND cpu = 'cpu-total') AND time >= now() - 6h GROUP BY time(20s), "cpu" fill(null) limit 100"
epoch:"ms"
data:null
precision:"ms"
hideFromInspector:false
response:Object
results:Array[1]
0:Object
statement_id:0
series:Array[1]
0:Object
name:"cpu"
tags:Object
cpu:"cpu-total"
columns:Array[2]
0:"time"
1:"last"
values:Array[100]
0:Array[1602776660000,null]
1:Array[1602776680000,null]
2:Array[1602776700000,null]
3:Array[1602776720000,null]
4:Array[1602776740000,null]
...