zoukankan      html  css  js  c++  java
  • 单节点Redis使用 Python pipline大批量插入数据

    方法一:

    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+' ')
    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
  • 相关阅读:
    jquery operate
    ujs
    图标站
    rails foreign key
    feedback product from uservoice
    秒杀网
    short url
    rails nil blank
    paperclip imagemagic api &paperclip relevent
    类似优米网
  • 原文地址:https://www.cnblogs.com/EikiXu/p/9431007.html
Copyright © 2011-2022 走看看