【Redis】 redis-cluster刪除指定的key
需要從redis-cluster中刪除指定的key
迴圈每個主節點的版本:
-
[root@ip-172-31-39-42 ~]# more delkey-single.py
-
# encoding: utf-8
-
-
import redis
-
import random
-
import string
-
import rediscluster
-
import time
-
all_nodes=['172.31.39.42','172.31.36.10','172.31.47.157','172.31.36.49','172.31.40.169','172.31.32.234']
-
-
-
def del_keys_without_pipe(newhost):
-
pool = redis.ConnectionPool(host=newhost, port=6379, db=0,password='inH7HkD7iXoxKZCi')
-
r = redis.StrictRedis(connection_pool=pool)
-
start_time = time.time()
-
result_length = 0
-
for key in r.scan_iter(match='user:*:type:*:news:msg', count=100):
-
if r.ttl(key) == -1: 如果未設定過期時間
-
print key
-
# r.delete(key)
-
result_length += 1
-
print "normal ways end at:", time.time() - start_time
-
print newhost,"normal ways delete numbers:", result_length
-
for i in all_nodes:
- del_keys_without_pipe(i)
注意:迴圈連結只能去連結主節點,如果連結從節點會報以下的錯誤:
>>> for key in r.scan_iter(count=10):
... if r.ttl(key) == -1:
... print key
...
Traceback (most recent call last):
File "", line 2, in
File "/usr/lib/python2.7/site-packages/redis/client.py", line 1231, in ttl
return self.execute_command('TTL', name)
File "/usr/lib/python2.7/site-packages/redis/client.py", line 668, in execute_command
return self.parse_response(connection, command_name, **options)
File "/usr/lib/python2.7/site-packages/redis/client.py", line 680, in parse_response
response = connection.read_response()
File "/usr/lib/python2.7/site-packages/redis/connection.py", line 629, in read_response
raise response
redis.exceptions.ResponseError: MOVED 12752 172.31.32.234:6379
連結叢集版本
-
[root@ip-172-31-39-42 ~]# more delkey-cluster.py
-
# encoding: utf-8
-
-
import redis
-
import random
-
import string
-
import rediscluster
-
import time
-
-
-
def del_keys_without_pipe():
-
startup_nodes = [{"host": "172.31.39.42", "port": 6379,"db":0}]
-
r = rediscluster.StrictRedisCluster(startup_nodes=startup_nodes, password='inH7HkD7iXoxKZCi')
-
start_time = time.time()
-
result_length = 0
-
for key in r.scan_iter(match='user:*:type:*:news:msg', count=100):
-
# r.delete(key)
-
result_length += 1
-
print "normal ways end at:", time.time() - start_time
-
print "normal ways delete numbers:", result_length
-
- del_keys_without_pipe()
綜合以上兩種方式,還是使用連結cluster的方式吧,使用的模組也不一樣!
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/29096438/viewspace-2153950/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- Redis 實用小技巧——批次刪除指定的 keyRedis
- Redis 刪除1.2億指定字首的keyRedis
- Redis刪除大KeyRedis
- 批量刪除 redis keysRedis
- 【Redis】redis-cluster到redis-cluster的快速複製Redis
- Redis刪除特定字首key的優雅實現Redis
- 前端刪除多條資料,如何將多個被刪除項指定key傳給後臺前端
- redis-cluster心得Redis
- 字串-刪除指定字元字串字元
- Git刪除指定分支Git
- 刪除指定名稱的程式
- JavaScript 刪除指定的li元素JavaScript
- Git刪除指定檔案Git
- Git刪除指定commitGitMIT
- 陣列刪除指定項陣列
- jQuery刪除指定子元素jQuery
- jQuery刪除指定li元素jQuery
- Js刪除字串中的指定字串JS字串
- JavaScript刪除字串中的指定字元JavaScript字串字元
- 【Redis】redis遷移資料到redis-clusterRedis
- 【Redis】redis-cluster 安裝遇到的問題Redis
- 刪除指定目錄下指定字尾的檔案
- redis-cluster主從搭建Redis
- 優雅搭建redis-clusterRedis
- JavaScript 刪除陣列指定元素JavaScript陣列
- JavaScript刪除table表格指定行JavaScript
- MySQL批量刪除指定字首表MySql
- Array · 刪除陣列中指定的元素陣列
- 如何批量刪除指定的GitHub ReposGithub
- jQuery刪除具有指定文字的li元素jQuery
- js刪除陣列指定索引的元素JS陣列索引
- rman刪除指定時間的歸檔
- 【Redis】redis-cluster需要注意的幾個地方Redis
- jquery為指定的元素新增或者刪除指定樣式類jQuery
- 刪除元素的指定的class樣式類
- 【臨實戰】使用 Python 從 Redis 中刪除 4000W 個 KEYPythonRedis
- Linux下批量刪除空檔案或者刪除指定大小的檔案Linux
- js刪除陣列元素中的指定值JS陣列