您的位置:首页 > 运维架构

Hadoop中wordcount源码分析

2012-04-29 14:42 477 查看
0、前言

本文是学习hadoop后的笔记总结,由于对hadoop了解不深,正处于摸索阶段,所以分析不够透测。本文是记录我的学习过程和学习总结。

环境:ubuntu 8.04.4 hadoop1.0.2(hadoop的版本不同,API略有变化)

参考书籍:

《 Hadoop权威指南(中文版)》 清华出版社

《实战Hadoop--开启通向运计算的捷径》 刘鹏主编

1、Hadoop版的helloworld源码(即wordcount)

源码来自于 /usr/local/hadoop-1.0.2/src/examples/org/apache/hadoop/examples/WordCount.java

WordCount

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<Object, Text, Text, IntWritable>
{

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
{
//print "key"  "value"
//System.out.println("key= "+key.toString());
//System.out.println("value= "+value.toString());

StringTokenizer itr = new StringTokenizer(value.toString());
while (itr.hasMoreTokens()) {
word.set(itr.nextToken());
context.write(word, one);
}
}
}

public static class IntSumReducer
extends Reducer<Text,IntWritable,Text,IntWritable>
{
private IntWritable result = new IntWritable();

public void reduce(Text key, Iterable<IntWritable> 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 <in> <out>");
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);
}
}


2、分析

从以下几个方面进行分析:数据类型,执行过程(map,reduce),主函数(作业的配置方法)

(1)常见数据类型

整型:IntWritable, 这是Hadoop对int的封装

字符串型:Text,这是Hadoop对String的封装

上下文对象:Context,它用来与MapReduce系统进行通信,如把map的结果传给
reduce
处理

由于map、reduce的输入输出key/value组成的键值对,所以用context.write(key,value)来传递数据

(2)执行过程

分为两个阶段:map和reduce, 以key/value为输入输出,其中key、value的类型可以由程序员自定义

2.1 map阶段

自定义一个类,继承于基类Mapper,该基类是一个泛型,有4个形参类型:用来指定map函数的输入键、输入值,输出键、输 出值,如下 public class Mapper<KEYIN, VALUEIN, KEYOUT, VALUEOUT>,源码位于/usr/local/hadoop-1.0.2/src/mapred/org/apache/hadoop/mapreduce/Mapper.java,但老版本的Mapper是一个接口public interface Mapper<K1, V1, K2, V2> extends JobConfigurable, Closeable ,老版本源码位于/usr/local/hadoop-1.0.2/src/mapred/org/apache/hadoop/mapred/Mapper.java。

根据实际需要,重写map函数,函数类型由Mapper指定。Called once for each key/value pair in the input split. Most applications should override map().每一对<key,value>调用一次map函数。

wordcount程序中,map方法中的value值存储的是文本文件中的一行,key值为该行的首字符相对于文本文件首字符的偏移量,在本程序中,key值未使用。StringTokenizer类是将每一行拆分为一个个的单词。

2.2 reduce阶段

自定义一个类,继承于基类Reducer,该基类是一个泛型,有4个形参类型:用来指定reduce函数的输入键、输入值,输出键、输出值public class Reducer<KEYIN,VALUEIN,KEYOUT,VALUEOUT>,其中reduce的输入类型必须与map的输出类型一致。

根据实际需要,重写reduce方法,方法的类型由Reducer指定,called once for each key. Most applications will define their reduce class by overriding this method。每一个key调用一次reduce方法。

2.3 主函数(作业的配置方法)

注意:新版本中使用/usr/local/hadoop-1.0.2/src/mapred/org/apache/hadoop/mapreduce 中的Job类来进行作业的配置

Job类主要的方法:

setJarByClass(Class<?> cls),作用:Set the Jar by finding where a given class came from.

setOutputKeyClass(Class<?> theClass),作用:Set the key class for the job output data.

setOutputValueClass(Class<?> theClass) ,作用:Set the value class for job outputs.

setJobName(String name)

setMapperClass(Class<? extends Mapper> cls),作用:Set the{@link Mapper} for the job

setReducerClass(Class<? extends Reducer> cls) ,作用: Set the {@link Reducer} for the job

waitForCompletion(boolean verbose ) ,作用:Submit the job to the cluster and wait for it to finish.

3、Hadoop程序处理流程

(1)将文件拆分为splits,并由MapReduce框架自动完成分割,将每一个split分割为<key,value>对

(2)每一对<key,value>调用一次map函数,处理后生产新的<key,value>对,由Context传递给reduce处理

(3)Mapper对<key,value>对进行按key值进行排序,并执行Combine过程,将key值相同的value进行合并。最后得到Mapper的最终输出结果

(4)reduce处理,处理后将新的<key,value>对输出。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: