TS.MADD

将新样本附加到一个或多个时间序列

语法

TS.MADD key timestamp value [key timestamp value ...]

引入版本:1.0.0

复杂度摘要:O(N*M)

[!NOTE] 此命令的行为在集群 Redis 环境中有所不同。请参阅 多键操作 页面了解更多信息。

将新样本附加到一个或多个时间序列

示例

必需参数

key

是时间序列的键名称。

timestamp

是 Unix 时间(整数,以毫秒为单位),指定样本时间戳或 * 将样本时间戳设置为服务器时钟的 Unix 时间。

Unix 时间是自 1970 年 1 月 1 日(Unix 纪元)UTC 00:00:00 起经过的毫秒数,没有因闰秒而进行调整。

value

是样本的数值数据值(双精度)。双数应遵循RFC 7159(JSON标准)。解析器会拒绝不适合 binary64 的过大值。它不接受无限值。从 Redis 8.6 开始支持 NaN(非数字)值。

Notes: - If timestamp is older than the retention period compared to the maximum existing timestamp, the sample is discarded and an error is returned. - Explicitly adding samples to a compacted time series (using TS.ADD, TS.MADD, TS.INCRBY, or TS.DECRBY) may result in inconsistencies between the raw and the compacted data. The compaction process may override such samples. - ignoreMaxTimeDiff and ignoreMaxValDiff cannot be specified as is the case with TS.ADD. However, the same logic still applies based on the values of the per-key configuration parameters. See the TS.ADD command page for more information.

复杂性

如果时间序列上存在压缩规则,TS.MADD 性能可能会降低。 TS.MADD 的复杂度始终为 O(N*M),其中 N 是更新的系列数量,M 是压缩规则的数量或没有压缩的 O(N)。

示例

Add stock prices at different timestamps

创建两只股票并添加它们在三个不同时间戳的价格。

127.0.0.1:6379> TS.CREATE 库存:A LABELS 类型库存名称 A OK 127.0.0.1:6379> TS.CREATE 库存:B LABELS 类型库存名称 B OK 127.0.0.1:6379> TS.MADD 库存:A 1000 100 库存:A 1010 110库存:A 1020 120 库存:B 1000 120 库存:B 1010 110 库存:B 1020 100 1) (整数) 1000 2) (整数) 1010 3) (整数) 1020 4) (整数) 1000 5) (整数) 1010 6) (整数)1020

Redis Software 与 Redis Cloud 兼容性

Redis
软件
Redis
Cloud
说明
✅ 支持
✅ 灵活且每年一次
✅ 免费和固定

返回值

RESP2

以下之一: * 数组回复,其中每个元素都是代表更新插入样本的时间戳的 整数回复。对于每个被忽略的元素(参见 TS.CREATE 中的 IGNORE),回复元素值将是时间序列中最大的时间戳。 * 简单错误回复 在这些情况下:无效参数、错误的键类型、重复策略为 BLOCK,或者当 timestamp 比最大现有时间戳早于保留期时。

RESP3

以下之一: * 数组回复,其中每个元素都是代表更新插入样本的时间戳的 整数回复。对于每个被忽略的元素(参见 TS.CREATE 中的 IGNORE),回复元素值将是时间序列中最大的时间戳。 * 简单错误回复 在这些情况下:无效参数、错误的键类型、重复策略为 BLOCK,或者当 timestamp 比最大现有时间戳早于保留期时。

参见

TS.MRANGE | TS.RANGE | TS.MREVRANGE | TS.REVRANGE

Redis时间系列

资料来源:Redis 官方 TS.MADD 文档;中文内容参考 Redis 文档许可。