Created
April 2, 2015 11:22
-
-
Save YarekTyshchenko/df8c5c4f4bf54660e9ef to your computer and use it in GitHub Desktop.
Poor man's Hadoop in Javascript
This file contains hidden or 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
var reduce = function(k, list) { | |
return [k, list]; | |
}; | |
var map = function(yield, item) { | |
yield(item[0], item[1]); | |
}; | |
var PoorMansHadoop = function(map, reduce, data) { | |
var r = data.map(function(node) { | |
var mapperOutput = []; | |
var yield = function() { | |
return function(k, v) { | |
mapperOutput.push([k, v]); | |
} | |
}(); | |
map(yield, node); | |
return mapperOutput; | |
}).reduce(function(list, nodeList) { | |
nodeList.forEach(function(node) { | |
if (list[node[0]]) { | |
list[node[0]].push(node[1]); | |
} else { | |
list[node[0]] = [node[1]]; | |
} | |
}); | |
return list; | |
}, {}); | |
var output = []; | |
for (var key in r) { | |
if (r.hasOwnProperty(key)) { | |
output.push(reduce(key, r[key])); | |
} | |
} | |
return output; | |
} | |
// Example | |
var result = PoorMansHadoop(function(y, i) { | |
y(i, 1); | |
}, function(k, list) { | |
return { | |
key:k, | |
value:list.reduce(function(a, b){ | |
return a + b; | |
}, 0) | |
}; | |
}, ['foo', 'bar', 'apple', 'foo', 'banana', 'foo', 'foo', 'bar']); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Careful there, yield is a reserved keyword.