mapreduce - Calculating pagerank in mongodb -
i'm trying run pagerank using mapreduce in mongodb.
my documents in format:
{ "_id" : "u: 10000", "value" : [ [ "u: 10000", "s: 985272", 1 ], [ "s: 985272", "u: 10000", 1 ], [ "u: 10000", "s: 303770", 1 ], [ "s: 303770", "u: 10000", 1 ] ] }
now think first step collect links key. have several outbound links per document. (these happen bidirectional).
here map , reduce functions:
m = function () { (var = 0; < this.value.length; i++){ var out = {}; out.out = this.value[i][1]; out.weight = this.value[i][2]; emit(this.value[i][0], [out]); } } r = function(key, values){ var result = { value: [] }; values.foreach(function(val) { result.value.push({out: val.out, weight: val.weight}); }); return result; }
the problem i'm not sure emit producing multiple emissions per document. results like:
{ "_id" : "s: 1000082", "value" : [ { "out" : "u: 37317", "weight" : 1 } ] }
when expect multiple items per document.
anyone have ideas? appreciated!
edit:
i'm not satisfied, example how things work?. reduce result doesn't @ emit output.
the issue not mapping array reduce trying push array.
if want have each key map array of "out" , "weight" pairs, need emit array containing that, , in reduce need concat arrays together.
that means when map emits (key, value) structure of "value" must identical structure of reduce function returns result.
if change map function this, value document field "value" array of documents each having field "out" , field "weight":
function () { (var = 0; < this.value.length; i++) { key = this.value[i][0]; value = {value:[{out:this.value[i][1], weight:this.value[i][2]}]}; emit(key, value); } }
and reduce function this, constructs result have identical structure value emit above (since concatenates gets passed in each key):
function (key, values) { result = {value:[]}; (var in values) { result.value = values[i].value.concat(result.value); } return result; }
you expecting back.
{ "_id" : "s: 303770", "value" : { "value" : [ { "out" : "u: 10000", "weight" : 1 } ] } } { "_id" : "s: 985272", "value" : { "value" : [ { "out" : "u: 10000", "weight" : 1 } ] } } { "_id" : "u: 10000", "value" : { "value" : [ { "out" : "s: 303770", "weight" : 1 }, { "out" : "s: 985272", "weight" : 1 } ] } }
Comments
Post a Comment