MapReduce中wordcount详细介绍

来源:互联网 发布:筹码分布软件 编辑:程序博客网 时间:2024/06/11 10:42

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

MapReduce处理大数据集的过程

3、wordcount源码分析

3.1、特别数据类型介绍

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

BooleanWritable:标准布尔型数值

ByteWritable:单字节数值

DoubleWritable:双字节数

FloatWritable:浮点数

IntWritable:整型数

LongWritable:长整型数

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

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

3.2、源代码

    package org.apache.hadoop.examples;    import java.io.IOException;    import java.util.Iterator;    import java.util.StringTokenizer;    import org.apache.hadoop.fs.Path;    import org.apache.hadoop.io.IntWritable;    import org.apache.hadoop.io.LongWritable;    import org.apache.hadoop.io.Text;    import org.apache.hadoop.mapred.FileInputFormat;    import org.apache.hadoop.mapred.FileOutputFormat;    import org.apache.hadoop.mapred.JobClient;    import org.apache.hadoop.mapred.JobConf;    import org.apache.hadoop.mapred.MapReduceBase;    import org.apache.hadoop.mapred.Mapper;    import org.apache.hadoop.mapred.OutputCollector;    import org.apache.hadoop.mapred.Reducer;    import org.apache.hadoop.mapred.Reporter;    import org.apache.hadoop.mapred.TextInputFormat;    import org.apache.hadoop.mapred.TextOutputFormat;    public class WordCount {        public static class Map extends MapReduceBase implements                Mapper<LongWritable, Text, Text, IntWritable> {            private final static IntWritable one = new IntWritable(1);            private Text word = new Text();            public void map(LongWritable key, Text value,                    OutputCollector<Text, IntWritable> output, Reporter reporter)                    throws IOException {                String line = value.toString();                StringTokenizer tokenizer = new StringTokenizer(line);                while (tokenizer.hasMoreTokens()) {                    word.set(tokenizer.nextToken());                    output.collect(word, one);                }            }        }        public static class Reduce extends MapReduceBase implements                Reducer<Text, IntWritable, Text, IntWritable> {            public void reduce(Text key, Iterator<IntWritable> values,                    OutputCollector<Text, IntWritable> output, Reporter reporter)                    throws IOException {                int sum = 0;                while (values.hasNext()) {                    sum += values.next().get();                }                output.collect(key, new IntWritable(sum));            }        }        public static void main(String[] args) throws Exception {            JobConf conf = new JobConf(WordCount.class);            conf.setJobName("wordcount");            conf.setOutputKeyClass(Text.class);            conf.setOutputValueClass(IntWritable.class);            conf.setMapperClass(Map.class);            conf.setCombinerClass(Reduce.class);            conf.setReducerClass(Reduce.class);            conf.setInputFormat(TextInputFormat.class);            conf.setOutputFormat(TextOutputFormat.class);            FileInputFormat.setInputPaths(conf, new Path(args[0]));            FileOutputFormat.setOutputPath(conf, new Path(args[1]));            JobClient.runJob(conf);        }    }

4、WordCount处理过程

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

分割过程

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

执行map方法

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

Map端排序及Combine过程

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

Reduce端排序及输出结果






原文链接




0 0
原创粉丝点击