importredisfromredisclusterimportRedisCluster#client redis.StrictRedis(host172.31.24.54, port7001,password)clientRedisCluster(host172.31.25.54,port7001,password,skip_full_coverage_checkTrue)# 用于存储所有键和重复键的集合all_keysset()duplicatesset()keysclient.scan_iter(matchAUTH*,count100)forkeyinkeys:# 检查是否已经存在于集合中ifkeyinall_keys:duplicates.add(key)else:all_keys.add(key)#client.delete(key)print(ftype{client.type(key)})ifclient.type(key).decode(utf-8)string:print(fKey:{key}, \n Value:{client.get(key)})elifclient.type(key).decode(utf-8)hash:print(fKey:{key}, \n Value:{client.hgetall(key)})elifclient.type(key).decode(utf-8)list:print(fKey:{key}, \n Value:{client.lrange(key,0,-1)})elifclient.type(key).decode(utf-8)set:print(fKey:{key}, \n Value:{client.smembers(key)})elifclient.type(key).decode(utf-8)zset:print(fKey:{key}, \n Value:{client.zrange(key,0,-1)})# 输出结果ifduplicates:print(Duplicate keys found:)forkeyinduplicates:print(key.decode(utf-8))# keys are returned as bytes, so decode themelse:print(No duplicates found.)print(len(all_keys))importredisfromredis.clusterimportRedisClusterdeffind_big_keys_in_cluster():# 1. 初始化集群连接try:rcRedisCluster(host192.168.0.1,port7001,passwordxxx,skip_full_coverage_checkTrue)print( 成功连接到 Redis 集群\n)exceptExceptionase:print(f❌ 集群连接失败:{e})returnthreshold_bytes1*1024*1024#1 mb的keyfound_count0# 2. 获取所有主节点列表primariesrc.get_primaries()fornodeinprimaries:node_hostnode.host node_portnode.portprint(f--- 正在扫描主节点:{node_host}:{node_port}---)# 3. 单独连接到该主节点进行 SCAN 遍历try:node_clientredis.StrictRedis(hostnode_host,portnode_port,passwordxxx)cursor0whileTrue:# 每次迭代扫描 1000 个 key避免阻塞 Redis 主线程cursor,keysnode_client.scan(cursorcursor,count1000)forkeyinkeys:try:# 4. 核心使用 MEMORY USAGE 获取真实的内存占用字节数memory_usagenode_client.execute_command(MEMORY USAGE,key)ifmemory_usageandmemory_usagethreshold_bytes:size_mbround(memory_usage/1024/1024,4)key_typenode_client.type(key)print(f 发现大 Key: [{key}] | 类型:{key_type}| 大小:{size_mb}MB)found_count1exceptException:# 忽略在扫描过程中刚好被删除的 keypass# cursor 归零代表当前节点遍历完成ifcursor0:breakexceptExceptionase:print(f ⚠️ 扫描节点{node_host}:{node_port}时出错:{e})print(f--- 节点{node_host}:{node_port}扫描完毕 ---\n)print(f✅ 全部扫描完成共发现{found_count}个大 Key。)if__name____main__:find_big_keys_in_cluster()
python查询redis
发布时间:2026/6/12 6:58:17
importredisfromredisclusterimportRedisCluster#client redis.StrictRedis(host172.31.24.54, port7001,password)clientRedisCluster(host172.31.25.54,port7001,password,skip_full_coverage_checkTrue)# 用于存储所有键和重复键的集合all_keysset()duplicatesset()keysclient.scan_iter(matchAUTH*,count100)forkeyinkeys:# 检查是否已经存在于集合中ifkeyinall_keys:duplicates.add(key)else:all_keys.add(key)#client.delete(key)print(ftype{client.type(key)})ifclient.type(key).decode(utf-8)string:print(fKey:{key}, \n Value:{client.get(key)})elifclient.type(key).decode(utf-8)hash:print(fKey:{key}, \n Value:{client.hgetall(key)})elifclient.type(key).decode(utf-8)list:print(fKey:{key}, \n Value:{client.lrange(key,0,-1)})elifclient.type(key).decode(utf-8)set:print(fKey:{key}, \n Value:{client.smembers(key)})elifclient.type(key).decode(utf-8)zset:print(fKey:{key}, \n Value:{client.zrange(key,0,-1)})# 输出结果ifduplicates:print(Duplicate keys found:)forkeyinduplicates:print(key.decode(utf-8))# keys are returned as bytes, so decode themelse:print(No duplicates found.)print(len(all_keys))importredisfromredis.clusterimportRedisClusterdeffind_big_keys_in_cluster():# 1. 初始化集群连接try:rcRedisCluster(host192.168.0.1,port7001,passwordxxx,skip_full_coverage_checkTrue)print( 成功连接到 Redis 集群\n)exceptExceptionase:print(f❌ 集群连接失败:{e})returnthreshold_bytes1*1024*1024#1 mb的keyfound_count0# 2. 获取所有主节点列表primariesrc.get_primaries()fornodeinprimaries:node_hostnode.host node_portnode.portprint(f--- 正在扫描主节点:{node_host}:{node_port}---)# 3. 单独连接到该主节点进行 SCAN 遍历try:node_clientredis.StrictRedis(hostnode_host,portnode_port,passwordxxx)cursor0whileTrue:# 每次迭代扫描 1000 个 key避免阻塞 Redis 主线程cursor,keysnode_client.scan(cursorcursor,count1000)forkeyinkeys:try:# 4. 核心使用 MEMORY USAGE 获取真实的内存占用字节数memory_usagenode_client.execute_command(MEMORY USAGE,key)ifmemory_usageandmemory_usagethreshold_bytes:size_mbround(memory_usage/1024/1024,4)key_typenode_client.type(key)print(f 发现大 Key: [{key}] | 类型:{key_type}| 大小:{size_mb}MB)found_count1exceptException:# 忽略在扫描过程中刚好被删除的 keypass# cursor 归零代表当前节点遍历完成ifcursor0:breakexceptExceptionase:print(f ⚠️ 扫描节点{node_host}:{node_port}时出错:{e})print(f--- 节点{node_host}:{node_port}扫描完毕 ---\n)print(f✅ 全部扫描完成共发现{found_count}个大 Key。)if__name____main__:find_big_keys_in_cluster()