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)

  1.  
  2. var Job = {
  3.  
  4.   data : [
  5.           "We are glad to see you here. This site is dedicated to",
  6.           "poetry and to the people who make poetry possible",
  7.           "poets and their readers. FamousPoetsAndPoems.com is",
  8.           "a free poetry site. On our site you can find a large",
  9.           "collection of poems and quotes from over 631 poets",
  10.           "Read and Enjoy Poetry",
  11.           "I, too, sing America",
  12.           "I am the darker brother",
  13.           "They send me to eat in the kitchen",
  14.           "When company comes",
  15.           "But I laugh",
  16.           "And eat well",
  17.           "And grow strong",
  18.           "Tomorrow",
  19.           "Ill be at the table",
  20.           "When company comes",
  21.           "Nobodyll dare",
  22.           "Say to me",
  23.           "Eat in the kitchen",
  24.           "Then",
  25.           "Besides",
  26.           "Theyll see how beautiful I am",
  27.           "And be ashamed",
  28.           "I, too, am America"
  29.         ],
  30.        
  31.  
  32.   map : function(line) {
  33.     var splits = line.split(" ");
  34.     var temp = [];
  35.     for(var i=0; i<splits .length; i++)
  36.     {
  37.       temp.push({key : splits[i], value : 1});
  38.     }
  39.     return temp;
  40.   },
  41.  
  42.  
  43.   reduce : function(allSteps) {
  44.     var result = {};
  45.     for(var i=0; i<allSteps.length; i++)
  46.     {
  47.       var step = allSteps[i];
  48.       result[step.key] = result[step.key] ? (result[step.key] + 1) : 1;
  49.     }
  50.     return result;
  51.   },
  52.  
  53.  
  54.   init : function() {
  55.     var allSteps = [];
  56.     for(var i=0; i<Job.data.length; i++)
  57.       allSteps = allSteps.concat(Job.map(Job.data[i]));
  58.      
  59.     var result = Job.reduce(allSteps)
  60.     console.log(JSON.stringify(result));
  61.   }
  62.  
  63. }; // Job
  64.  
  65.  
  66. Job.init();
  67.  

Copy & paste this code in your browser’s JS console.

Console output…

  1.  
  2. {"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}
  3.  

Related posts:

  1. 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.

  • http://www.facebook.com/sachinsaluja Sachin Saluja

    Perfect example…!! just run the reducer on a server and mapper on as many nodes as possible…

  • http://makuchaku.in/ makuchaku

    Why not run the reducer on browsers as well?
    & keep reducing in an inverted tree fashion till you can’t reduce any further :D