Python连接redis-cluster插入1000万条数据详细脚本(二种写法)
1.结合上文提到的,在离线情况下搭建python连接rediscluster所需安装包及环境后,脚本如下:
#!/usr/bin/env Python3
import redis
import time
from time import ctime,sleep
from rediscluster import StrictRedisCluster
startup_nodes = [
{“host”:“10.1.23.151”, “port”:6379}, # 主
{“host”:“10.1.23.217”, “port”:6379}, # 主
{“host”:“10.1.23.227”, “port”:6379}, # 主
{“host”:“10.1.23.228”, “port”:6379},
{“host”:“10.1.23.230”, “port”:6379},
{“host”:“10.1.23.53”, “port”:6379}
]
redis_conn = StrictRedisCluster(startup_nodes=startup_nodes,decode_responses=True, password=‘cib’)
#redis_conn = StrictRedis(startup_nodes=startup_nodes, decode_responses=True)
for i in range(0, 5000000):
# try:
# key = redis_conn.set(‘key-test’+str(i),str(i).zfill(6))
# value = redis_conn.set(‘redis-test-value’+str(i),str(i).zfill(6))
# print(key,value)
# except:
# print(“connect to redis cluster error”)
# #time.sleep(2)
# key = redis_conn.set(‘key-test’+str(i),str(i))
try:
#第一种
redis_conn.set(‘key-test’+str(i),str(i).zfill(6))
redis_conn.set(‘redis-test-value’+str(i),str(i).zfill(6))
print(“key:”,redis_conn.get(“key-test”))
print(“value”,redis_conn.get(“redis-test-value”))
#第二种
# key = str(i).zfill(8)
# value = str(i).zfill(8)
# redis_conn.set(“key-test”+key,“redis-test-value”+value)
# print(“key:”,“key-test”+key)
# print(“value:”,redis_conn.get(“key-test”+key))
except:
print(“connect to redis cluster error”)
2.附带手动连接redis cmd:
查看redis cluster主从服务器节点:
redis-cli --cluster check 10.1.23.53:6379 -a cib
手动命令连接redis
-
redis-cli -h 192.168.0.12 -p 6379 -a 123456
-
或者find的一下src×
cd /root/redis-5.0.3/src
./redis-cli -h 192.168.0.12 -a cib
192.168.0.12:6379> keys *
(empty list or set)
192.168.0.12:6379> keys *
- “key-test000000001”
- “key-test000000002”
- “key-test000000003”
… - “key-test000000004”
…
#清空所有数据
192.168.0.12:6379> flushall
另外: 如果类似于redis集群报错:(error) MOVED 11469 192.168.163.249:7002
应该是你没有启动集群模式(即缺少了那个"-c"):
redis-cli -c -h 192.168.0.12 -p 6379 -a 123456