TS.CREATERULE
TS.CREATERULE sourceKey destKey AGGREGATION aggregator bucketDuration [alignTimestamp]
- 可用:
- Redis 堆栈 / TimeSeries 1.0.0
- 时间复杂度:
- O(1)
创建压缩规则
必需参数
sourceKey
是源时间序列的键名称。
destKey
是目标 (压缩) 时间序列的键名称。它必须在TS.CREATERULE被调用。
AGGREGATION aggregator bucketDuration
将结果聚合到时间桶中。
-
aggregator采用以下聚合类型之一:aggregator描述 avg所有值的算术平均值 sum所有值的总和 min最小值 max最大值 range最高值和最低值之间的差值 count值的数量 first存储桶中时间戳最低的值 last存储桶中时间戳最高的值 std.p值的总体标准差 std.s值的样本标准差 var.p值的总体方差 var.s值的样本方差 twa存储桶时间范围内的时间加权平均值(自 RedisTimeSeries v1.8 起) -
bucketDuration是每个存储桶的持续时间(以毫秒为单位)。
- 只会聚合在创建规则后添加到源系列中的新样本。
- 叫
TS.CREATERULE替换为非空destKey可能会导致原始数据与压缩数据不一致。 - 将样本显式添加到压缩的时间序列中(使用
TS.ADD,TS.MADD,TS.INCRBY或TS.DECRBY) 可能会导致原始数据与压缩数据不一致。压缩过程可能会覆盖此类样本。 - 如果在存储桶期间没有向源时间序列添加样本。不会将压缩样本添加到目标时间序列中。
- 添加到目标时间序列的压缩样本的时间戳设置为相应压缩存储桶的开始时间戳。例如,对于未对齐的 10 分钟压缩存储桶,压缩的示例时间戳为
x:00,x:10,x:20等。 - 删除
destKey也会导致 Compaction 规则被删除。
可选参数
alignTimestamp(自 RedisTimeSeries v1.8 起)
确保有一个 Bucket 正好从alignTimestamp并相应地对齐所有其他存储桶。它以毫秒为单位。默认值为 0:与 Unix 纪元对齐。
例如,如果bucketDuration是 24 小时 (24 * 3600 * 1000)、设置alignTimestamp到 Unix 纪元后 6 小时 (6 * 3600 * 1000) 确保每个存储桶的时间范围为[06:00 .. 06:00).
返回值
返回以下回复之一:
- 简单的字符串回复 -
OK如果执行正确 - [] 出错(参数无效、键类型错误等),当
sourceKey不存在,则destKey不存在,则sourceKey已经是压缩规则的目标,则当destKey已经是压缩规则的源或目标,或者当sourceKey和destKey相同
例子
创建压缩规则
创建时间序列以存储在特拉维夫测得的温度。
127.0.0.1:6379> TS.CREATE temp:TLV LABELS type temp location TLV
OK
Next, create a compacted time series named dailyAvgTemp containing one compacted sample per 24 hours: the time-weighted average of all measurements taken from midnight to next midnight.
127.0.0.1:6379> TS.CREATE dailyAvgTemp:TLV LABELS type temp location TLV
127.0.0.1:6379> TS.CREATERULE temp:TLV dailyAvgTemp:TLV AGGREGATION twa 86400000
Now, also create a compacted time series named dailyDiffTemp. This time series will contain one compacted sample per 24 hours: the difference between the minimum and the maximum temperature measured between 06:00 and 06:00 next day.
Here, 86400000 is the number of milliseconds in 24 hours, 21600000 is the number of milliseconds in 6 hours.
127.0.0.1:6379> TS.CREATE dailyDiffTemp:TLV LABELS type temp location TLV
127.0.0.1:6379> TS.CREATERULE temp:TLV dailyDiffTemp:TLV AGGREGATION range 86400000 21600000
See also
Related topics