V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐学习书目
Learn Python the Hard Way
Python Sites
PyPI - Python Package Index
http://diveintopython.org/toc/index.html
Pocoo
值得关注的项目
PyPy
Celery
Jinja2
Read the Docs
gevent
pyenv
virtualenv
Stackless Python
Beautiful Soup
结巴中文分词
Green Unicorn
Sentry
Shovel
Pyflakes
pytest
Python 编程
pep8 Checker
Styles
PEP 8
Google Python Style Guide
Code Style from The Hitchhiker's Guide
copyangle
V2EX  ›  Python

使用 redis-py-cloud 操作 redis5 stream 数据

  •  
  •   copyangle · 2019-04-04 10:47:37 +08:00 · 1294 次点击
    这是一个创建于 1842 天前的主题,其中的信息可能已经有所发展或是发生改变。

    redis-py-cloud 是基于 redis-py-cluster 开发的 redis 集群包,特别支持 redis5.0 的 stream 新特性。目前支持的命令有 xadd, xread, xreadgroup, xack 等。

    包下载地址:

    https://github.com/ChinaGoldBear/redis-py-cloud

    或 pip install:

    pip install redis-py-cloud

    基本操作:

    1.连接集群

    from rediscluster import RedisCluster

    '''连接集群''' startup_nodes = [{"host": "ip3", "port": "7000"}, {"host": "ip2", "port": "7000"}, {"host": "ip1", "port": "7000"}, ]

    REDIS_CLUSTER = RedisCluster(startup_nodes=startup_nodes, decode_responses=True)

    2.xadd

    REDIS_CLUSTER.xadd("mystream", "*", 100,{"name": "data"})

    3.xread

    REDIS_CLUSTER.xread("mystream","*",1) # 非阻塞读取最后一条

    REDIS_CLUSTER.xread("mystream","*",1,0) # 阻塞读取最后一条

    REDIS_CLUSTER.xread("mystream","*",10,0) # 阻塞读取最后 10 条

    REDIS_CLUSTER.xread("mystream","1527849629172-0",1,0) # 阻塞读取特定消息

    1. xrange

    REDIS_CLUSTER.xrange("mystream","-","+") # -表示最小值, +表示最大值

    REDIS_CLUSTER.xrange("mystream","1527849629172-0","+") # 指定最小消息 ID 的列表

    REDIS_CLUSTER.xrange("mystream","-","1527849629172-0") # 指定最大消息 ID 的列表

    5.xreadgroup

    REDIS_CLUSTER.xreadgroup("group_name","consumer_name","mystream",">",0) # 消息组读取,0 表示阻塞读取

    REDIS_CLUSTER.xreadgroup("group_name","consumer_name","mystream",">") # 消息组读取,非阻塞读取

    REDIS_CLUSTER.xreadgroup("group_name","consumer_name","mystream","1527849629172-0") # 消息组特定消息读取,非阻塞读取

    6.xack

    REDIS_CLUSTER.xack("mystream","counsumer_name","1527849629172-0") # ack 消费组消息

    其他 stream 命令请参考:

    https://blog.csdn.net/enmotech/article/details/81230531

    作者:copyangle 来源:CSDN 原文: https://blog.csdn.net/copyangle/article/details/81975975 版权声明:本文为博主原创文章,转载请附上博文链接!

    目前尚无回复
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   4834 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 26ms · UTC 03:55 · PVG 11:55 · LAX 20:55 · JFK 23:55
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.