码迷,mamicode.com
首页 > 编程语言 > 详细

使用python向Redis批量导入数据

时间:2014-07-29 13:06:56      阅读:394      评论:0      收藏:0      [点我收藏+]

标签:redis   pipeline   大数据   

1.使用pipeline进行批量导入数据

class Redis_Handler(Handler):
	def connect(self):
		#print self.host,self.port,self.table
		self.conn = Connection(self.host,self.port,self.table)	
		
	def execute(self, action_name):
		filename = "/tmp/temp.txt"
		batch_size = 10000
		with open(filename) as file:
			try:
				count = 0
				pipeline_redis = self.conn.client.pipeline()
				for lines in file:
					(key,value) = lines.split(',')
						count = count + 1
						if len(key)>0:
							pipeline_redis.rpush(key,value.strip())
							if not count % batch_size:
								pipeline_redis.execute()
								count = 0
			
	
				#send the last batch
				pipeline_redis.execute()
			except Exception:
				print 'redis add error'

使用python向Redis批量导入数据,布布扣,bubuko.com

使用python向Redis批量导入数据

标签:redis   pipeline   大数据   

原文地址:http://blog.csdn.net/magicharvey/article/details/38237163

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!