Skip to content

Instantly share code, notes, and snippets.

@RobinStamer
Created May 18, 2011 19:35
Show Gist options
  • Select an option

  • Save RobinStamer/979353 to your computer and use it in GitHub Desktop.

Select an option

Save RobinStamer/979353 to your computer and use it in GitHub Desktop.
Single M/R pass example
genoce:PRIMARY> m
function () {
emit(this.from + ":" + this.to, {amount:this.amount});
}
genoce:PRIMARY> r
function (k, values) {
var r = {amount:0};
values.forEach(function (v) {r.amount += v.amount;});
return r;
}
genoce:PRIMARY> f
function (r) {
var res = {};
for (var i = 0; i < r.length; ++i) {
var k = r[i]._id, rk = k.split(":").reverse().join(":"), v = r[i].value.amount;
res[k] = res[k] || 0;
res[k] += v;
res[rk] = res[rk] || 0;
res[rk] -= v;
}
return res;
}
genoce:PRIMARY> db.mr.find()
{ "_id" : ObjectId("4dd41a3f8ed84dc8d2239d15"), "from" : 1234, "to" : 4321, "amount" : 2 }
{ "_id" : ObjectId("4dd41a418ed84dc8d2239d16"), "from" : 1234, "to" : 4321, "amount" : 2 }
{ "_id" : ObjectId("4dd41a4c8ed84dc8d2239d17"), "from" : 4321, "to" : 1234, "amount" : 2 }
genoce:PRIMARY> f(db.runCommand({mapreduce: 'mr', map: m, reduce: r, out: { inline : 1}}).results)
{ "1234:4321" : 2, "4321:1234" : -2 }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment