概况

A Redis stream is a data structure that acts like an append-only log. You can use streams to record and simultaneously syndicate events in real time. Examples of Redis stream use cases include:

  • Event sourcing (e.g., tracking user actions, clicks, etc.)
  • Sensor monitoring (e.g., readings from devices in the field)
  • Notifications (e.g., storing a record of each user's notifications in a separate stream)

Redis generates a unique ID for each stream entry. You can use these IDs to retrieve their associated entries later or to read and process all subsequent entries in the stream.

Redis streams support several trimming strategies (to prevent streams from growing unbounded) and more than one consumption strategy (see XREADXREADGROUP, and XRANGE).

redis stream 是一个追加式的数据结构,可以用在以下三个场景:
1、事件溯源,比如跟踪用户的操作,点击,等
2、传感监视,如某些专业领域的设置监控
3、消息 可以把不同用户的消息存在一个单据的stream中
127.0.0.1:6386> XADD temperatures:us-ny:10007 * temp_f 87.2 pressure 29.69 humidity 46
"1678622044821-0"
127.0.0.1:6386> XADD temperatures:us-ny:10007 * temp_f 83.1 pressure 29.21 humidity 46.5
"1678622061283-0"
127.0.0.1:6386> XADD temperatures:us-ny:10007 * temp_f 81.9 pressure 28.37 humidity 43.7
"1678622071669-0"
127.0.0.1:6386> XRANGE temperatures:us-ny:10007 1658354934941-0 + COUNT 2 [COUNT count]
[root@machine138 redis-stack]# bin/redis-cli -p 6386
127.0.0.1:6386> XRANGE temperatures:us-ny:10007 1678622061283-0 + COUNT 2
1) 1) "1678622061283-0"
2) 1) "temp_f"
2) "83.1"
3) "pressure"
4) "29.21"
5) "humidity"
6) "46.5"
2) 1) "1678622071669-0"
2) 1) "temp_f"
2) "81.9"
3) "pressure"
4) "28.37"
5) "humidity"
6) "43.7"

xadd:唯一可以把数据存入stream中的命令

XADD

Syntax
XADD key [NOMKSTREAM] [<MAXLEN | MINID> [= | ~] threshold
[LIMIT count]] <* | id> field value [field value ...]

Appends the specified stream entry to the stream at the specified key. If the key does not exist, as a side effect of running this command the key is created with a stream value. The creation of stream's key can be disabled with the NOMKSTREAM option.

An entry is composed of a list of field-value pairs. The field-value pairs are stored in the same order they are given by the user. Commands that read the stream, such as XRANGE or XREAD, are guaranteed to return the fields and values exactly in the same order they were added by XADD.

XADD is the only Redis command that can add data to a stream, but there are other commands, such as XDEL and XTRIM, that are able to remove data from a stream.

Streams are an append-only data structure. The fundamental write command, called XADD, appends a new entry to the specified stream.

each stream entry consists of one or more field-value pairs, somewhat like a record or a Redis hash

The entry ID returned by the XADD command, and identifying univocally each entry inside a given stream, is composed of two parts:

xadd命令可以创建一个stream 并把 键值对存入其中,顺序与用户输入的顺序相同,可以通过xread,xrange等命令读取,
每个stream entry 都由一个或多个key-value 组成,有点像hashes (HSET key value,HGET key value)

例如:XADD mystream * sensor-id 1234 temperature 19.8
mystream:stream key
*:自动生成 stream ID :
<millisecondsTime>-<sequenceNumber> millisecondsTime:就是本地时间
sequenceNumber:就是同一个millisecondsTime之内产生的64位整数

sensor-id:key1
1234:vlaue1
temperature:key2
19.8 value2

另外,ID可以自定义,但是要遵循以下几个原则 :

1、格式必须是 :number-seq ,其中 number可以是任何数字,但是要确保后面的ID 的number 不能比之前的ID 小,等于之前的ID 中number时,必须使seq大于前一个seq,否则会提示

ERR The ID specified in XADD is equal or smaller than the target stream top item,在redis 7.0之后 ID 中seq 可以用*代替   如:

XADD somestream 0-* baz qux
0-3
2、-seq 可以省略,redis 自动补全 -0,如 127.0.0.1:6386> xadd ms1 20 key1 value1
"20-0" 正式上面的对ID的规则 ,才有后面的命令 XRANGE XRANGE key start end [COUNT count]


Getting data from Streams

Now we are finally able to append entries in our stream via XADD. However, while appending data to a stream is quite obvious, the way streams can be queried in order to extract data is not so obvious. If we continue with the analogy of the log file, one obvious way is to mimic what we normally do with the Unix command tail -f, that is, we may start to listen in order to get the new messages that are appended to the stream. Note that unlike the blocking list operations of Redis, where a given element will reach a single client which is blocking in a pop style operation like BLPOP, with streams we want multiple consumers to see the new messages appended to the stream (the same way many tail -f processes can see what is added to a log). Using the traditional terminology we want the streams to be able to fan out messages to multiple clients.

However, this is just one potential access mode. We could also see a stream in quite a different way: not as a messaging system, but as a time series store. In this case, maybe it's also useful to get the new messages appended, but another natural query mode is to get messages by ranges of time, or alternatively to iterate the messages using a cursor to incrementally check all the history. This is definitely another useful access mode.

Finally, if we see a stream from the point of view of consumers, we may want to access the stream in yet another way, that is, as a stream of messages that can be partitioned to multiple consumers that are processing such messages, so that groups of consumers can only see a subset of the messages arriving in a single stream. In this way, it is possible to scale the message processing across different consumers, without single consumers having to process all the messages: each consumer will just get different messages to process. This is basically what Kafka (TM) does with consumer groups. Reading messages via consumer groups is yet another interesting mode of reading from a Redis Stream.

Redis Streams support all three of the query modes described above via different commands. The next sections will show them all, starting from the simplest and most direct to use: range queries.

这一段主要是stream 读取方式的由来,最后采取了类型Kafka类似的方式,做为stream 的读取方式。

To query the stream by range we are only required to specify two IDs, start and end. The range returned will include the elements having start or end as ID, so the range is inclusive. The two special IDs - and + respectively mean the smallest and the greatest ID possible.

127.0.0.1:6386> xrange ms1 - +
1) 1) "1-1"
2) 1) "key1"
2) "value1"
2) 1) "12-0"
2) 1) "key1"
2) "value1"
3) 1) "13-0"
2) 1) "key1"
2) "value1"
4) 1) "13-1"
2) 1) "key1"
2) "value1"
5) 1) "20-0"
2) 1) "key1"
2) "value1"

- :表示当前stream (ms1)下最小ID 的entry

+: 表当前stream (ms1)下最大ID 的entry

后面的 count  n可以限定获取多少条entry (范围内前n个entry)

127.0.0.1:6386> xrange ms1 - + count 2
1) 1) "1-1"
2) 1) "key1"
2) "value1"
2) 1) "12-0"
2) 1) "key1"
2) "value1"
127.0.0.1:6386>
127.0.0.1:6386> xrange ms1 1 13-1
1) 1) "1-1"
2) 1) "key1"
2) "value1"
2) 1) "12-0"
2) 1) "key1"
2) "value1"
3) 1) "13-0"
2) 1) "key1"
2) "value1"
4) 1) "13-1"
2) 1) "key1"
2) "value1"

上面的 1:表示从1-0 开始的ID

13-1:表示要获取的最大的ID

127.0.0.1:6386> xrange ms1 1 13-1 count 2
1) 1) "1-1"
2) 1) "key1"
2) "value1"
2) 1) "12-0"
2) 1) "key1"
2) "value1"

在上面的命令中,如果 要完成后面的ENTRY的读取,需要借助 小括号 ( 后面跟已经读取到的最大的iD)

127.0.0.1:6386> xrange ms1 (12-0 + count 2
1) 1) "13-0"
2) 1) "key1"
2) "value1"
2) 1) "13-1"
2) 1) "key1"
2) "value1"

也可以选择确定的结束 ID

127.0.0.1:6386> xrange ms1 (12-0 13-1 count 2
1) 1) "13-0"
2) 1) "key1"
2) "value1"
2) 1) "13-1"
2) 1) "key1"
2) "value1"

上面是按照存入的顺序 获取的,如果 想按照存入的顺序相反的次序获取entry可以使用  xrevrange

XREVRANGE end start [count n]  注意 开始 和结束 id 与range 命令相比是 相反的。   reverse order!!!

127.0.0.1:6386> xrevrange ms1 15 1 count 1
1) 1) "13-1"
2) 1) "key1"
2) "value1"

+ - 同样适用

127.0.0.1:6386> xrevrange ms1 + - count 1
1) 1) "20-0"
2) 1) "key1"
2) "value1"

后面会学习 xread 命令。。。。

Redis Streams tutorial | Redis

Redis Stream Commands 命令学习-1 XADD XRANGE XREVRANGE的更多相关文章

  1. 【Redis数据库】命令学习笔记——发布订阅、事务、脚本、连接等命令汇总

    本篇基于redis 4.0.11版本,学习发布订阅.事务.脚本.连接的相关命令. Redis 发布订阅(pub/sub)是一种消息通信模式:发送者(pub)发送消息,订阅者(sub)接收消息. 序号 ...

  2. Redis Stream类型的使用

    一.背景 最近在看redis这方面的知识,发现在redis5中产生了一种新的数据类型Stream,它和kafka的设计有些类似,可以当作一个简单的消息队列来使用. 二.redis中Stream类型的特 ...

  3. Redis 命令学习

    每天不学习点新的东西,感觉就有点会被社会淘汰掉了.也许现在学习的知识会很快忘记,下次学习用到这个知识点的时候,再回来翻记录的笔记,我想这样会比从头再学,效率会高点吧. 闲话不多聊,回归正题.今天学习r ...

  4. 【Redis】命令学习笔记——列表(list)+集合(set)+有序集合(sorted set)(17+15+20个超全字典版)

    本篇基于redis 4.0.11版本,学习列表(list)和集合(set)和有序集合(sorted set)相关命令. 列表按照插入顺序排序,可重复,可以添加一个元素到列表的头部(左边)或者尾部(右边 ...

  5. 【Redis】命令学习笔记——哈希(hash)(15个超全字典版)

    本篇基于redis 4.0.11版本,学习哈希(hash)相关命令. hash 是一个string类型的field和value的映射表,特别适合用于存储对象. 序号 命令 描述 实例 返回 HSET ...

  6. 【Redis】命令学习笔记——字符串(String)(23个超全字典版)

    Redis支持五种数据类型:string(字符串),hash(哈希),list(列表),set(集合)及zset(sorted set:有序集合). 本篇基于redis 4.0.11版本,学习字符串( ...

  7. 【Redis】命令学习笔记——键(key)(20个超全字典版)

    安装完redis和redis-desktop-manager后,开始学习命令啦!本篇基于redis 4.0.11版本,从对键(key)开始挖坑! 准备工作,使用db1(默认db0,由于之前练习用db0 ...

  8. redis命令学习(二) · THIS SPACE

    列表(Lists)操作命令 Redis列表是简单的字符串列表,按照插入顺序排序. 你可以添加一个元素导列表的头部(左边)或者尾部(右边)LPUSH命令插入一个新的元素导头部,而RPUSH插入一个新元素 ...

  9. Redis入门及常用命令学习

    Redis简介 Redis 是完全开源的,遵守 BSD 协议,是一个高性能的 key-value 数据库. Redis 与其他 key - value 缓存产品有以下三个特点: Redis支持数据的持 ...

  10. Redis 网络通信及连接机制学习

    看了这篇文章 http://blog.nosqlfan.com/html/4153.html 本文所述内容基于 Redis2.6 及以上版本. 注:在客户端通过 info 命令可以查看服务器版本信息, ...

随机推荐

  1. PAT-basic-1021 个位数统计 java

    一.题目 给定一个 k 位整数 N=dk−1​10k−1+⋯+d1​101+d0​ (0≤di​≤9, i=0,⋯,k−1, dk−1​>0),请编写程序统计每种不同的个位数字出现的次数.例如: ...

  2. Alfred 好用工具分享

    好用的mac工具分享 1.带历史记录的剪切板 根据快捷键切换选择保存的数据 2.创建热键工作流程 将常用的工具变为热键,快捷切换 如何创建: 设置热键 单击右键 创建actions 打开app 然后将 ...

  3. 【2020NOI.AC省选模拟#7】A. t1

    题目链接 原题解: 由于$+$满足幂等性,我们可以设$f_{i,j}$为从$i$号点向根$2^j$个点的权值之和,并且倍增计算出$f$.在查询是,可以像ST表一样用至多四个$f$中的路径拼出询问路径. ...

  4. 问题:配置apache的相关配置文件报错:Invalid command 'Order' (已解决)

    1. 问题描述 在虚拟文件httpd-vhosts.conf里面,directory里加入Order allow,deny,重启apache,出现Invalid command 'Order', pe ...

  5. iOS开发--APP性能检测方案汇总

    1 . CPU 占用率 CPU作为手机的中央处理器,可以说是手机最关键的组成部分,所有应用程序都需要它来调度运行,资源有限.所以当我们的APP因设计不当,使 CPU 持续以高负载运行,将会出现APP卡 ...

  6. Linux软件防火墙iptables

    Netfilter组件 内核空间,集成在linux内核中 官网文档:https://netfilter.org/documentation/ 扩展各种网络服务的结构化底层框架 内核中选取五个位置放了五 ...

  7. 如何为linux kernel贡献代码

    参考: Linux内核文档:<如何让你的改动进入内核> - 广漠飘羽 - 博客园 提交内核补丁到Linux社区的步骤 - 广漠飘羽 - 博客园 建议: 内容具有时效性,需要阅读最新版本的同 ...

  8. 4、Redis底层原理(持久化+分布式锁)

    Redis底层原理 持久化 Redis虽然是个内存数据库,但是Redis支持RDB和AOF两种持久化机制,将数据写往磁盘,可以有效地避免因进程退出造成的数据丢失问题,当下次重启时利用之前持久化的文件即 ...

  9. 【Leetcode第285场周赛】——周赛总结

    1.6027. 统计数组中峰和谷的数量 - 力扣(LeetCode) (leetcode-cn.com) 给你一个下标从 0 开始的整数数组 nums .如果两侧距 i 最近的不相等邻居的值均小于 n ...

  10. 20201003--统计数字字符个数(奥赛一本通 P98 1--字符类型和字符数组)

    输入一行字符,统计出其中数字字符的个数 输入:一行字符串,总长度不超过255 输出:一行,输出字符串里面数字字符的个数. 样例输入:Peking University is set up at 189 ...