如何在Influxdb中以动态间隔存储和对齐系列数据

时间:2017-06-16 19:01:30

标签: time-series influxdb

我从一些设备收集了动态间隔耗水量的系列数据。 每个系列中的数据可以以不同的时间间隔记录。我想查询和对齐固定间隔的系列,如5米,10米等。

示例原始数据:

{ device: 1, from: 2017/01/01 00:00, to: 2017/01/01 00:13, consumption: 13 },
{ device: 1, from: 2017/01/01 00:13, to: 2017/01/01 00:20, consumption: 12 },
{ device: 1, from: 2017/01/01 00:20, to: 2017/01/01 00:28, consumption: 20 },
                              .
                              .
                              .
{ device: 2, from: 2017/01/01 00:00, to: 2017/01/01 00:06, consumption: 13 }, <--
{ device: 2, from: 2017/01/01 00:09, to: 2017/01/01 00:15, consumption: 12 }, <--Time gap between previous data may exist and can treat as zero consumption in this gap
{ device: 2, from: 2017/01/01 00:15, to: 2017/01/01 00:25, consumption: 20 },
                              .
                              .
                              .

您可以假设特定时间间隔内的消费率相同,即此时间间隔内每分钟{ device: 1, from: 2017/01/01 00:00, to: 2017/01/01 00:13, consumption: 13 }的每分钟消耗量为1。

如果固定间隔= 5m,预期结果:

Device 1
time              consumption
----              -----------
2017-01-01T00:00Z     5
2017-01-01T05:00Z     5
2017-01-01T10:00Z     6.428571
2017-01-01T15:00Z     8.571429
2017-01-01T20:00Z     12.5
               .
               .
               .
Device 2
time              consumption
----              -----------
2017-01-01T00:00Z     10.833333
2017-01-01T05:00Z     4.166667
2017-01-01T10:00Z     10
2017-01-01T15:00Z     10
2017-01-01T20:00Z     10
               .
               .
               .

任何人都可以帮我解决这个问题吗?

1 个答案:

答案 0 :(得分:0)

按需要的时间间隔进行分组,在适当的地方汇总结果和填充值。

例如,以5分钟的间隔持续一小时的平均消费,填补linear interpolation of missing values的空白。

SELECT mean("consumption") AS "consumption" 
FROM "Device 1", "Device 2"
WHERE time > now() - 1h
GROUP BY time(5m)
FILL(linear)

假设测量名称为“设备1”和“设备2”,请根据需要进行替换。