# Timeseries命令

# TS.ADD

# 语法

TS.ADD key timestamp value 
  [RETENTION retentionPeriod] 
  [ENCODING <COMPRESSED|UNCOMPRESSED>] 
  [CHUNK_SIZE size] 
  [DUPLICATE_POLICY policy] 
  [ON_DUPLICATE policy_ovr] 
  [IGNORE ignoreMaxTimediff ignoreMaxValDiff] 
  [LABELS [label value ...]]

# 描述

向时间序列追加一个样本

# 参数

  • key

键名

  • timestamp

该参数为 Unix 时间戳(整数,单位为毫秒),用于指定样本的时间戳;或者输入 *,表示将样本的时间戳设置为当前服务器时钟的 Unix 时间

  • value

该字段为样本的数值数据。该双精度数字需符合 RFC 7159(JSON 标准)。如果数值过大,超出了 binary64的表示范围,解析器将会拒绝该输入

  • RETENTION retentionPeriod

该参数表示最大保留期限,其计算基准为现有的最大时间戳,单位为毫秒。仅在创建新的时间序列时使用此参数;如果向现有的时间序列中添加样本,该参数将被忽略。详情请参阅 TS.CREATE 命令中的 RETENTION 选项

  • ENCODING enc

该参数)用于指定时间序列样本的编码格式。此参数仅在创建新的时间序列时有效;如果向现有的时间序列中添加样本,该参数将被忽略。有关详细信息,请参阅 TS.CREATE 命令中的 ENCODING 选项

  • CHUNK_SIZE size

为每个数据块分配的内存大小,单位是字节。仅在创建新时间序列时使用此参数;如果是向现有时间序列追加样本,该参数会被忽略。详情请参考 TS.CREATE 命令中的 CHUNK_SIZE 参数

# 示例

> TS.ADD temperature:3:11 1548149183000 27 RETENTION 31536000000
(integer) 1548149183000

> TS.ADD temperature:3:11 * 30
(integer) 1662042954573

# TS.ALTER

# 语法

TS.ALTER key 
  [RETENTION retentionPeriod] 
  [CHUNK_SIZE size] 
  [DUPLICATE_POLICY policy] 
  [LABELS [label value ...]]

# 描述

更新现有时间序列的保留策略、数据块大小、重复数据处理策略以及标签

# 参数

  • key

键名

  • RETENTION retentionPeriod

该参数表示最大保留期限,其计算基准为现有的最大时间戳,单位为毫秒。仅在创建新的时间序列时使用此参数;如果向现有的时间序列中添加样本,该参数将被忽略。详情请参阅 TS.CREATE 命令中的 RETENTION 选项

  • CHUNK_SIZE size

为每个数据块分配的内存大小,单位是字节。仅在创建新时间序列时使用此参数;如果是向现有时间序列追加样本,该参数会被忽略。详情请参考 TS.CREATE 命令中的 CHUNK_SIZE 参数

  • LABELS

这是一组键值对,用来表示该键的元数据标签,同时也能作为二级索引。如果指定了 LABELS 参数,就会应用你传入的标签列表;而那些不在新列表里的旧标签,会被自动移除。如果只写了 LABELS 但没传任何键值对,就会清空该键上现有的所有标签

# 示例

> TS.CREATE temperature:2:32 RETENTION 60000 DUPLICATE_POLICY MAX LABELS sensor_id 2 area_id 32
OK

> TS.ALTER temperature:2:32 LABELS sensor_id 2 area_id 32 sub_area_id 15
OK

# TS.CREATE

# 语法

TS.CREATE key 
  [RETENTION retentionPeriod] 
  [ENCODING <COMPRESSED|UNCOMPRESSED>] 
  [CHUNK_SIZE size] 
  [DUPLICATE_POLICY policy] 
  [IGNORE ignoreMaxTimediff ignoreMaxValDiff] 
  [LABELS [label value ...]]

# 描述

创建一个新的时间序列

# 参数

  • key

键名

  • RETENTION retentionPeriod

该参数表示最大保留期限,其计算基准为现有的最大时间戳,单位为毫秒。仅在创建新的时间序列时使用此参数;如果向现有的时间序列中添加样本,该参数将被忽略。详情请参阅 TS.CREATE 命令中的 RETENTION 选项

  • ENCODING enc

该参数)用于指定时间序列样本的编码格式。此参数仅在创建新的时间序列时有效;如果向现有的时间序列中添加样本,该参数将被忽略。有关详细信息,请参阅 TS.CREATE 命令中的 ENCODING 选项

  • CHUNK_SIZE size

为每个数据块分配的内存大小,单位是字节。仅在创建新时间序列时使用此参数;如果是向现有时间序列追加样本,该参数会被忽略。详情请参考 TS.CREATE 命令中的 CHUNK_SIZE 参数

  • LABELS

这是一组键值对,用来表示该键的元数据标签,同时也能作为二级索引。如果指定了 LABELS 参数,就会应用你传入的标签列表;而那些不在新列表里的旧标签,会被自动移除。如果只写了 LABELS 但没传任何键值对,就会清空该键上现有的所有标签

# 示例

> TS.CREATE temperature:2:32 RETENTION 60000 DUPLICATE_POLICY MAX LABELS sensor_id 2 area_id 32
OK

# TS.CREATERULE

# 语法

TS.CREATERULE sourceKey destKey 
  AGGREGATION aggregator bucketDuration 
  [alignTimestamp]

# 描述

创建合并规则

# 参数

  • sourceKey

是源时间序列的键名

  • destKey

是目标时间序列的键名。在调用 TS.CREATERULE 之前,必须先创建该目标序列

  • AGGREGATION aggregator bucketDuration

将结果按时间段进行聚合

# 示例

> TS.CREATE temp:{TLV} LABELS type temp location TLV
OK

> TS.CREATE dailyAvgTemp:{TLV} LABELS type temp location TLV
> TS.CREATERULE temp:{TLV} dailyAvgTemp:{TLV} AGGREGATION twa 86400000

> TS.CREATE dailyDiffTemp:{TLV} LABELS type temp location TLV
> TS.CREATERULE temp:{TLV} dailyDiffTemp:{TLV} AGGREGATION range 86400000 21600000

# TS.DECRBY

# 语法

TS.DECRBY key subtrahend 
  [TIMESTAMP timestamp] 
  [RETENTION retentionPeriod] 
  [ENCODING <COMPRESSED|UNCOMPRESSED>] 
  [CHUNK_SIZE size] 
  [DUPLICATE_POLICY policy] 
  [IGNORE ignoreMaxTimediff ignoreMaxValDiff]  
  [LABELS [label value ...]]

# 描述

减小具有最大现有时间戳的样本的值,或者以给定的减量创建一个新的样本,其值等于该最大时间戳样本的值减去该减量

# 参数

  • key

键名

  • subtrahend

表示减数。如果减数为 NaN,则返回错误

  • TIMESTAMP

为 Unix 时间戳,用于指定样本的时间戳;或者使用 * 将样本的时间戳设置为服务器时钟的当前 Unix 时间


# TS.DEL

# 语法

TS.DEL key from_timestamp to_timestamp

# 描述

删除指定时间序列中两个时间戳之间的所有样本

# 参数

  • fromTIMESTAMP

指定范围删除的起始时间戳

  • toTIMESTAMP

指定范围删除的结束时间戳

  • key

键名

# 示例

> TS.CREATE temp:TLV LABELS type temp location TLV
OK
> TS.CREATE temp:JLM LABELS type temp location JLM
OK
> TS.MADD temp:TLV 1000 30 temp:TLV 1010 35 temp:TLV 1020 9999 temp:TLV 1030 40
1) (integer) 1000
2) (integer) 1010
3) (integer) 1020
4) (integer) 1030
> TS.MADD temp:JLM 1005 30 temp:JLM 1015 35 temp:JLM 1025 9999 temp:JLM 1035 40
1) (integer) 1005
2) (integer) 1015
3) (integer) 1025
4) (integer) 1035

> TS.DEL temp:TLV 1000 1030
(integer) 4

# TS.DELETERULE

# 语法

TS.DELETERULE sourceKey destKey

# 描述

删除合并规则

# 参数

  • sourceKey

是源时间序列的键名

  • destKey

是目标时间序列的键名。在调用 TS.CREATERULE 之前,必须先创建该目标序列


# TS.GET

# 语法

TS.GET key [LATEST]

# 描述

获取指定时间序列中时间戳最大的样本

# 参数

  • key

键名

  • LATEST

当时间序列是压缩序列时,会用到这个参数

# 示例

> TS.CREATE temp:TLV LABELS type temp location TLV
OK
> TS.MADD temp:TLV 1672534800 12 temp:TLV 1672556400 16 temp:TLV 1672578000 21 temp:TLV 1672599600 14

> TS.GET temp:TLV
1) (integer) 1672599600
2) 14

> TS.CREATE temp:JLM LABELS type temp location JLM
OK

> TS.CREATE dailyMaxTemp:JLM LABELS type temp location JLM
OK
> TS.CREATERULE temp:JLM dailyMaxTemp:JLM AGGREGATION max 86400000
OK

> TS.MADD temp:JLM 1672534800000 12 temp:JLM 1672556400000 16 temp:JLM 1672578000000 21 temp:JLM 1672599600000 14
1) (integer) 1672534800000
2) (integer) 1672556400000
3) (integer) 1672578000000
4) (integer) 1672599600000
> TS.MADD temp:JLM 1672621200000 11 temp:JLM 1672642800000 21 temp:JLM 1672664400000 26
1) (integer) 1672621200000
2) (integer) 1672642800000
3) (integer) 1672664400000

> TS.GET dailyMaxTemp:JLM
1) (integer) 1672531200000
2) 21

> TS.GET dailyMaxTemp:JLM LATEST
1) (integer) 1672617600000
2) 26

# TS.INCRBY

# 语法

TS.INCRBY key addend 
  [TIMESTAMP timestamp] 
  [RETENTION retentionPeriod] 
  [ENCODING <COMPRESSED|UNCOMPRESSED>] 
  [CHUNK_SIZE size] 
  [DUPLICATE_POLICY policy] 
  [IGNORE ignoreMaxTimediff ignoreMaxValDiff]   
  [LABELS [label value ...]]

# 描述

增加具有最大现有时间戳的样本的值;或者创建一个新样本,其值等于该最大时间戳样本的值加上给定的增量

# 参数

  • key

键名

  • TIMESTAMP

可以传入一个 Unix 时间戳(整数,单位为毫秒)来指定样本的时间戳;或者传入 *,让系统自动使用服务器当前时钟的 Unix 时间作为样本时间戳

  • RETENTION retentionPeriod

该参数表示最大保留期限,其计算基准为现有的最大时间戳,单位为毫秒。仅在创建新的时间序列时使用此参数;如果向现有的时间序列中添加样本,该参数将被忽略。详情请参阅 TS.CREATE 命令中的 RETENTION 选项

  • ENCODING enc

该参数)用于指定时间序列样本的编码格式。此参数仅在创建新的时间序列时有效;如果向现有的时间序列中添加样本,该参数将被忽略。有关详细信息,请参阅 TS.CREATE 命令中的 ENCODING 选项

  • CHUNK_SIZE size

为每个数据块分配的内存大小,单位是字节。仅在创建新时间序列时使用此参数;如果是向现有时间序列追加样本,该参数会被忽略。详情请参考 TS.CREATE 命令中的 CHUNK_SIZE 参数

  • LABELS

这是一组键值对,用来表示该键的元数据标签,同时也能作为二级索引。如果指定了 LABELS 参数,就会应用你传入的标签列表;而那些不在新列表里的旧标签,会被自动移除。如果只写了 LABELS 但没传任何键值对,就会清空该键上现有的所有标签

# 示例

> TS.INCRBY a 232 TIMESTAMP 1657811829000		// point-of-sale #1
(integer) 1657811829000
> TS.INCRBY a 157 TIMESTAMP 1657811829000		// point-of-sale #2
(integer) 1657811829000
> TS.INCRBY a 432 TIMESTAMP 1657811829000		// point-of-sale #3
(integer) 1657811829000

> ts.incrby a 100 TIMESTAMP 50
(error) TSDB: timestamp must be equal to or higher than the maximum existing timestamp

> TS.INCRBY a 1
(integer) 1658431553109

# TS.INFO

# 语法

TS.INFO key [DEBUG]

# 描述

返回时间序列的信息与统计数据

# 参数

  • key

键名

  • DEBUG

是一个可选的标志,用于获取关于数据块的更详细信息

# 示例

> TS.CREATE telemetry:study:temperature LABELS room study type temperature
OK
> TS.CREATE telemetry:study:humidity LABELS room study type humidity
OK
> TS.CREATE telemetry:kitchen:temperature LABELS room kitchen type temperature
OK
> TS.CREATE telemetry:kitchen:humidity LABELS room kitchen type humidity
OK

> TS.INFO telemetry:kitchen:temperature
 1) totalSamples
 2) (integer) 0
 3) memoryUsage
 4) (integer) 4246
 5) firstTimestamp
 6) (integer) 0
 7) lastTimestamp
 8) (integer) 0
 9) retentionTime
10) (integer) 0
11) chunkCount
12) (integer) 1
13) chunkSize
14) (integer) 4096
15) chunkType
16) compressed
17) duplicatePolicy
18) (nil)
19) labels
20) 1) 1) "room"
       2) "kitchen"
    2) 1) "type"
       2) "temperature"
21) sourceKey
22) (nil)
23) rules
24) (empty array)

> TS.INFO telemetry:kitchen:temperature DEBUG
 1) totalSamples
 2) (integer) 0
 3) memoryUsage
 4) (integer) 4246
 5) firstTimestamp
 6) (integer) 0
 7) lastTimestamp
 8) (integer) 0
 9) retentionTime
10) (integer) 0
11) chunkCount
12) (integer) 1
13) chunkSize
14) (integer) 4096
15) chunkType
16) compressed
17) duplicatePolicy
18) (nil)
19) labels
20) 1) 1) "room"
       2) "kitchen"
    2) 1) "type"
       2) "temperature"
21) sourceKey
22) (nil)
23) rules
24) (empty array)
25) keySelfName
26) "telemetry:kitchen:temperature"
27) Chunks
28) 1)  1) startTimestamp
        2) (integer) 0
        3) endTimestamp
        4) (integer) 0
        5) samples
        6) (integer) 0
        7) size
        8) (integer) 4096
        9) bytesPerSample
       10) "inf"

# TS.MADD

# 语法

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

# 描述

将成员从一个集合移动到另一个集合

# 参数

  • key

键名

  • timestamp

该参数为 Unix 时间戳(整数,单位为毫秒),用于指定样本的时间戳;或者输入 *,表示将样本的时间戳设置为当前服务器时钟的 Unix 时间

  • value

该字段为样本的数值数据。该双精度数字需符合 RFC 7159(JSON 标准)。如果数值过大,超出了 binary64的表示范围,解析器将会拒绝该输入

# 示例

> TS.CREATE stock:A LABELS type stock name A
OK
> TS.CREATE stock:B LABELS type stock name B
OK
> TS.MADD stock:A 1000 100 stock:A 1010 110 stock:A 1020 120 stock:B 1000 120 stock:B 1010 110 stock:B 1020 100
1) (integer) 1000
2) (integer) 1010
3) (integer) 1020
4) (integer) 1000
5) (integer) 1010
6) (integer) 1020

# TS.MGET

# 语法

TS.MGET [LATEST] [WITHLABELS | <SELECTED_LABELS label...>] FILTER filterExpr...

# 描述

  • LATEST

当时间序列是压缩数据时,会用到这个参数。加上 LATEST,TS.MGET 会额外报告最新的那个(可能还没填满的)时间桶的压缩值,前提是这个桶的起始时间在 [fromTimestamp, toTimestamp] 这个范围内。如果不加 LATEST,就不会报告这个最新的、可能还不完整的时间桶。另外,如果时间序列本身不是压缩数据,LATEST 参数就会被直接忽略

  • WITHLABELS

回复中会包含代表该时间序列元数据标签的所有标签-值对。如果没有指定 WITHLABELS 或 SELECTED_LABELS,默认情况下,返回的标签-值对将是一个空列表

  • SELECTED_LABELS

它会返回代表时间序列元数据标签的部分标签-值对。当每个时间序列包含大量标签,但你只需要其中某些标签的值时,就可以使用这个参数

# 示例

9> TS.CREATE temp:TLV LABELS type temp location TLV
OK
> TS.CREATE temp:JLM LABELS type temp location JLM
OK
> TS.MADD temp:TLV 1000 30 temp:TLV 1010 35 temp:TLV 1020 9999 temp:TLV 1030 40
1) (integer) 1000
2) (integer) 1010
3) (integer) 1020
4) (integer) 1030
> TS.MADD temp:JLM 1005 30 temp:JLM 1015 35 temp:JLM 1025 9999 temp:JLM 1035 40
1) (integer) 1005
2) (integer) 1015
3) (integer) 1025
4) (integer) 1035

# TS.MRANGE

# 语法

TS.MRANGE fromTimestamp toTimestamp
  [LATEST]
  [FILTER_BY_TS ts...]
  [FILTER_BY_VALUE min max]
  [WITHLABELS | <SELECTED_LABELS label...>]
  [COUNT count]
  [[ALIGN align] AGGREGATION aggregators bucketDuration [BUCKETTIMESTAMP bt] [EMPTY]]
  FILTER filterExpr...
  [GROUPBY label REDUCE reducer]

# 描述

通过过滤器条件,按时间正序查询多个时间序列的某个时间范围内的数据

# 参数

  • fromTIMESTAMP

指定范围删除的起始时间戳

  • toTIMESTAMP

指定范围删除的结束时间戳

  • LATEST

当时间序列是压缩数据时,会用到这个参数。加上 LATEST,TS.MGET 会额外报告最新的那个(可能还没填满的)时间桶的压缩值,前提是这个桶的起始时间在 [fromTimestamp, toTimestamp] 这个范围内。如果不加 LATEST,就不会报告这个最新的、可能还不完整的时间桶。另外,如果时间序列本身不是压缩数据,LATEST 参数就会被直接忽略

# 示例

> TS.CREATE stock:A LABELS type stock name A
OK
> TS.CREATE stock:B LABELS type stock name B
OK
> TS.MADD stock:A 1000 100 stock:A 1010 110 stock:A 1020 120
1) (integer) 1000
2) (integer) 1010
3) (integer) 1020
> TS.MADD stock:B 1000 120 stock:B 1010 110 stock:B 1020 100
1) (integer) 1000
2) (integer) 1010
3) (integer) 1020

> TS.MRANGE - + WITHLABELS FILTER type=stock GROUPBY type REDUCE max
1) 1) "type=stock"
   2) 1) 1) "type"
         2) "stock"
      2) 1) "__reducer__"
         2) "max"
      3) 1) "__source__"
         2) "stock:A,stock:B"
   3) 1) 1) (integer) 1000
         2) 120
      2) 1) (integer) 1010
         2) 110
      3) 1) (integer) 1020
         2) 120

> TS.CREATE stock:A LABELS type stock name A
OK
> TS.CREATE stock:B LABELS type stock name B
OK
> TS.MADD stock:A 1000 100 stock:A 1010 110 stock:A 1020 120
1) (integer) 1000
2) (integer) 1010
3) (integer) 1020
> TS.MADD stock:B 1000 120 stock:B 1010 110 stock:B 1020 100
1) (integer) 1000
2) (integer) 1010
3) (integer) 1020
> TS.MADD stock:A 2000 200 stock:A 2010 210 stock:A 2020 220
1) (integer) 2000
2) (integer) 2010
3) (integer) 2020
> TS.MADD stock:B 2000 220 stock:B 2010 210 stock:B 2020 200
1) (integer) 2000
2) (integer) 2010
3) (integer) 2020
> TS.MADD stock:A 3000 300 stock:A 3010 310 stock:A 3020 320
1) (integer) 3000
2) (integer) 3010
3) (integer) 3020
> TS.MADD stock:B 3000 320 stock:B 3010 310 stock:B 3020 300
1) (integer) 3000
2) (integer) 3010
3) (integer) 3020

> TS.MRANGE - + WITHLABELS AGGREGATION avg 1000 FILTER type=stock GROUPBY type REDUCE max
1) 1) "type=stock"
   2) 1) 1) "type"
         2) "stock"
      2) 1) "__reducer__"
         2) "max"
      3) 1) "__source__"
         2) "stock:A,stock:B"
   3) 1) 1) (integer) 1000
         2) 110
      2) 1) (integer) 2000
         2) 210
      3) 1) (integer) 3000
         2) 310

> TS.ADD ts1 1548149180000 90 labels metric cpu metric_name system
(integer) 1548149180000
> TS.ADD ts1 1548149185000 45
(integer) 1548149185000
> TS.ADD ts2 1548149180000 99 labels metric cpu metric_name user
(integer) 1548149180000
> TS.MRANGE - + WITHLABELS FILTER metric=cpu GROUPBY metric_name REDUCE max
1) 1) "metric_name=system"
   2) 1) 1) "metric_name"
         2) "system"
      2) 1) "__reducer__"
         2) "max"
      3) 1) "__source__"
         2) "ts1"
   3) 1) 1) (integer) 1548149180000
         2) 90
      2) 1) (integer) 1548149185000
         2) 45
2) 1) "metric_name=user"
   2) 1) 1) "metric_name"
         2) "user"
      2) 1) "__reducer__"
         2) "max"
      3) 1) "__source__"
         2) "ts2"
   3) 1) 1) (integer) 1548149180000
         2) 99

 TS.ADD ts1 1548149180000 90 labels metric cpu metric_name system
(integer) 1548149180000
> TS.ADD ts1 1548149185000 45
(integer) 1548149185000
> TS.ADD ts2 1548149180000 99 labels metric cpu metric_name user
(integer) 1548149180000
> TS.MRANGE - + FILTER_BY_VALUE 90 100 WITHLABELS FILTER metric=cpu
1) 1) "ts1"
   2) 1) 1) "metric"
         2) "cpu"
      2) 1) "metric_name"
         2) "system"
   3) 1) 1) (integer) 1548149180000
         2) 90
2) 1) "ts2"
   2) 1) 1) "metric"
         2) "cpu"
      2) 1) "metric_name"
         2) "user"
   3) 1) 1) (integer) 1548149180000
         2) 99

> TS.ADD ts1 1548149180000 90 labels metric cpu metric_name system team NY
(integer) 1548149180000
> TS.ADD ts1 1548149185000 45
(integer) 1548149185000
> TS.ADD ts2 1548149180000 99 labels metric cpu metric_name user team SF
(integer) 1548149180000
> TS.MRANGE - + SELECTED_LABELS team FILTER metric=cpu
1) 1) "ts1"
   2) 1) 1) "team"
         2) "NY"
   3) 1) 1) (integer) 1548149180000
         2) 90
      2) 1) (integer) 1548149185000
         2) 45
2) 1) "ts2"
   2) 1) 1) "team"
         2) "SF"
   3) 1) 1) (integer) 1548149180000
         2) 99

# TS.MREVRANGE

# 语法

TS.MREVRANGE fromTimestamp toTimestamp
  [LATEST]
  [FILTER_BY_TS ts...]
  [FILTER_BY_VALUE min max]
  [WITHLABELS | <SELECTED_LABELS label...>]
  [COUNT count]
  [[ALIGN align] AGGREGATION aggregators bucketDuration [BUCKETTIMESTAMP bt] [EMPTY]]
  FILTER filterExpr...
  [GROUPBY label REDUCE reducer]

# 描述

通过过滤器按反向顺序查询多个时间序列的区间数据

# 参数

  • fromTIMESTAMP

指定范围删除的起始时间戳

  • toTIMESTAMP

指定范围删除的结束时间戳

  • LATEST

当时间序列是压缩数据时,会用到这个参数。加上 LATEST,TS.MGET 会额外报告最新的那个(可能还没填满的)时间桶的压缩值,前提是这个桶的起始时间在 [fromTimestamp, toTimestamp] 这个范围内。如果不加 LATEST,就不会报告这个最新的、可能还不完整的时间桶。另外,如果时间序列本身不是压缩数据,LATEST 参数就会被直接忽略

  • FILTER_BY_TS ts

通过一组特定的时间戳列表来过滤样本。如果样本的精确时间戳被包含在列表中,且落在 [fromTimestamp, toTimestamp] 这个时间区间内,则该样本可以通过过滤

  • FILTER_BY_VALUE min max

根据最小值和最大值来过滤样本。其中,最小值和最大值不能是 非数字值。当与聚合函数一起使用时,样本会先被过滤,然后再进行聚合

  • WITHLABELS

回复中会包含代表该时间序列元数据标签的所有标签-值对。如果没有指定 WITHLABELS 或 SELECTED_LABELS,默认情况下,返回的标签-值对将是一个空列表

  • COUNT

当不配合聚合使用时:用于限制每个时间序列返回的样本数量。当与聚合配合使用时:用于限制返回的桶的数量

# 示例

> TS.CREATE stock:A LABELS type stock name A
OK
> TS.CREATE stock:B LABELS type stock name B
OK
> TS.MADD stock:A 1000 100 stock:A 1010 110 stock:A 1020 120
1) (integer) 1000
2) (integer) 1010
3) (integer) 1020
> TS.MADD stock:B 1000 120 stock:B 1010 110 stock:B 1020 100
1) (integer) 1000
2) (integer) 1010
3) (integer) 1020

> TS.MREVRANGE - + WITHLABELS FILTER type=stock GROUPBY type REDUCE max
1) 1) "type=stock"
   2) 1) 1) "type"
         2) "stock"
      2) 1) "__reducer__"
         2) "max"
      3) 1) "__source__"
         2) "stock:A,stock:B"
   3) 1) 1) (integer) 1020
         2) 120
      2) 1) (integer) 1010
         2) 110
      3) 1) (integer) 1000
         2) 120

> TS.CREATE stock:A LABELS type stock name A
OK
> TS.CREATE stock:B LABELS type stock name B
OK
> TS.MADD stock:A 1000 100 stock:A 1010 110 stock:A 1020 120
1) (integer) 1000
2) (integer) 1010
3) (integer) 1020
> TS.MADD stock:B 1000 120 stock:B 1010 110 stock:B 1020 100
1) (integer) 1000
2) (integer) 1010
3) (integer) 1020
> TS.MADD stock:A 2000 200 stock:A 2010 210 stock:A 2020 220
1) (integer) 2000
2) (integer) 2010
3) (integer) 2020
> TS.MADD stock:B 2000 220 stock:B 2010 210 stock:B 2020 200
1) (integer) 2000
2) (integer) 2010
3) (integer) 2020
> TS.MADD stock:A 3000 300 stock:A 3010 310 stock:A 3020 320
1) (integer) 3000
2) (integer) 3010
3) (integer) 3020
> TS.MADD stock:B 3000 320 stock:B 3010 310 stock:B 3020 300
1) (integer) 3000
2) (integer) 3010
3) (integer) 3020

> TS.MREVRANGE - + WITHLABELS AGGREGATION avg 1000 FILTER type=stock GROUPBY type REDUCE max
1) 1) "type=stock"
   2) 1) 1) "type"
         2) "stock"
      2) 1) "__reducer__"
         2) "max"
      3) 1) "__source__"
         2) "stock:A,stock:B"
   3) 1) 1) (integer) 3000
         2) 310
      2) 1) (integer) 2000
         2) 210
      3) 1) (integer) 1000
         2) 110

# TS.QUERYINDEX

# 语法

TS.QUERYINDEX filterExpr...

# 描述

获取所有匹配过滤条件列表的时间序列键

# 参数

  • filterExpr

  • filterExpr……用于根据时间序列的标签及其值来进行过滤。每个过滤表达式支持以下语法之一:

    • label!=:时间序列必须包含名为 label 的标签
    • label=value:时间序列必须包含名为 label 的标签,且其值等于 value
    • label=(value1,value2,...):时间序列必须包含名为 label 的标签,且其值等于列表中的某一个值。
    • label!=:时间序列不能包含名为 label 的标签
    • label!=value:时间序列不能包含名为 label 且值等于 value 的标签。
    • label!=(value1,value2,...):时间序列不能包含名为 label 且值等于列表中任何一个值的标签

# SUNION

# 语法

SUNION key [key ...]

# 描述

返回多个集合的并集

# 参数

  • key

一个或多个要进行并集操作的集合键

# 示例

> TS.CREATE telemetry:study:temperature LABELS room study type temperature
OK
> TS.CREATE telemetry:study:humidity LABELS room study type humidity
OK
> TS.CREATE telemetry:kitchen:temperature LABELS room kitchen type temperature
OK
> TS.CREATE telemetry:kitchen:humidity LABELS room kitchen type humidity
Ok

> TS.QUERYINDEX room=kitchen
1) "telemetry:kitchen:humidity"
2) "telemetry:kitchen:temperature"

> TS.QUERYINDEX type=temperature
1) "telemetry:kitchen:temperature"
2) "telemetry:study:temperature"

# TS.RANGE

# 语法

TS.RANGE key fromTimestamp toTimestamp
  [LATEST]
  [FILTER_BY_TS ts...]
  [FILTER_BY_VALUE min max]
  [COUNT count] 
  [[ALIGN align] AGGREGATION aggregators bucketDuration [BUCKETTIMESTAMP bt] [EMPTY]]

# 描述

按正查询指定时间区间的数据

# 参数

  • key

键名

  • fromTIMESTAMP

指定范围删除的起始时间戳

  • toTIMESTAMP

指定范围删除的结束时间戳

  • LATEST

当时间序列是压缩序列时,会用到这个参数

  • FILTER_BY_TS ts

通过一组特定的时间戳列表来过滤样本。如果样本的精确时间戳被包含在列表中,且落在 [fromTimestamp, toTimestamp] 这个时间区间内,则该样本可以通过过滤

  • FILTER_BY_VALUE min max

根据最小值和最大值来过滤样本。其中,最小值和最大值不能是 非数字值。当与聚合函数一起使用时,样本会先被过滤,然后再进行聚合

  • COUNT

当不配合聚合使用时:用于限制每个时间序列返回的样本数量。当与聚合配合使用时:用于限制返回的桶的数量

  • **ALIGN **

用于控制聚合(AGGREGATION)的时间桶对齐方式。它通过更改定义时间桶的参考时间戳,来调整时间桶的时间戳。可选的值包括:

  • start 或 -:参考时间戳将是查询的起始时间,该值不能为 -
  • end 或 +:参考时间戳将是查询的结束时间,该值不能为 +
  • 特定的时间戳:将参考时间戳对齐到指定的时间

# 示例

> TS.CREATE temp:TLV LABELS type temp location TLV
OK
> TS.MADD temp:TLV 1000 30 temp:TLV 1010 35 temp:TLV 1020 9999 temp:TLV 1030 40
1) (integer) 1000
2) (integer) 1010
3) (integer) 1020
4) (integer) 1030

TS.RANGE temp:TLV - + FILTER_BY_VALUE -100 100
1) 1) (integer) 1000
   2) 30
2) 1) (integer) 1010
   2) 35
3) 1) (integer) 1030
   2) 40

TS.RANGE temp:TLV - + FILTER_BY_VALUE -100 100 AGGREGATION min,avg,max 1000
1) 1) (integer) 1000
   2) 30
   3) 35
   4) 40

> TS.CREATE stock:A LABELS type stock name A
OK
> TS.MADD stock:A 1000 100 stock:A 1010 110 stock:A 1020 120
1) (integer) 1000
2) (integer) 1010
3) (integer) 1020
> TS.MADD stock:A 2000 200 stock:A 2010 210 stock:A 2020 220
1) (integer) 2000
2) (integer) 2010
3) (integer) 2020
> TS.MADD stock:A 3000 300 stock:A 3010 310 stock:A 3020 320
1) (integer) 3000
2) (integer) 3010
3) (integer) 3020

> TS.RANGE stock:A - + AGGREGATION min 20
1) 1) (integer) 1000
   2) 100
2) 1) (integer) 1020
   2) 120
3) 1) (integer) 2000
   2) 200
4) 1) (integer) 2020
   2) 220
5) 1) (integer) 3000
   2) 300
6) 1) (integer) 3020
   2) 320

> TS.RANGE stock:A - + ALIGN 10 AGGREGATION min 20
1) 1) (integer) 990
   2) 100
2) 1) (integer) 1010
   2) 110
3) 1) (integer) 1990
   2) 200
4) 1) (integer) 2010
   2) 210
5) 1) (integer) 2990
   2) 300
6) 1) (integer) 3010
   2) 310

> TS.RANGE stock:A 5 + ALIGN - AGGREGATION min 20
1) 1) (integer) 985
   2) 100
2) 1) (integer) 1005
   2) 110
3) 1) (integer) 1985
   2) 200
4) 1) (integer) 2005
   2) 210
5) 1) (integer) 2985
   2) 300
6) 1) (integer) 3005
   2) 310

# TS.REVRANGE

# 语法

TS.REVRANGE key fromTimestamp toTimestamp
  [LATEST]
  [FILTER_BY_TS ts...]
  [FILTER_BY_VALUE min max]
  [COUNT count]
  [[ALIGN align] AGGREGATION aggregators bucketDuration [BUCKETTIMESTAMP bt] [EMPTY]]

# 描述

按反方向查询某个范围

# 参数

  • key

键名

  • fromTIMESTAMP

指定范围删除的起始时间戳

  • toTIMESTAMP

指定范围删除的结束时间戳

  • LATEST

当时间序列是压缩序列时,会用到这个参数

  • FILTER_BY_TS ts

通过一组特定的时间戳列表来过滤样本。如果样本的精确时间戳被包含在列表中,且落在 [fromTimestamp, toTimestamp] 这个时间区间内,则该样本可以通过过滤

  • FILTER_BY_VALUE min max

根据最小值和最大值来过滤样本。其中,最小值和最大值不能是 非数字值。当与聚合函数一起使用时,样本会先被过滤,然后再进行聚合

  • COUNT

当不配合聚合使用时:用于限制每个时间序列返回的样本数量。当与聚合配合使用时:用于限制返回的桶的数量

  • **ALIGN **

用于控制聚合(AGGREGATION)的时间桶对齐方式。它通过更改定义时间桶的参考时间戳,来调整时间桶的时间戳。可选的值包括:

  • start 或 -:参考时间戳将是查询的起始时间,该值不能为 -
  • end 或 +:参考时间戳将是查询的结束时间,该值不能为 +
  • 特定的时间戳:将参考时间戳对齐到指定的时间

# 示例

> TS.CREATE temp:TLV LABELS type temp location TLV
OK
> TS.MADD temp:TLV 1000 30 temp:TLV 1010 35 temp:TLV 1020 9999 temp:TLV 1030 40
1) (integer) 1000
2) (integer) 1010
3) (integer) 1020
4) (integer) 1030

TS.REVRANGE temp:TLV - + FILTER_BY_VALUE -100 100
1) 1) (integer) 1030
   2) 40
2) 1) (integer) 1010
   2) 35
3) 1) (integer) 1000
   2) 30

TS.REVRANGE temp:TLV - + FILTER_BY_VALUE -100 100 AGGREGATION min,avg,max 1000
1) 1) (integer) 1000
   2) 30
   3) 35
   4) 40

> TS.CREATE stock:A LABELS type stock name A
OK
> TS.MADD stock:A 1000 100 stock:A 1010 110 stock:A 1020 120
1) (integer) 1000
2) (integer) 1010
3) (integer) 1020
> TS.MADD stock:A 2000 200 stock:A 2010 210 stock:A 2020 220
1) (integer) 2000
2) (integer) 2010
3) (integer) 2020
> TS.MADD stock:A 3000 300 stock:A 3010 310 stock:A 3020 320
1) (integer) 3000
2) (integer) 3010
3) (integer) 3020

> TS.REVRANGE stock:A - + AGGREGATION min 20
1) 1) (integer) 3020
   2) 320
2) 1) (integer) 3000
   2) 300
3) 1) (integer) 2020
   2) 220
4) 1) (integer) 2000
   2) 200
5) 1) (integer) 1020
   2) 120
6) 1) (integer) 1000
   2) 100

> TS.REVRANGE stock:A - + ALIGN 10 AGGREGATION min 20
1) 1) (integer) 3010
   2) 310
2) 1) (integer) 2990
   2) 300
3) 1) (integer) 2010
   2) 210
4) 1) (integer) 1990
   2) 200
5) 1) (integer) 1010
   2) 110
6) 1) (integer) 990
   2) 100

> TS.REVRANGE stock:A 5 + ALIGN - AGGREGATION min 20
1) 1) (integer) 3005
   2) 310
2) 1) (integer) 2985
   2) 300
3) 1) (integer) 2005
   2) 210
4) 1) (integer) 1985
   2) 200
5) 1) (integer) 1005
   2) 110
6) 1) (integer) 985
   2) 100