MapReduce的Word Count过程说明

 

 

1、MapReduce编程模型

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

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

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

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

2、MapReduce处理过程

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

MapReduce处理大数据集的过程

3、wordcount源码分析

3.1、特别数据类型介绍

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

BooleanWritable:标准布尔型数值

ByteWritable:单字节数值

DoubleWritable:双字节数

FloatWritable:浮点数

IntWritable:整型数

LongWritable:长整型数

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

NullWritable:当<key, value>中的key或value为空时使用

3.2、源代码

  1.  
  2. package org.apache.hadoop.examples;
  3.  
  4. import java.io.IOException;
  5. import java.util.Iterator;
  6. import java.util.StringTokenizer;
  7.  
  8. import org.apache.hadoop.fs.Path;
  9. import org.apache.hadoop.io.IntWritable;
  10. import org.apache.hadoop.io.LongWritable;
  11. import org.apache.hadoop.io.Text;
  12. import org.apache.hadoop.mapred.FileInputFormat;
  13. import org.apache.hadoop.mapred.FileOutputFormat;
  14. import org.apache.hadoop.mapred.JobClient;
  15. import org.apache.hadoop.mapred.JobConf;
  16. import org.apache.hadoop.mapred.MapReduceBase;
  17. import org.apache.hadoop.mapred.Mapper;
  18. import org.apache.hadoop.mapred.OutputCollector;
  19. import org.apache.hadoop.mapred.Reducer;
  20. import org.apache.hadoop.mapred.Reporter;
  21. import org.apache.hadoop.mapred.TextInputFormat;
  22. import org.apache.hadoop.mapred.TextOutputFormat;
  23.  
  24. public class WordCount {
  25.  
  26. public static class Map extends MapReduceBase implements
  27. Mapper<LongWritable, Text, Text, IntWritable> {
  28. private final static IntWritable one = new IntWritable(1);
  29. private Text word = new Text();
  30.  
  31. public void map(LongWritable key, Text value,
  32. OutputCollector<Text, IntWritable> output, Reporter reporter)
  33. throws IOException {
  34. String line = value.toString();
  35. StringTokenizer tokenizer = new StringTokenizer(line);
  36. while (tokenizer.hasMoreTokens()) {
  37. word.set(tokenizer.nextToken());
  38. output.collect(word, one);
  39. }
  40. }
  41. }
  42.  
  43. public static class Reduce extends MapReduceBase implements
  44. Reducer<Text, IntWritable, Text, IntWritable> {
  45. public void reduce(Text key, Iterator<IntWritable> values,
  46. OutputCollector<Text, IntWritable> output, Reporter reporter)
  47. throws IOException {
  48. int sum = 0;
  49. while (values.hasNext()) {
  50. sum += values.next().get();
  51. }
  52. output.collect(key, new IntWritable(sum));
  53. }
  54. }
  55.  
  56. public static void main(String[] args) throws Exception {
  57. JobConf conf = new JobConf(WordCount.class);
  58. conf.setJobName("wordcount");
  59.  
  60. conf.setOutputKeyClass(Text.class);
  61. conf.setOutputValueClass(IntWritable.class);
  62.  
  63. conf.setMapperClass(Map.class);
  64. conf.setCombinerClass(Reduce.class);
  65. conf.setReducerClass(Reduce.class);
  66.  
  67. conf.setInputFormat(TextInputFormat.class);
  68. conf.setOutputFormat(TextOutputFormat.class);
  69.  
  70. FileInputFormat.setInputPaths(conf, new Path(args[0]));
  71. FileOutputFormat.setOutputPath(conf, new Path(args[1]));
  72.  
  73. JobClient.runJob(conf);
  74. }
  75. }

 

4、WordCount处理过程

1)将文件拆分成splits,由于测试用的文件较小,所以每个文件为一个split,并将文件按行分割形成<key, value>对,如下图所示,这一步由MapReduce框架自动完成,其中偏移量(即key值)包括了回车所占的字符数(Windows和Linux环境会不同)。

2)将分割好的<key, value>对交给用户定义的map方法进行处理,生成新的<key, value>对,如下图所示:

3)得到map方法输出的<key, value>之后,Mapper会将它们按照key值进行排序,并执行Combine过程,将key值相同的value值累加,得到Mapper的最终输出结果。如下图所示:

4)Reducer先对Mapper接收的数据进行排序,再交由用户自定义的reduce方法进行处理,得到新的<key, value>对,并作为wordcount的输出结果,如下图所示:

参考文章:

https://blog.csdn.net/universe_ant/article/details/52624867

https://blog.csdn.net/litianxiang_kaola/article/details/71154302

https://blog.csdn.net/u012453843/article/details/52561533

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值