当前位置:
凯发ag旗舰厅登录网址下载 >
编程语言
> python
>内容正文
python
使用python向redis批量导入数据 -凯发ag旗舰厅登录网址下载
凯发ag旗舰厅登录网址下载
收集整理的这篇文章主要介绍了
使用python向redis批量导入数据
小编觉得挺不错的,现在分享给大家,帮大家做个参考.
1.使用pipeline进行批量导入数据。包含先使用rpush插入数据,然后使用expire改动过期时间
class redis_handler(handler):def connect(self):#print self.host,self.port,self.tableself.conn = connection(self.host,self.port,self.table) def execute(self, action_name):filename = "/tmp/temp.txt"batch_size = 10000with open(filename) as file:try:count = 0pipeline_redis = self.conn.client.pipeline()for lines in file:(key,value) = lines.split(',')count = count 1if len(key)>0:pipeline_redis.rpush(key,value.strip())if not count % batch_size:pipeline_redis.execute()count = 0#send the last batchpipeline_redis.execute()except exception:print 'redis add error'
class redis_handler(handler):def connect(self):#print self.host,self.port,self.tableself.conn = connection(self.host,self.port,self.table) def execute(self, action_name):filename = "/tmp/temp.txt"batch_size = 10000with open(filename) as file:try:count = 0pipeline_redis = self.conn.client.pipeline()for lines in file:(key,value) = lines.split(',')count = count 1if len(key)>0:pipeline_redis.rpush(key,value.strip())if not count % batch_size:pipeline_redis.execute()count = 0#send the last batchpipeline_redis.execute()except exception:print 'redis add error'
总结
以上是凯发ag旗舰厅登录网址下载为你收集整理的使用python向redis批量导入数据的全部内容,希望文章能够帮你解决所遇到的问题。
如果觉得凯发ag旗舰厅登录网址下载网站内容还不错,欢迎将凯发ag旗舰厅登录网址下载推荐给好友。
- 上一篇: 从几个sample来学习java堆,方法
- 下一篇: wpf学习开发客户端软件-任务助手(下