Skip to content

Instantly share code, notes, and snippets.

@daniel-roberts-10gen
Created May 8, 2012 14:35
Show Gist options
  • Save daniel-roberts-10gen/2635645 to your computer and use it in GitHub Desktop.
Save daniel-roberts-10gen/2635645 to your computer and use it in GitHub Desktop.
Sharded Map Reduce
use digg
//Map reduce functions
map = function() {
emit(this.user.name, {diggs: this.diggs, posts: 1});
}
reduce = function(key, values) {
var diggs = 0;
var posts = 0;
values.forEach(function(doc) {
diggs += doc.diggs;
posts += doc.posts;
});
return {diggs: diggs, posts: posts};
}
//Output in the same database to collection digger_users.
//Works from MongoD and MongoS
db.stories.mapReduce(map, reduce, {out: 'digg_users'});
//Output in different database and collection.
//Works from MongoD
//Does not work from MongoS - uncaught exception: map reduce failed:{ "ok" : 0, "errmsg" : "can't use out 'db' with non-sharded db" }
db.stories.mapReduce(map, reduce, {out : {replace : "collectionName", db : "otherDB"}});
//Enable sharding on the input database, specify sharded : true
//
sh.enableSharding('digg')
sh.shardCollection('digg.stories',{'_id':1});
db.stories.mapReduce(map, reduce, {out : {replace : "collectionName", db : "otherDB", sharded : true}});
//This one works
db.stories.mapReduce(map, reduce, {out : {replace : "otherDB.collectionName", sharded : true}});
sh.enableSharding('otherDB')
sh.shardCollection('otherDB.collectionName',{'_id':1});
db.sourcecollection.mapReduce(map, reduce, {out : {replace : "otherDB.collectionName", sharded : true}});
db.runCommand(
{ mapreduce : 'stories',
map : map,
reduce : reduce,
out : {replace : "otherDB.collectionName", sharded : true},
verbose : true
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment