使用 InfluxDB (v1.1),我需要获取特定键的最后一个条目时间戳。不管它存储的是哪个测量值,也不管它是哪个值。
设置很简单,我有三个测量值:location
, network
和 usage
.
只有一把 key :device_id
.
在伪代码中,这将类似于:
# notice the lack of a FROM clause on measurement here...
SELECT MAX(time) WHERE 'device_id' = 'x';
问题:查询这个的最有效方法是什么?
我想要这个的原因是会有一个分散的同步过程。某些设备可能在过去一小时内已更新,而其他设备可能已几个月未更新。能够为设备( key )获得独特的“上次更新时间”时间戳将使我能够更有效地将新点存储到 Influx。
我也注意到在 InfluxDB's GitHub repo (#5793) 上也有类似的讨论。 ,但问题没有按任何字段/键过滤。这正是我想要的:获取特定键的“最后”条目。
最佳答案
不幸的是,不会有单一的查询可以让您找到您要找的东西。您必须在客户端做一些工作。
你想要的查询是
SELECT last(<field name>), time FROM <measurement> WHERE device_id = 'x'
您需要为每次测量运行此查询。
SELECT last(<field name>), time FROM location WHERE device_id = 'x'
SELECT last(<field name>), time FROM network WHERE device_id = 'x'
SELECT last(<field name>), time FROM usage WHERE device_id = 'x'
从那里你会得到一个带有最大时间戳的
> select last(value), time from location where device_id = 'x'; select last(value), time from network where device_id = 'x'; select last(value), time from usage where device_id = 'x';
name: location
time last
---- ----
1483640697584904775 3
name: network
time last
---- ----
1483640714335794796 4
name: usage
time last
---- ----
1483640783941353064 4
关于influxdb - 在 Influx 中查询特定键的最近时间戳(MAX/Last),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41377415/