Databases Reference
In-Depth Information
elegant code. It doesn't take care of exceptions or work fast with extremely large fi les, but it works
for the purpose at hand.
LISTING 6-1: movielens_dataloader.rb
Available for
download on
Wrox.com
require 'rubygems' #can skip this line in Ruby 1.9
require 'mongo'
field_map = {
“users” => %w(_id gender age occupation zip_code),
“movies” => %w(_id title genres),
“ratings” => %w(user_id movie_id rating timestamp)
}
db = Mongo::Connection.new.db(“mydb”)
collection_map = {
“users” => db.collection(“users”),
“movies” => db.collection(“movies”),
“ratings” => db.collection(“ratings”)
}
unless ARGV.length == 1
puts “Usage: movielens_dataloader data_filename”
exit(0)
end
class Array
def to_h(key_definition)
result_hash = Hash.new()
counter = 0
key_definition.each do |definition|
if not self[counter] == nil then
if self[counter].is_a? Array or self[counter].is_a? Integer then
result_hash[definition] = self[counter]
else
result_hash[definition] = self[counter].strip
end
else
# Insert the key definition with an empty value.
# Because we probably still want the hash to contain the key.
result_hash[definition] = “”
end
# For some reason counter.next didn't work here....
counter = counter + 1
end
return result_hash
end
end
if File.exists?(ARGV[0])
file = File.open(ARGV[0], 'r')
data_set = ARGV[0].chomp.split(“.”)[0]
Search WWH ::




Custom Search