阿里云Redis集群有多个节点,用户需要查看每个子节点具体有哪些key。使用keys命令容易导致业务受影响,而scan命令由于集群版的架构问题无法很好的支持。目前阿里云Redis提供了iscan命令用于查看某个节点的key信息。
如果只是想查看每个子实例总的内存和key数目情况可见:阿里云Redis集群子实例内存和key数目查看
安装python客户端
下载python客户端
wget “https://pypi.python.org/packages/68/44/5efe9e98ad83ef5b742ce62a15bea609ed5a0d1caf35b79257ddb324031a/redis-2.10.5.tar.gz#md5=3b26c2b9703b4b56b30a1ad508e31083”
解压安装
tar -xvf redis-2.10.5.tar.gz
cd redis-2.10.5
sudo python setup.py install
命令介绍
# idx为节点的id,从0开始,16到64gb的集群实例为8个节点故idx为0到7,128g 256gb的为16个节点
# 其余参数与官方scan命令含义一致
ISCAN idx cursor [MATCH pattern] [COUNT count]
扫描脚本
import sys
import redis
import time
if __name__ == '__main__':
if len(sys.argv) != 4:
print 'Usage: python ', sys.argv[0], ' host port password '
exit(1)
db_host = sys.argv[1]
db_port = sys.argv[2]
db_password = sys.argv[3]
r = redis.StrictRedis(host=db_host, port=int(db_port), password=db_password)
nodecount = r.info()['nodecount']
for node in range(0, nodecount):
print "============ node ", str(node), " ================"
cursor = -1
loop = 0
while cursor != 0:
if cursor == -1:
cursor = 0
if loop > 10000:
time.sleep(0.1)
loop = 0
loop = loop + 1
info = r.execute_command("iscan", str(node), str(cursor))
cursor = info[0]
key_list = info[1]
for key in key_list:
print key
执行脚本
python check_sharding_db host port password > keys.txt
tail -f keys.txt可以看到其中出现有如下内容:
============ node 0 ================
具体的key1
具体的key2
...
============ node 1 =================
...