1.安装redis和redis-py-cluster
import redis
from rediscluster import RedisCluster
2.建立连接
startup_nodes = [
{"host": 'x.x.x.x', "port": 7000},
{"host": 'x.x.x.x', "port": 7001},
{"host": 'x.x.x.x', "port": 7002},
{"host": 'x.x.x.x', "port": 7003},
{"host": 'x.x.x.x', "port": 7004},
{"host": 'x.x.x.x', "port": 7005},
]
redis_client = RedisCluster(startup_nodes=startup_nodes, decode_responses=True)
3.获取指定key的value
key_str = 'realStat@'+str(targetPointId)
hash_values = redis_client.hgetall(key_str)
4.获取所有key
def scan_all_slots(rc):
"""遍历所有槽位并扫描其中的key"""
all_keys = []
for slot in range(0, 16384):
keys_in_slot = rc.execute_command('CLUSTER', 'GETKEYSINSLOT', slot, 100)
all_keys.extend(keys_in_slot)
return all_keys