【问题标题】:How to select the closest data to the given time for each group如何为每个组选择最接近给定时间的数据
【发布时间】:2018-08-17 14:26:14
【问题描述】:

我正在使用 InfluxDB 1.4,这是我的任务

1) find the closet value for each IDs. 
2) Do 1) for every hour

例如,

select id, value, time from myTable where time = '2018-08-14T00:00:00Z' group by id;
select id, value, time from myTable where time = '2018-08-14T01:00:00Z' group by id;
....
select id, value, time from myTable where time = '2018-08-14T23:00:00Z' group by id;

然后,一些 id 在每个点钟都有价值,而另一些则没有。在这种情况下,我想获得最接近给定时间“2018-08-14T14:00:00Z”的行,例如“2018-08-14T14:00:01Z”或“2018-08-14T13:59”: 59Z'

我不想每小时查询 24 次。我可以使用 group by time、id 或其他东西来完成这项任务吗?

【问题讨论】:

  • 您介意解释您的问题吗? closest 值是什么意思?最接近?
  • 我编辑了我的问题。我想要的是获得最接近给定时间的行。之前或之后。比如“2018-08-14T14:00:01Z”或“2018-08-14T13:59:59Z”

标签: go influxdb


【解决方案1】:

问:我想select最接近每小时边界的点数据。有没有一种方法可以做到这一点,而不必每天查询 24 次? group by time 对此有何帮助?

答:

group by time 会对此有所帮助吗?

不幸的是,group by time 函数对您没有多大帮助,因为它要求查询具有聚合函数。 group by time 函数的作用是通过使用 aggregation 函数(如 summean 等)将组合行的值制成表格,将区间内的所有数据分组到一条记录中。

有没有一种方法可以做到这一点,而不必为每个查询 24 次 天?

据我所知,我认为influxdb 1.5 没有任何方法可以为此任务构建单行查询。也许1.6 中有什么东西,我不确定。没试过。

目前我认为您目前最好的解决方案是构建一个使用 time filterorder bylimit 函数的查询,例如

select * from uv where time >= '2018-08-18T14:00:00Z' and time < '2018-08-18T15:00:00Z' order by desc limit 1;

上面的查询表示你是selecting下午2点到3点之间的所有点然后按降序排列但只返回第一行,这就是你想要的。

如果由于某种原因您只能向 influxdb 发出 1 个 HTTP 请求以获取特定日期的每小时数据。您可以使用 ; 分隔符将 24 个查询捆绑到一个大查询中,并在 1 个事务中检索数据。例如

select * from uv where time >= '2018-08-18T14:00:00Z' and time < '2018-08-18T15:00:00Z' order by desc limit 1; select * from uv where time >= '2018-08-18T15:00:00Z' and time < '2018-08-18T16:00:00Z' order by desc limit 1; select * from uv where time >= '2018-08-18T16:00:00Z' and time < '2018-08-18T17:00:00Z' order by desc limit 1;

输出:

name: uv
time                tag1 id         value
----                -------- --         -----
1534603500000000000 apple  uv 2
1534607100000000000 apple  uv 1
1534610700000000000 apple  uv 3.1

【讨论】:

  • 感谢您的友好回答。其实我也是这样做的。但由于我花了太长时间,我想知道更好的方法。 :)
  • @JihuiChoi 不用担心。你探索过Kapacitor吗?因为您可以使用它对数据进行预处理并将其写入另一个测量值,这样您的应用程序就可以简单地select 数据,而无需经历解释的麻烦。
  • 不。在公司里,我们只用自己的工具使用influxdb。但我想我应该尝试一下是否可以将它与我们的数据一起使用。谢谢。
猜你喜欢
  • 2012-02-03
  • 2021-02-25
  • 1970-01-01
  • 2022-11-26
  • 2019-04-16
  • 1970-01-01
  • 2017-10-01
  • 1970-01-01
  • 2019-11-12
相关资源
最近更新 更多