标签:
在之前的工作中,主要做了三件事情:
1 如何完成Hadoop的完全分布式集群搭建
2 如何运行Hadoop自带示例WordCount,验证集群的运行
3 如何基于eclipse插件实现Hadoop编程
完成每一件事都需要经过谨慎的操作、反复的验证,还有耐心。安装完之后一下成功是很难的,仍需要检验每一步的操作、查看错误问题的日志、分析网上类似问题的各类解决方法,于是在千转百回之下,柳暗花明。我分享了以上操作的详细步骤和注意事项,如果你尚未搭建Hadoop,可以看一看,或许有帮助。
自此就正式开始进入Hadoop的学习之旅了。今天介绍Hadoop编程模型mapreduce中最基础的示例Wordcount。主要介绍两部分:
WordCount 的源码一般是在下载的Hadoop安装包下的hadoop-1.2.1/src/examples/org/apache/hadoop/examples 里面有WordCount.java文件,你可以使用UltraEdit或者记事本打开。内容如下:
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 {
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();
conf.set("mapred.job.tracker", "172.16.10.15:9001");//自己额外加的代码
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);
}
}
这里包含了三个部分:编写map类实现Mapper操作,编写reduce类实现Reduce操作,编写主函数实现参数设置和执行。
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 {
StringTokenizer itr = new StringTokenizer(value.toString());
while (itr.hasMoreTokens()) {
word.set(itr.nextToken());
context.write(word, one);
}
}
}
a) TokenizerMapper 这个类名自己设定,这个类需要继承org.apache.hadoop.mapreduce包中的Mapper类,并重写map方法。
b) map方法中参数value是指文本文件中的一行,参数key是为该行首字母相对于文本文件首地址的偏移量。
c) StringTokenizer类是一个用来分隔String的应用类,类似于split。
//它的构造函数有三种:
public StringTokenizer(String str)
public StringTokenizer(String str,String delim)
public StringTokenizer(String str,String delim,boolean returnDelims)
//其中第一个参数为要分隔的String,第二个参数为分隔字符集合,第三个参数为分隔符是否作为标记返回,如果不指定分隔符,默认是‘\t\n\r\f‘
//它的方法主要有三种:
public boolean hasMoreTokens()//返回是否还有分隔符
public String nextToken()//返回从当前位置到下一个分隔符的字符串
public int countTokens()//返回nextToken方法被调用的次数
d) 经过StringTolenizer 处理之后会得到一个个
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);
}
}
a) 同mapper 过程一样,Reduce过程需要继承org.apache.hadoop.mapreduce包中Reducer类,并重写其reduce方法。
标签:
原文地址:http://blog.csdn.net/u010414589/article/details/51334880