您的位置:首页 > 编程语言 > Java开发

通过简单的Word Count讲解MapReduce原理以及Java实现

2014-09-26 16:40 746 查看
MapReduce原理:

MapReduce采用"分而治之"的思想,把对大规模数据集的操作,分发给一个主节点管理下的各个分节点共同完成,然后通过整合各个节点的中间结果,得到最终结果。简单地说,MapReduce就是"任务的分解与结果的汇总"。



  在Hadoop中,用于执行MapReduce任务的机器角色有两个:一个是JobTracker;另一个是TaskTracker,JobTracker是用于调度工作的,TaskTracker是用于执行工作的。一个Hadoop集群中只有一台JobTracker。



  在分布式计算中,MapReduce框架负责处理了并行编程中分布式存储、工作调度、负载均衡、容错均衡、容错处理以及网络通信等复杂问题,把处理过程高度抽象为两个函数:map和reduce,map负责把任务分解成多个任务,reduce负责把分解后多任务处理的结果汇总起来。



需要注意的是,用MapReduce来处理的数据集(或任务)必须具备这样的特点:待处理的数据集可以分解成许多小的数据集,而且每一个小数据集都可以完全并行地进行处理。

在Hadoop中,每个MapReduce任务都被初始化为一个Job,每个Job又可以分为两种阶段:map阶段和reduce阶段。这两个阶段分别用两个函数表示,即map函数和reduce函数。map函数接收一个形式的输入,然后同样产生一个形式的中间输出,Hadoop函数接收一个如形式的输入,然后对这个value集合进行处理,每个reduce产生0或1个输出,reduce的输出也是形式的。

下面以一个最简单的例子说明:

单词计数是最简单也是最能体现MapReduce思想的程序之一,可以称为MapReduce版"Hello
World",该程序的完整代码可以在Hadoop安装包的"src/examples"目录下找到。单词计数主要完成功能是:统计一系列文本文件中每个单词出现的次数,如下图所示。



package org.apache.hadoop.examples;



import java.io.IOException;



import java.util.StringTokenizer;



import org.apache.hadoop.conf.Configuration;



import org.apache.hadoop.fs.Path;



import org.apache.hadoop.io.IntWritable;



import org.apache.hadoop.io.Text;



import org.apache.hadoop.mapreduce.Job;



import org.apache.hadoop.mapreduce.Mapper;



import org.apache.hadoop.mapreduce.Reducer;



import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;



import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;



import org.apache.hadoop.util.GenericOptionsParser;



public class WordCount {



  public static class TokenizerMapper



      extends Mapper {



      private final static IntWritable
one = new IntWritable(1);



      private Text word = new
Text();







      public void map(Object
key, Text value, Context context)



        throws IOException, InterruptedException
{



        StringTokenizer itr =
new StringTokenizer(value.toString());



        while (itr.hasMoreTokens())
{



        word.set(itr.nextToken());



        context.write(word, one);



      }



    }



  }



  public static class IntSumReducer



      extends Reducer {



      private IntWritable result
= new IntWritable();



      public void reduce(Text
key, Iterable values,Context context)



           throws IOException,
InterruptedException {



        int sum = 0;



        for (IntWritable val
: values) {



           sum += val.get();



        }



      result.set(sum);



      context.write(key, result);



    }



  }







  public static void main(String[]
args) throws Exception {



    Configuration conf = new
Configuration();



    String[] otherArgs = new
GenericOptionsParser(conf, args).getRemainingArgs();



    if (otherArgs.length != 2)
{



      System.err.println("Usage:
wordcount ");



      System.exit(2);



    }



    Job job = new Job(conf, "word
count");



    job.setJarByClass(WordCount.class);



    job.setMapperClass(TokenizerMapper.class);



    job.setCombinerClass(IntSumReducer.class);



    job.setReducerClass(IntSumReducer.class);



    job.setOutputKeyClass(Text.class);



    job.setOutputValueClass(IntWritable.class);



    FileInputFormat.addInputPath(job,
new Path(otherArgs[0]));



    FileOutputFormat.setOutputPath(job,
new Path(otherArgs[1]));



    System.exit(job.waitForCompletion(true)
? 0 : 1);



}



}



Map过程需要继承org.apache.hadoop.mapreduce包中Mapper类,并重写其map方法。通过在map方法中添加两句把key值和value值输出到控制台的代码,可以发现map方法中value值存储的是文本文件中的一行(以回车符为行结束标记),而key值为该行的首字母相对于文本文件的首地址的偏移量。然后StringTokenizer类将每一行拆分成为一个个的单词,并将作为map方法的结果输出,其余的工作都交有MapReduce框架处理。

Reduce过程需要继承org.apache.hadoop.mapreduce包中Reducer类,并重写其reduce方法。Map过程输出中key为单个单词,而values是对应单词的计数值所组成的列表,Map的输出就是Reduce的输入,所以reduce方法只要遍历values并求和,即可得到某个单词的总次数。

在MapReduce中,由Job对象负责管理和运行一个计算任务,并通过Job的一些方法对任务的参数进行相关的设置。此处设置了使用TokenizerMapper完成Map过程中的处理和使用IntSumReducer完成Combine和Reduce过程中的处理。还设置了Map过程和Reduce过程的输出类型:key的类型为Text,value的类型为IntWritable。任务的输出和输入路径则由命令行参数指定,并由FileInputFormat和FileOutputFormat分别设定。完成相应任务的参数设定后,即可调用job.waitForCompletion()方法执行任务。



Hadoop提供了如下内容的数据类型,这些数据类型都实现了WritableComparable接口,以便用这些类型定义的数据可以被序列化进行网络传输和文件存储,以及进行大小比较。







BooleanWritable:标准布尔型数值



ByteWritable:单字节数值



DoubleWritable:双字节数



FloatWritable:浮点数



IntWritable:整型数



LongWritable:长整型数



Text:使用UTF8格式存储的文本



NullWritable:当中的key或value为空时使用
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: