亚洲激情专区-91九色丨porny丨老师-久久久久久久女国产乱让韩-国产精品午夜小视频观看

溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

【Redis】用python操作redis集群

發布時間:2020-08-11 08:59:37 來源:ITPUB博客 閱讀:571 作者:小亮520cl 欄目:關系型數據庫

https://blog.csdn.net/bitcarmanlee/article/details/51852126

 密碼不能寫到列表中去:

  1. 有密碼 就是這樣 redisconn = StrictRedisCluster(startup_nodes=redis_nodes,password=password)


結合上一篇文章,改進腳本后操作redis-cluster
  1. [root@ip-172-31-47-226 tmp]# more /root/get_nottl.py
  2. # encoding: utf-8
  3. """
  4. author: yangyi@youzan.com
  5. time: 2018/4/26 下午4:34
  6. func: 獲取數據庫中沒有設置ttl的 key
  7. """
  8. import redis
  9. import argparse
  10. import time
  11. import sys
  12. import rediscluster

  13. class ShowProcess:
  14.     """
  15.     顯示處理進度的類
  16.     調用該類相關函數即可實現處理進度的顯示
  17.     """
  18.     i = 0 # 當前的處理進度
  19.     max_steps = 0 # 總共需要處理的次數
  20.     max_arrow = 50 # 進度條的長度

  21.     # 初始化函數,需要知道總共的處理次數
  22.     def __init__(self, max_steps):
  23.         self.max_steps = max_steps
  24.         self.i = 0

  25.     # 顯示函數,根據當前的處理進度i顯示進度
  26.     # 效果為[>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>]100.00%
  27.     def show_process(self, i = None):
  28.         if i is not None:
  29.             self.i = i
  30.         else:
  31.             self.i += 1
  32.         num_arrow = int(int(self.i) * int(self.max_arrow) / int(self.max_steps)) # 計算顯示多少個'>'
  33.         num_line = self.max_arrow - num_arrow # 計算顯示多少個'-'
  34.         percent = self.i * 100.0 / self.max_steps # 計算完成進度,格式為xx.xx%
  35.         process_bar = '[' + '>' * num_arrow + ' ' * num_line + ']'+ '%.2f' % percent + '%' + '\r' # 帶輸出的字符串,'\r'表示不換行回到最左邊
  36.         sys.stdout.write(process_bar) # 這兩句打印字符到終端
  37.         sys.stdout.flush()

  38.     def close(self, words='done'):
  39.         print ''
  40.         print words
  41.         self.i = 0


  42. def check_ttl(redis_conn, no_ttl_file, dbindex):
  43.     start_time = time.time()
  44.     no_ttl_num = 0
  45.     allkey = redis_conn.dbsize()
  46.     keys_num = sum(list(set(allkey.values())))
  47.     print "key分布如下:",allkey
  48.     print "there are {num} keys in db {index} ".format(num=keys_num, index=dbindex)
  49.     process_bar = ShowProcess(keys_num)
  50.     with open(no_ttl_file, 'a') as f:

  51.         for key in redis_conn.scan_iter(count=10):
  52.             process_bar.show_process()
  53.             if redis_conn.ttl(key) == -1:
  54.                 no_ttl_num += 1
  55.                 if no_ttl_num < 10000:
  56.                     f.write(key+'\n')
  57.             else:
  58.                 continue

  59.     process_bar.close()
  60.     print "cost time(s):", time.time() - start_time
  61.     print "no ttl keys number:", no_ttl_num
  62.     print "we write keys with no ttl to the file: %s" % no_ttl_file


  63. def main():
  64.     parser = argparse.ArgumentParser()
  65.     parser.add_argument('-p', type=int, dest='port', action='store', help='port of redis ')
  66.     parser.add_argument('-a', type=str, dest='password', action='store', help='password of redis ')
  67.     parser.add_argument('-d', type=str, dest='db_list', action='store', default=0, help='ex : -d all / -d 1,2,3,4 ')
  68.     args = parser.parse_args()
  69.     port = args.port
  70.     pwd = args.password
  71.     if args.db_list == 'all':
  72.         db_list = [i for i in xrange(0, 16)]
  73.     else:
  74.         db_list = [int(i) for i in args.db_list.split(',')]

  75.     for index in db_list:
  76.         try:
  77.             startup_nodes = [{"host": "172.31.47.226", "port": port,"db":index}]
  78.             r = rediscluster.StrictRedisCluster(startup_nodes=startup_nodes, password=pwd)
  79.         except Exception, e:
  80.             print e
  81.         else:
  82.             no_ttl_keys_file = "/tmp/{port}_{db}_no_ttl_keys.txt".format(port=port, db=index)
  83.             check_ttl(r, no_ttl_keys_file, index)


  84. if __name__ == '__main__':
  85.     main()





向AI問一下細節
推薦閱讀:
  1. redis集群安裝
  2. redis集群

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

松原市| 祁阳县| 婺源县| 宣恩县| 阳山县| 漳浦县| 库伦旗| 和政县| 芜湖市| 潞西市| 阜南县| 洛南县| 昌平区| 奉新县| 栾城县| 芜湖县| 沂水县| 同德县| 延安市| 精河县| 临夏市| 文山县| 阿图什市| 鄂伦春自治旗| 曲水县| 阜阳市| 长白| 贵港市| 龙州县| 宁都县| 新巴尔虎右旗| 乐平市| 翼城县| 凤翔县| 乐清市| 巴马| 肥西县| 黄平县| 肃北| 连江县| 镇雄县|