【Redis】用python操作redis叢集
https://blog.csdn.net/bitcarmanlee/article/details/51852126
密碼不能寫到列表中去:
- 有密碼 就是這樣 redisconn = StrictRedisCluster(startup_nodes=redis_nodes,password=password)
結合上一篇文章,改進指令碼後操作redis-cluster
-
[root@ip-172-31-47-226 tmp]# more /root/get_nottl.py
-
# encoding: utf-8
-
"""
-
author: yangyi@youzan.com
-
time: 2018/4/26 下午4:34
-
func: 獲取資料庫中沒有設定ttl的 key
-
"""
-
import redis
-
import argparse
-
import time
-
import sys
-
import rediscluster
-
-
class ShowProcess:
-
"""
-
顯示處理進度的類
-
呼叫該類相關函式即可實現處理進度的顯示
-
"""
-
i = 0 # 當前的處理進度
-
max_steps = 0 # 總共需要處理的次數
-
max_arrow = 50 # 進度條的長度
-
-
# 初始化函式,需要知道總共的處理次數
-
def __init__(self, max_steps):
-
self.max_steps = max_steps
-
self.i = 0
-
-
# 顯示函式,根據當前的處理進度i顯示進度
-
# 效果為[>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>]100.00%
-
def show_process(self, i = None):
-
if i is not None:
-
self.i = i
-
else:
-
self.i += 1
-
num_arrow = int(int(self.i) * int(self.max_arrow) / int(self.max_steps)) # 計算顯示多少個'>'
-
num_line = self.max_arrow - num_arrow # 計算顯示多少個'-'
-
percent = self.i * 100.0 / self.max_steps # 計算完成進度,格式為xx.xx%
-
process_bar = '[' + '>' * num_arrow + ' ' * num_line + ']'+ '%.2f' % percent + '%' + '\r' # 帶輸出的字串,'\r'表示不換行回到最左邊
-
sys.stdout.write(process_bar) # 這兩句列印字元到終端
-
sys.stdout.flush()
-
-
def close(self, words='done'):
-
print ''
-
print words
-
self.i = 0
-
-
-
def check_ttl(redis_conn, no_ttl_file, dbindex):
-
start_time = time.time()
-
no_ttl_num = 0
-
allkey = redis_conn.dbsize()
-
keys_num = sum(list(set(allkey.values())))
-
print "key分佈如下:",allkey
-
print "there are {num} keys in db {index} ".format(num=keys_num, index=dbindex)
-
process_bar = ShowProcess(keys_num)
-
with open(no_ttl_file, 'a') as f:
-
-
for key in redis_conn.scan_iter(count=10):
-
process_bar.show_process()
-
if redis_conn.ttl(key) == -1:
-
no_ttl_num += 1
-
if no_ttl_num < 10000:
-
f.write(key+'\n')
-
else:
-
continue
-
-
process_bar.close()
-
print "cost time(s):", time.time() - start_time
-
print "no ttl keys number:", no_ttl_num
-
print "we write keys with no ttl to the file: %s" % no_ttl_file
-
-
-
def main():
-
parser = argparse.ArgumentParser()
-
parser.add_argument('-p', type=int, dest='port', action='store', help='port of redis ')
-
parser.add_argument('-a', type=str, dest='password', action='store', help='password of redis ')
-
parser.add_argument('-d', type=str, dest='db_list', action='store', default=0, help='ex : -d all / -d 1,2,3,4 ')
-
args = parser.parse_args()
-
port = args.port
-
pwd = args.password
-
if args.db_list == 'all':
-
db_list = [i for i in xrange(0, 16)]
-
else:
-
db_list = [int(i) for i in args.db_list.split(',')]
-
-
for index in db_list:
-
try:
-
startup_nodes = [{"host": "172.31.47.226", "port": port,"db":index}]
-
r = rediscluster.StrictRedisCluster(startup_nodes=startup_nodes, password=pwd)
-
except Exception, e:
-
print e
-
else:
-
no_ttl_keys_file = "/tmp/{port}_{db}_no_ttl_keys.txt".format(port=port, db=index)
-
check_ttl(r, no_ttl_keys_file, index)
-
-
-
if __name__ == '__main__':
- main()
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/29096438/viewspace-2153849/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- Jedis操作單節點redis,叢集及redisTemplate操作redis叢集(一)Redis
- redis與叢集實用操作筆記Redis筆記
- java操作Redis3.0.6叢集JavaRedisS3
- Redis系列:搭建Redis叢集(叢集模式)Redis模式
- java操作redis叢集連線池JavaRedis
- redis叢集Redis
- redis 叢集Redis
- Jedis使用連線池操作redis叢集Redis
- redis cluster 叢集故障恢復操作思路Redis
- 認識Redis叢集——Redis ClusterRedis
- Redis cluster 叢集Redis
- redis系列:叢集Redis
- Redis Cluster(叢集)Redis
- redis叢集原理Redis
- 搭建 Redis 叢集Redis
- redis叢集搭建Redis
- Redis操作及叢集搭建以及高可用配置Redis
- 【Redis叢集實戰】Redis Cluster 部署Redis
- Redis(5.0) 叢集搭建Redis
- 搭建Redis原生叢集Redis
- redis叢集指導Redis
- Redis--叢集搭建Redis
- docker-redis叢集DockerRedis
- redis叢集報錯Redis
- 搭建redis cluster叢集Redis
- redis叢集的搭建Redis
- redis 4.0.11 叢集搭建Redis
- redis 叢集構建Redis
- Docker搭建Redis叢集DockerRedis
- redis偽叢集配置Cluster叢集模式Redis模式
- 【Redis】Redis Cluster-叢集故障轉移Redis
- 深入淺出Redis-redis哨兵叢集Redis
- python 操作redisPythonRedis
- Python操作RedisPythonRedis
- redis 叢集 遇坑1Redis
- Redis 5.02官方叢集搭建Redis
- 玩轉Redis叢集(上)Redis
- 39_配置redis叢集Redis