超简单的MapReduce Javascript实现

2011-05-18 18:15

超简单的MapReduce Javascript实现

by yuanyi

at 2011-05-18 10:15:25

original http://heikezhi.com/2011/05/17/simple-mapreduce-with-javascript/

要理解MapReduce,最好的办法就是通过它的实现来了解它的工作原理,Mayank Jain为我们提供了一个可能是最简单的Javascript实现,通过这个实现,你会发现,MapReduce最核心的理念其实并不复杂(Gist链接)。

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();

下面是代码的运行结果:

{"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}

想和我们一道传播黑客精神?快来加入吧!

无觅猜您也喜欢:

Handlebars.js入门介绍

JavaScript版查找树介绍及性能分析

Javascript版Heroku: Akshell

Traceur: 体验下一代Javascript语言
无觅