Redis - 寻求数据建模建议
我使用 Redis 来存储来自许多模拟传感器的数据日志。我的目标是根据日志时间戳对数据进行排序,并从特定日期时间范围中提取数据。我最初的数据模型是使用传感器名称作为键,并为每个时间戳和附加到散列键的值提供一个散列。
所以。如果我有 SensorA、SensorB 和 SensorC,执行 Keys * 将返回 1. SensorA、2. SensorB 和 3. SensorC。执行 hget SensorB 20110111172900 将返回,比方说 25。
当前建模的问题是它不允许对时间戳进行排序,或者我认为因为我所做的所有尝试都失败了。
有人能够建议一个允许对数据范围进行排序和提取的数据模型,或者建议在上面的数据模型中允许这样做的正确排序参数。
I'm using Redis to store data logs from many analog sensors. My goal is to sort the data according to a log time stamp and extract the data from a specific datetime range. My originial data model was to use the sensor name as the key and have a hash for each timestamp and the value attached to the hashkey.
So. if I have SensorA, SensorB and SensorC, doing a Keys * would return 1. SensorA, 2. SensorB and 3. SensorC. Doing hget SensorB 20110111172900 would return, let's say 25.
The problem with the current modeling is that it doesn't allow sorting on the timestamp, or so I think since all I've tried has failed.
Would someone be able to suggest a data model that would allow sorting and extracting ranges of data, or suggest the proper sort arguments that would allow this in the data model above.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在这种情况下,排序集可能比散列更合适。
该值将是时间戳和传感器值的组合。分数将是时间戳。使用 ZRANGEBYSCORE 检索值。读取和写入都从 O(1) 变为 O(Log(N)),但您可以获得返回一系列值的能力。
您还可以使用列表来获得 O(1) 插入。读取特定条目的时间复杂度为 O(N),但获取最新条目的时间复杂度为 O(1)。
A sorted set is probably a better fit than a hash in this case.
The value would be a combination of timestamp and sensor value. The score would be the timestamp. Use ZRANGEBYSCORE to retrieve the values. Both read and write go from O(1) to O(Log(N)), but you gain the ability to return a range of values.
You could also use a list to get O(1) insertion. Reading would be O(N) for retrieving a specific entry, but getting the most recent entries would be O(1).