在 influxDB 中获取给定时间的最后存储值

Get last stored value at a given time in influxDB

我将温度传感器的值存储在 influxDB 数据库中,我正在寻找特殊请求。 当温度以一定的阈值变化时,每个传感器都会发送感测数据,这意味着所有传感器不会同时发送数据。 所以传感器 1,即 S1 将在 t1 时刻发送值 1 (S1_v1)。然后 S2 将在 t2 发送 S2_v2S3t3 发送 S3_v3,等等。 我想在给定时间 t 获得所有传感器的值,以便在 t2 时,S1 的返回值将是 S1_v1(最后存储的一个)。

请问我如何使用 influxDB 做到这一点?我希望我的要求足够明确。 非常感谢。

您可以将所有传感器数据存储到一个 measurement

然后有一个tag调用名称来存储传感器的名称。

示例:

> select * from sensors;
name: sensors
time                name value
----                ---- -----
1547100000000000000 s1   500
1547200000000000000 s2   600
1547300000000000000 s3   700
1548000000000000000 s1   900
1548000000000000000 s2   800
1548000000000000000 s3   999

要检索所有传感器在给定时间范围内的最新存储值 t,您可以执行以下操作;

SELECT * FROM sensors 
 WHERE time >= 1547000000000000000 and time <= 1547300000000000000
 GROUP BY "name" order by desc limit 1;

输出:

name: sensors
tags: name=s3
time                value
----                -----
1547300000000000000 700

name: sensors
tags: name=s2
time                value
----                -----
1547200000000000000 600

name: sensors
tags: name=s1
time                value
----                -----
1547100000000000000 500

上面的查询实质上是根据您的时间过滤器将所有传感器的数据分组到单独的桶中。然后 ORDER BY DESC 用于将它们按降序排序,以便第一行始终是时间最长的点。 Limit 1 只是要求查询引擎 return 你在前 1 行。