单节点Redis使用 Python pipline大批量插入数据

时间:2024-07-08 21:03:32
方法一:

import redis

import time
filename = 'redis_resulit.txt'
def openPool():
    pool = redis.ConnectionPool(host='10.200.22.110', port=16379)
    redis.Connection
    return redis.Redis(connection_pool=pool)
def showResult(key,value,result):
    with open(filename,'a') as f:
        outmsg = "key%s value%s :%s" % (key,value,result)
        f.write(outmsg+'\n')
def main():
    r=openPool()
    #pipe = r.pipeline()
    pipe = r.pipeline(transaction=False) #集群模式下使用pipeline
    num=0
    start=time.time()
    with open('redis_commands.txt') as f:
        for line in f:
            num +=1
            pipe.set(line.split()[1],line.split()[2])
            if num%10000==0:
                print("num:",num)
                pipe.execute()
                print("执行时间:",round(time.time()-start,2))
                start=time.time()
    pipe.execute()
if __name__ == '__main__':
  main()

方法二:

vi  redis_commands.txt

SET Key0 Value0
SET Key1 Value1
SET Key2 Value2
SET Key3 Value3
SET Key4 Value4
SET Key5 Value5
SET Key6 Value6 
cat redis_commands.txt | redis-cli -h 192.168.127.130 -p 6379 [-a "password"] -n 0 --pipe