Simple MapReduce with Javascript
The best way to understand MapReduce is to actually see & feel it in action. Following is the simplest possible example I could cook… (Gist link)
-
-
var Job = {
-
-
data : [
-
"We are glad to see you here. This site is dedicated to",
-
"poetry and to the people who make poetry possible",
-
"poets and their readers. FamousPoetsAndPoems.com is",
-
"a free poetry site. On our site you can find a large",
-
"collection of poems and quotes from over 631 poets",
-
"Read and Enjoy Poetry",
-
"I, too, sing America",
-
"I am the darker brother",
-
"They send me to eat in the kitchen",
-
"When company comes",
-
"But I laugh",
-
"And eat well",
-
"And grow strong",
-
"Tomorrow",
-
"Ill be at the table",
-
"When company comes",
-
"Nobodyll dare",
-
"Say to me",
-
"Eat in the kitchen",
-
"Then",
-
"Besides",
-
"Theyll see how beautiful I am",
-
"And be ashamed",
-
"I, too, am America"
-
],
-
-
-
map : function(line) {
-
var splits = line.split(" ");
-
var temp = [];
-
for(var i=0; i<splits .length; i++)
-
{
-
temp.push({key : splits[i], value : 1});
-
}
-
return temp;
-
},
-
-
-
reduce : function(allSteps) {
-
var result = {};
-
for(var i=0; i<allSteps.length; i++)
-
{
-
var step = allSteps[i];
-
result[step.key] = result[step.key] ? (result[step.key] + 1) : 1;
-
}
-
return result;
-
},
-
-
-
init : function() {
-
var allSteps = [];
-
for(var i=0; i<Job.data.length; i++)
-
allSteps = allSteps.concat(Job.map(Job.data[i]));
-
-
var result = Job.reduce(allSteps)
-
console.log(JSON.stringify(result));
-
}
-
-
}; // Job
-
-
-
Job.init();
-
Copy & paste this code in your browser’s JS console.
Console output…
-
-
{"631":1,"We":1,"are":1,"glad":1,"to":5,"see":2,"you":2,"here.":1,"This":1,"site":2,"is":2,"dedicated":1,"poetry":3,"and":4,"the":5,"people":1,"who":1,"make":1,"possible":1,"poets":2,"their":1,"readers.":1,"FamousPoetsAndPoems.com":1,"a":2,"free":1,"site.":1,"On":1,"our":1,"can":1,"find":1,"large":1,"collection":1,"of":1,"poems":1,"quotes":1,"from":1,"over":1,"Read":1,"Enjoy":1,"Poetry":1,"I,":2,"too,":2,"sing":1,"America":2,"I":3,"am":3,"darker":1,"brother":1,"They":1,"send":1,"me":2,"eat":2,"in":2,"kitchen":2,"When":2,"company":2,"comes":2,"But":1,"laugh":1,"And":3,"well":1,"grow":1,"strong":1,"Tomorrow":1,"Ill":1,"be":2,"at":1,"table":1,"Nobodyll":1,"dare":1,"Say":1,"Eat":1,"Then":1,"Besides":1,"Theyll":1,"how":1,"beautiful":1,"ashamed":1}
-
Related posts:
- Kickass MCV for your Javascript Apps Very recently, I had an opportunity to work on a...
Related posts brought to you by Yet Another Related Posts Plugin.








