[Redis] use python to operate redis cluster
https://blog.csdn.net/bitcarmanlee/article/details/51852126
Passwords cannot be written to the list:
redisconn = strictRedisCluster(startup_nodes=redis_nodes, password=password)
Combined with the previous article, improve the script operation 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 PM 4:34
func: Get key without ttl set in database
"""
import redis
import argparse
import time
import sys
import rediscluster
class ShowProcess:
"""
Class showing processing progress
The display of processing progress can be realized by calling the related functions of this class
"""
i = 0 #Current processing progress
max_steps = 0 #Total number of times to process
max_arrow = 50 #Length of progress bar
#Initialize the function, you need to know the total number of processing times
def __init__(self, max_steps):
self.max_steps = max_steps
self.i = 0
#Show function, show progress according to current processing progress i
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
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)) #Calculate how many '>' are displayed
num_line = self.max_arrow - num_arrow #Calculate how many '-' are displayed
percent = self.i * 100.0 / self.max_steps #Calculate completion progress in xx.xx%
process_bar = '[' + '>' * num_arrow + ' ' * num_line + ']'+ '%.2f' % percent + '%' + '\r' #string with output,'\r' means no line break back to the far left
sys.stdout.write(process_bar) #Print these two sentences to the terminal
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 is distributed as follows:",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