阿里云 re
dis大key
搜索工具
https://yq.aliyun.com/articles/117042?spm=a2c4e.11153940.blogcont531067.15.fdaf45b5YA9Dvl
1、下载re
dis python客户端
```
wget "https://pypi.python.org/packages/68/44/5efe9e98ad83ef5b742ce62a15bea609ed5a0d1caf35b79257ddb324031a/re
dis-2.10.5.tar.gz#md5=3b26c2b9703b4b56b30a1ad508e31083”
tar -xvf re
dis-2.10.5.tar.gz
cd re
dis-2.10.5
sudo python setup.py install
```
2、
copy脚本 find_bigkey.py
```
import sys
import re
dis
def check_big_key(db,r, k):
bigKey = False
length = 0
try:
type = r.type(k)
if type == "string":
length = r.strlen(k)
elif type == "hash":
length = r.hlen(k)
elif type == "list":
length = r.llen(k)
elif type == "set":
length = r.scard(k)
elif type == "zset":
length = r.zcard(k)
except:
return
if length > 10240:
bigKey = True
if bigKey :
print db,k,type,length
def find_big_key_
normal(db_host, db_port, db_password, db_num):
r = re
dis.StrictRe
dis(host=db_host, port=db_port, password=db_password, db=db_num)
for k in r.scan_iter(count=1000):
check_big_key(db_num,r, k)
def find_big_key_sharding(db_host, db_port, db_password, db_num, nodecount):
r = re
dis.StrictRe
dis(host=db_host, port=db_port, password=db_password, db=db_num)
cursor = 0
for node in range(0, nodecount) :
while True:
iscan = r.execute_command("iscan",str(node), str(cursor), "count", "1000")
for k in iscan[1]:
check_big_key(db_num,r, k)
cursor = iscan[0]
print cursor, db, node, len(iscan[1])
if cursor == "0":
break;
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 = re
dis.StrictRe
dis(host=db_host, port=int(db_port), password=db_password)
print 'begin scanning big key in re
dis:', db_host,':',db_port
nodecount = 1
if 'nodecount' in r.info():
nodecount = r.inf()['nodecount']
keyspace_info = r.info("keyspace")
for db in keyspace_info:
print 'start check ', db, ' ', keyspace_info[db]
if nodecount > 1:
find_big_key_sharding(db_host, db_port, db_password, db.replace("db",""), nodecount)
else:
find_big_key_
normal(db_host, db_port, db_password, db.replace("db", ""))
else:
print 'start check ', 'db', ' ', 0
find_big_key_
normal(db_host, db_port, db_password, 0)
```
3、执行脚本
```
如: python find_bigkey.py 127.0.0.1 6375 123456
```
4、re
dis一般命令
```
./re
dis-cli -h 10.4.7.203 -p 6379 -a 111111
查询key :keys *
执行清理前查看(若不需要清理全部则清理指定key即可)
>keys * //查看所有key值
清理re
dis
>del key //
删除指定key
>Flushdb //
删除当前
数据库中的所有Key
>flushall //
删除所有
数据库中的key
```
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。