Skip to content

Instantly share code, notes, and snippets.

@skayikci
Created April 3, 2014 13:37
Show Gist options
  • Save skayikci/9954508 to your computer and use it in GitHub Desktop.
Save skayikci/9954508 to your computer and use it in GitHub Desktop.
(mysql2 - redis - ruby combo) for inserting bulk data into a redis list
require 'mysql2'
begin
# you should fill those empty places inorder to read from the database
db = Mysql2::Client.new(:host => "localhost", :username => "", :password => "", :database => "")
# update table_name with the table name you created
@results = db.query("SELECT * FROM table_name")
command ||= ""
# a simple error handling message
rescue Mysql2::Error
puts "Oh noes! We could not connect to our database. -_-;;"
exit 1
end
# a little imitation of the code from http://redis.io/topics/mass-insert
def gen_redis_proto(*cmd)
proto = ""
proto << "*"+cmd.length.to_s+"\r\n"
cmd.each{|arg|
proto << "$"+arg.to_s.bytesize.to_s+"\r\n"
proto << arg.to_s+"\r\n"
}
proto
end
# change ListName with whatever you want
# change RowName with whatever row you want to use for pushing
records = @results.map do |row|
STDOUT.write(gen_redis_proto("LPUSH","ListName",row['RowName']))
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment