Created
June 20, 2012 08:58
-
-
Save p-baleine/2958913 to your computer and use it in GitHub Desktop.
mongoDB, MapReduce with calendar
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
## coding: utf-8 | |
# Demo mongoDB mapreduce with callendar | |
require 'date' | |
require 'mongo' | |
require 'securerandom' | |
require 'pp' | |
# Create calendar collection with random string name, | |
# return the generated collection name. | |
def prepare_callendar(db, from, to, init_val) | |
out_coll_name = 'callendar' + SecureRandom.hex(8) | |
(from.to_date..to.to_date).each do |day| | |
db[out_coll_name].insert('_id' => day.to_time, 'value' => init_val) | |
end | |
out_coll_name | |
end | |
include Mongo | |
db = Connection.new.db('callendar-demo') | |
# include js libraries | |
db.eval(File.read('underscore-min.js')) | |
db.eval(File.read('moment.js')) | |
coll = db['todo'] | |
# sample data | |
coll.save content: 'hoge', done: Time.local(2012, 6, 19), insert: Time.local(2012, 6, 17) | |
coll.save content: 'piyo', insert: Time.local(2012, 6, 19) | |
coll.save content: 'foo', done: Time.local(2012, 6, 21), insert: Time.local(2012, 6, 19) | |
map = <<EOS | |
function() { | |
var ts = moment(this.done).sod().toDate(); | |
emit(ts, { done_count : 1 }); | |
} | |
EOS | |
reduce = <<EOS | |
function(key, values) { | |
var sum = _.reduce(values, function(memo, v) { return memo + v.done_count; }, 0); | |
return { done_count : sum }; | |
} | |
EOS | |
from = Time.local(2012, 6, 17) | |
to = Time.local(2012, 6, 23) | |
# First create calendar collection | |
out_coll_name = prepare_callendar(db, from, to, 'done_count' => 0) | |
# then do MapReduce so the output is merged into callendar collection. | |
begin | |
coll.map_reduce(map, reduce, | |
query: { | |
'done' => { '$exists' => true }, | |
'insert' => { | |
'$gte' => from, | |
'$lt' => to | |
} | |
}, | |
out: { 'merge' => out_coll_name } | |
) | |
db[out_coll_name].find.sort_by{ |doc| doc['_id'] }.each{ |doc| pp doc } | |
rescue Exception => e | |
p e | |
ensure | |
# drop the tmporary collection | |
db[out_coll_name].drop() | |
end | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment