本文为博主原创,未经授权,严禁转载及使用。
本文链接:https://blog.csdn.net/zyooooxie/article/details/123760358

之前曾经分享过2篇 关于Redis命令:https://blog.csdn.net/zyooooxie/article/details/120334491 、 https://blog.csdn.net/zyooooxie/article/details/95352530,这一篇博客是 讲Redis cluster对key相关的操作。

此外 有些博客 写到 python的redis库是不支持集群操作,现在已经支持了。

https://pypi.org/project/redis/ redis-py now supports cluster mode and provides a client for Redis Cluster.

Redis集群 命令

CLUSTER INFO

CLUSTER KEYSLOT key

CLUSTER NODES

CLUSTER SLAVES node-id

CLUSTER GETKEYSINSLOT slot count

CLUSTER COUNTKEYSINSLOT slot

想要更加详细学习 可以参考: https://redis.io/commands/?group=cluster

我的疑惑

在接触到集群命令后,我不仅仅 KEYSLOT(test_key), 我还实际去对应master节点去查看 这个test_key,然后我就发现:公司好几个Redis集群 每个master节点都有这个test_key。

这。。。很遗憾,没确切找到原因。

此外,Redis 集群不支持那些需要同时处理多个键的 Redis 命令, 因为执行这些命令需要在多个 Redis 节点之间移动数据, 并且在高负载的情况下, 这些命令将降低 Redis 集群的性能, 并导致不可预测的行为。

但 我发现 redis-py-cluster 是支持mset、mget的,所以本期的代码 主要讲 2个库 如何连接 集群。

代码:redis-py

最初搜 使用Python操作 Redis cluster,所有答案都是redis-py 不支持。但我在学习 cluster commands,看到 https://redis-py.readthedocs.io/en/stable/commands.html#redis-cluster-commands,我恍惚了,又确认好几遍 是redis-py,才去翻看pypi redis-py的Project description。

我在使用 redis 4.1.4。

"""
@blog: https://blog.csdn.net/zyooooxie
"""def test_rp():"""redis-py:return:"""from redis.cluster import RedisClusterfrom redis.cluster import ClusterNodehost2 = 'host'port = 6666pwd = 'pwd'nodes = [ClusterNode(host2, port)]rc = RedisCluster(startup_nodes=nodes, password=pwd, decode_responses=True)Log.info(rc)Log.info(rc.cluster_info())Log.info(rc.cluster_slots())Log.info(rc.cluster_nodes())exist_key = 'exist_key'exist_key_slot = rc.cluster_keyslot(exist_key)Log.info(exist_key_slot)Log.info(rc.cluster_countkeysinslot(exist_key_slot))Log.info(rc.cluster_get_keys_in_slot(exist_key_slot, 5))Log.info('------')Log.info('------')Log.info('------')cluster_test_str = 'test_str'Log.info(rc.set(cluster_test_str, 'zyooooxie', ex=random.randint(600, 1000)))Log.info(rc.cluster_keyslot(cluster_test_str))Log.info(rc.exists(cluster_test_str))Log.info(rc.type(cluster_test_str))Log.info(rc.ttl(cluster_test_str))Log.info(rc.expire(cluster_test_str, 2 * 60 * 60))Log.info(rc.ttl(cluster_test_str))Log.info(rc.get(cluster_test_str))Log.info(rc.getset(cluster_test_str, '新值str'))Log.info(rc.get(cluster_test_str))# 类似mset, mget这样的多个key的原生批量操作命令, redis集群只支持所有key落在同一slot的情况key1 = 'test1'key2 = 'test2'Log.info(rc.cluster_keyslot(key1))Log.info(rc.cluster_keyslot(key2))# Log.info(rc.mset({key1: 'Redis Cluster-{}'.format(key1), key2: 'Redis Cluster-{}'.format(key2)}))# # TODO 留意:redis.exceptions.RedisClusterException: MSET - all keys must map to the same key slot# Log.info(rc.mget(key1, key2))# # redis.exceptions.RedisClusterException: MGET - all keys must map to the same key slotLog.info(rc.delete(cluster_test_str, key1, key2))Log.info('------------------')cluster_test_hash = 'test_hash'Log.info(rc.hset(cluster_test_hash, mapping={'hash_key': 'hash_value', 'hash_key1': 'hash_value1','hash_key2': 'hash_value2', 'hash_key3': 'hash_value3'}))Log.info(rc.cluster_keyslot(cluster_test_hash))Log.info(rc.exists(cluster_test_hash))Log.info(rc.type(cluster_test_hash))Log.info(rc.ttl(cluster_test_hash))Log.info(rc.expire(cluster_test_hash, 60 * 60))Log.info(rc.ttl(cluster_test_hash))Log.info(rc.hget(cluster_test_hash, 'hash_key'))Log.info(rc.hget(cluster_test_hash, 'hash_key2222'))Log.info(rc.hgetall(cluster_test_hash))Log.info(rc.hexists(cluster_test_hash, 'hash_key'))Log.info(rc.hexists(cluster_test_hash, 'hash_key2222'))Log.info(rc.hkeys(cluster_test_hash))Log.info(rc.hvals(cluster_test_hash))Log.info(rc.hlen(cluster_test_hash))Log.info(rc.hdel(cluster_test_hash, 'hash_key2222', 'hash_key'))Log.info(rc.hlen(cluster_test_hash))Log.info(rc.hmget(cluster_test_hash, 'hash_key', 'hash_key2'))Log.info(rc.hmset(cluster_test_hash, {'test': 'test1', 'test2': 'test3'}))Log.info(rc.hgetall(cluster_test_hash))Log.info(rc.delete(cluster_test_hash))Log.info('------------------')cluster_test_list = 'test_list'Log.info(rc.rpush(cluster_test_list, 'list1', 'list2', 'list3'))Log.info(rc.cluster_keyslot(cluster_test_list))Log.info(rc.type(cluster_test_list))Log.info(rc.exists(cluster_test_list))Log.info(rc.ttl(cluster_test_list))Log.info(rc.expire(cluster_test_list, 5 * 60 * 60))Log.info(rc.ttl(cluster_test_list))Log.info(rc.lindex(cluster_test_list, 1))Log.info(rc.llen(cluster_test_list))Log.info(rc.lrange(cluster_test_list, 0, -1))Log.info(rc.lpush(cluster_test_list, 'list0'))Log.info(rc.lrange(cluster_test_list, 0, -1))Log.info(rc.linsert(cluster_test_list, 'BEFORE', 'list0', 'list0000'))Log.info(rc.lrange(cluster_test_list, 0, -1))Log.info(rc.linsert(cluster_test_list, 'AFTER', 'list3', 'list4'))Log.info(rc.lrange(cluster_test_list, 0, -1))Log.info(rc.lpop(cluster_test_list))Log.info(rc.lrange(cluster_test_list, 0, -1))Log.info(rc.rpop(cluster_test_list))Log.info(rc.lrange(cluster_test_list, 0, -1))Log.info(rc.lrem(cluster_test_list, 1, 'list0'))Log.info(rc.lrange(cluster_test_list, 0, -1))Log.info(rc.lset(cluster_test_list, 0, 't_list'))Log.info(rc.lrange(cluster_test_list, 0, -1))Log.info(rc.delete(cluster_test_list))Log.info('------------------')cluster_test_set = 'test_set'Log.info(rc.sadd(cluster_test_set, 'set1', 'set2', 'set3', 'set3', 'set3'))Log.info(rc.cluster_keyslot(cluster_test_set))Log.info(rc.type(cluster_test_set))Log.info(rc.exists(cluster_test_set))Log.info(rc.ttl(cluster_test_set))Log.info(rc.expire(cluster_test_set, 4 * 60 * 60))Log.info(rc.ttl(cluster_test_set))Log.info(rc.scard(cluster_test_set))Log.info(rc.smembers(cluster_test_set))Log.info(rc.sismember(cluster_test_set, 'set0'))Log.info(rc.sismember(cluster_test_set, 'set2'))Log.info(rc.srem(cluster_test_set, 'set1'))Log.info(rc.smembers(cluster_test_set))Log.info(rc.delete(cluster_test_set))rc.close()
"""
@blog: https://blog.csdn.net/zyooooxie
"""def test_rp2():from redis.cluster import RedisClusterhost2 = 'host2'port = 6666pwd = 'pwd'rc = RedisCluster(host=host2, port=port, password=pwd, decode_responses=True)Log.info(rc)rc.close()

代码:redis-py-cluster

我在使用 redis-py-cluster 2.1.3。

ERROR: redis-py-cluster 2.1.3 has requirement redis<4.0.0, >=3.0.0

"""
@blog: https://blog.csdn.net/zyooooxie
"""# Redis Cluster中有一个16384长度的槽的概念,他们的编号为0、1、2、3 …… 16382、16383。这个槽是一个虚拟的槽,并不是真正存在的。
# 正常工作的时候,Redis Cluster中的每个Master节点都会负责一部分的槽,当有某个key被映射到某个Master负责的槽,那么这个Master负责为这个key提供服务。# 对于cluster而言,不同的key 往往 由不同的node处理。# 集群使用公式 CRC16(key) % 16384来计算键key属于哪个槽。def test_rpc0():"""redis-py-cluster:return:"""from rediscluster import RedisClusterrc = RedisCluster(startup_nodes=[{'host': host1, 'port': port}, {'host': host2, 'port': port}],decode_responses=True, password=pwd)print('{}'.format(rc))# print('{}----{}'.format(type(rc), rc.__dict__))print(rc.cluster_info())                        # 集群的信息# 每个节点在集群中都有一个独一无二的 ID , 该 ID 是一个十六进制表示的 160 位随机数, 在节点第一次启动时由 /dev/urandom 生成。# print(rc.cluster_nodes())                       # 集群当前已知的所有节点(node),以及这些节点的相关信息print(rc.cluster_slots())                       # 查看slot和节点的对应关系mec_key = 'mec_key'mec_key_slot = rc.cluster_keyslot(mec_key)print(mec_key_slot)                              # 计算key 应该被放置在哪个槽上print(rc.exists(mec_key), '存在的key-ttl是1小时', rc.ttl(mec_key))slot_len = rc.cluster_countkeysinslot(mec_key_slot)print(slot_len)                                 # 返回 槽slot_id 目前包含的键值对数量print(rc.cluster_get_keys_in_slot(mec_key_slot, 5))    # 返回 num_keys个 槽slot 的键print(rc.cluster_get_keys_in_slot(mec_key_slot, slot_len))rc.close()
"""
@blog: https://blog.csdn.net/zyooooxie
"""def test_rpc1():"""redis-py-cluster:return:"""from rediscluster import ClusterConnectionPoolfrom rediscluster import RedisClusterccp = ClusterConnectionPool(startup_nodes=[{'host': host1, 'port': port},{'host': host2, 'port': port}],decode_responses=True, password=pwd)rc = RedisCluster(connection_pool=ccp)print('{}'.format(rc))print('{}----{}'.format(type(rc), rc.__dict__))print(f'{rc.cluster_info()}')print(f'{rc.cluster_nodes()}')print(f'{rc.cluster_slots()}')rc.close()

个人常用环境是使用redis-py;虚拟环境装的redis-py-cluster。

本文链接:https://blog.csdn.net/zyooooxie/article/details/123760358

交流技术 欢迎+QQ 153132336 zy
个人博客 https://blog.csdn.net/zyooooxie

Python脚本之操作Redis Cluster相关推荐

  1. python操作redis实例_Java,php,Python连接并操作redis实例

    1.Java连接并操作redis 在Eclipse里新建一个java project,导入jedis-*.jar包. 示例代码,其他对应的操作类型见:http://my.oschina.net/u/2 ...

  2. python使用redis_Python操作redis系列之 列表(list) (五)

    # -*- coding: utf-8 -*-import redis r=redis.Redis(host="123.156.74.190",port=6379,password ...

  3. 安卓手机运行python脚本自动操作app_使用python控制Android手机自动操作WiFi开关

    最新公司上线的app,开发童鞋从后台看到android版本会有一定的概率在操作app时,网络断开再连接的时候会出现闪退问题.开发针对问题修改了下代码,需要我们再验证下,但通过手机的通知栏关闭和打开Wi ...

  4. 华为python673集_实现Redis Cluster并实现Python链接集群

    一.Redis Cluster简单介绍 Redis集群搭建的方式有多种,例如使用Redis主从复制+Sentinel高可用集群.zookeeper等,但从Redis 3.0之后版本支持Redis-cl ...

  5. python连接redis集群如何释放内存_python 连接 redis cluster 集群

    一. redis集群模式有多种, cluster模式只是其中的一种实现方式, 其原理请自行谷歌或者百度, 这里只举例如何使用Python操作 redis cluster 集群 二. python 连接 ...

  6. Redis进阶-Jedis以及Spring Boot操作 Redis 5.x Cluster

    文章目录 Pre Jedis操作Redis Cluster 添加依赖 Code Spring Boot 操作Redis Cluster 引入 依赖 application.yml Code Pre R ...

  7. redis映射的概念_搭建分布式Redis Cluster集群与Redis入门

    目录 Redis 集群搭建Redis 是啥集群(Cluster)Redis Cluster 说明Redis Cluster 节点Redis Cluster 集群模式不能保证一致性创建和使用 Redis ...

  8. Redis Cluster 实战 - 图解 - 秒懂 - 史上最全

    文章很长,而且持续更新,建议收藏起来,慢慢读! Java 高并发 发烧友社群:疯狂创客圈(总入口) 奉上以下珍贵的学习资源: 免费赠送 经典图书 : 极致经典 + 社群大片好评 < Java 高 ...

  9. 如何开发一个高性能的redis cluster proxy

    背景 redis cluster简介 Redis cluster是redis官方提供集群方案,设计上采用非中心化的架构,节点之间通过gossip协议交换互相的状态,redis cluster使用数据分 ...

最新文章

  1. vi/vim: 使用taglist插件
  2. thymeleaf加载不了js引用_web前端教程之js中的模块化一
  3. Python面对对象编程——公有与私有
  4. 084_html5WebWorkers
  5. 微脉java面试,微脉医疗开放平台
  6. es6 --- promise和async/await的区别
  7. ActiveMQ性能测试
  8. jQuery表单验证的几种方法
  9. Qt文档阅读笔记-The Meta-Object System解析及实例
  10. 【CSS】解决图片和盒子底部产生的缝隙问题
  11. python两个 list 获取交集,并集,差集的方法
  12. matlab对文件夹的遍历
  13. 组态王与DLT645-2007电能表通讯调试总结
  14. Raid0/raid1/raid5磁盘阵列数据恢复思路
  15. 大量精品国学论文免费下载
  16. 报错 Missing number, treated as zero. \begin{subfigure}{0.24\linewidth}?怎么解决
  17. 棋盘格相机标定图片拍摄方法
  18. 真正解决Word中表格首行字母或首列字母(首字母)大写的问题
  19. 实验十五 Java Swing 图形用户界面程序设计应用
  20. Led智慧照明系统功能

热门文章

  1. Mysql + Mybatis批量更新报错 BadSqlGrammarException
  2. jquery html() ie兼容,ie是否支持jquery
  3. recovery相关的FAQ总结
  4. Java 生成随机数
  5. Elasticsearch语法(聚合)
  6. 51单片机的c语言外部扩展,51单片机资源扩展:扩展片外RAM
  7. 从程序员到架构师需要掌握哪些技能
  8. 确保人生悲惨的七大秘诀
  9. cesium之3D tiles格式介绍
  10. python实现登录购物_python+selenium实现经京东登录+购物+支付