行善济人,人遂得以安全,即在我亦为快意
逞奸谋事,事难必其稳便,可惜他徒自坏心
相关链接
HDFS相关知识
Hadoop集群连接
HDFS Java API
Hadoop分布式文件系统(HDFS)Java接口(HDFS Java API)详细版
WordCount程序
程序下载
写在前面
以下内容主要分为两个部分,第一个部分是在IntelliJ IDEA和Eclipse中运行WordCount程序,以测试IDE和Hadoop集群是否连接成功,第二个部分是WordCount代码讲解
连接测试
Eclipse
IntelliJ IDEA
代码讲解
代码总览
package cn.neu.wordcount;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileSystem;
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;
import java.io.IOException;
import java.util.StringTokenizer;
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();
protected 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();
protected 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 IOException, ClassNotFoundException, InterruptedException {
System.setProperty("HADOOP_USER_NAME", "root");
Configuration conf = new Configuration();
FileSystem fs = FileSystem.get(conf);
String[] otherArgs = new GenericOptionsParser(conf, args).getRemainingArgs();
if(otherArgs.length != 2){
System.err.println("Usage: wordcount <in> <out>");
System.exit(2);
}
Path outPath = new Path(otherArgs[1]);
if(fs.exists(outPath)) fs.delete(outPath, true);
Job job = Job.getInstance(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);
}
}
分段讲解
public static class TokenizerMapper extends Mapper<Object, Text, Text, IntWritable>{
private final static IntWritable one = new IntWritable(1);
private Text word = new Text();
protected 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);
}
}
}
- TokenizerMapper继承Mapper接口,
- Map阶段接收输入的<key, value>(key是当前输入的行号,value是该行的内容),程序对该行内容进行切词
- 每切下来一个词就以<word, 1>的形式输出
- 由于数据需要在网络间传输,IntWritable为可序列化的整型类
- 程序中one表示单词出现的次数为1
- 程序中word用于存储切下来的单词
public static class IntSumReducer extends Reducer<Text, IntWritable, Text, IntWritable> {
private IntWritable result = new IntWritable();
protected 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);
}
}
- 在Reduce阶段,TaskTracker接收到形式为<word, n>的数据(之所以不是<word, 1>的原因是Map阶段可以有Combiner,即在Map阶段对单词计数)
- Reduce每接收一个<word, n>,就会在word的频数上加n
- Reduce以<word, sum>的形式输出
- 程序中result记录单词的频数
public static void main(String[] args) throws IOException, ClassNotFoundException, InterruptedException {
//防止出现权限错误
System.setProperty("HADOOP_USER_NAME", "root");
Configuration conf = new Configuration();
FileSystem fs = FileSystem.get(conf);
String[] otherArgs = new GenericOptionsParser(conf, args).getRemainingArgs();
//判断参数数目是否正确,若不正确则退出程序
if(otherArgs.length != 2){
System.err.println("Usage: wordcount <in> <out>");
System.exit(2);
}
Path outPath = new Path(otherArgs[1]);
//判断程序输出路径是否已经存在,若已经存在则删除该路径
if(fs.exists(outPath)) fs.delete(outPath, true);
//初始化Job
Job job = Job.getInstance(conf, "word count");
//设置Job参数
job.setJarByClass(WordCount.class);
//设置Mapper类
job.setMapperClass(TokenizerMapper.class);
//设置Combiner类,此处直接使用Reducer类作为Combiner类,Combiner类能够降低网络压力,提高效率,注意,MapReduce编程模型在一些情况下不能使用Combiner类
job.setCombinerClass(IntSumReducer.class);
//设置Reducer类
job.setReducerClass(IntSumReducer.class);
//设置输出Key的类型
job.setOutputKeyClass(Text.class);
//设置输出value的类型
job.setOutputValueClass(IntWritable.class);
//设置输入路径
FileInputFormat.addInputPath(job, new Path(otherArgs[0]));
//设置输出路径
FileOutputFormat.setOutputPath(job, new Path(otherArgs[1]));
//Map/Reduce执行
System.exit(job.waitForCompletion(true) ? 0 : 1);
}
- 在MapReduce启动阶段,JobTracker先将Job的输入文件分割到每个Map Task上
- MapReduce启动Job,每个Map Task在启动之后会接收到自己所分配的输入数据,Map函数会对输入的内容进行词分割,然后输出每个单词及其频次
- 本例设置了Combiner类为Reduce的类,每个Map Task将输出发送给Reduce时,会先执行一次Combiner,相当于将结果文件先进行局部合并
- 接下来是shuffle过程,对Map的输出进行排序和合并,根据Reduce Task的数量对Map的输出进行分割并交给对应的Reduce(此阶段一般采用Hash函数进行分割)
- Reduce对<key, value-list>进行处理,计算每个单词出现的次数
- Reduce输出单词和对应的频数