WordCount是Hadoop官方提供的一个测试示例,用于词频统计,非常适合初学者学习。
查看源码:
解压hadoop发行版(如hadoop 2.6.4)的压缩包,在目录“hadoop-2.6.4\share\hadoop\mapreduce\sources"中找到hadoop-mapreduce-examples-2.6.4-sources.jar文件,解压。然后在解压后的目录中“org/apache/hadoop/examples”中找到WordCount文件。
注解供参考:
/**
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
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 {
//Part1:Mapper模块
public static class TokenizerMapper
extends Mapper<Object, Text, Text, IntWritable>{ //继承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); //输出键值对<word,1>
}
}
}
//Part2:Reducer模块
public static class IntSumReducer
extends Reducer<Text,IntWritable,Text,IntWritable> { //继承Reducer,同时设置输入输出键值对格式
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); //输出键值对<key,result>
}
}
//Part3:应用程序Driver
public static void main(String[] args) throws Exception {
Configuration conf = new Configuration(); //初始化相关Hadoop配置
String[] otherArgs = new GenericOptionsParser(conf, args).getRemainingArgs();
if (otherArgs.length < 2) {
System.err.println("Usage: wordcount <in> [<in>...] <out>");
System.exit(2);
}
Job job = new Job(conf, "word count"); //新建job并设置主类,“word count”为MapReduce任务名
job.setJarByClass(WordCount.class);
//设置Mapper、Combiner、Reducer
job.setMapperClass(TokenizerMapper.class); //必选
job.setCombinerClass(IntSumReducer.class); //可选
job.setReducerClass(IntSumReducer.class); //必选
//设置输出键值对格式
job.setOutputKeyClass(Text.class);
job.setOutputValueClass(IntWritable.class);
//设置输入和输出路径
for (int i = 0; i < otherArgs.length - 1; ++i) {
FileInputFormat.addInputPath(job, new Path(otherArgs[i]));
}
FileOutputFormat.setOutputPath(job,
new Path(otherArgs[otherArgs.length - 1]));
//提交MapReduce任务运行,并等待运行结束
System.exit(job.waitForCompletion(true) ? 0 : 1); //固定写法
}
}