MapReduce实现词频统计功能我们就要用到他的两个最重要的类 Mapper、Reducer
首先我们先编写两个类,分别继承Mapper类和Reducer类
首先是 Mapper类:
package com.mr.wc;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Mapper;
import java.io.IOException;
/*
* KEYIN : Map任务读数据的Key类型,offset,是每行数据起始位置的偏移量 ,Long
* VALUEIN : Map任务读取数据的value类型,就是每一行的字符串,String
*
*
* KEYOUT:map方法自定义实现输出的key的类型,String
* VALUEOUT:map方法自定义实现输出的value的类型,Integer
*
* 词频统计:(word,1)
*
*
* Long,String,Integer是Java的类型
* Hapoop自定义类型:可以序列化、反序列化
*
*
*
* */
public class WordCountMapper extends Mapper {
@Override
protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
//把value对应的行数据按照指定的分割符分开
String[] words = value.toString().split("\t");
for(String word : words){
word = word.toLowerCase();
//(hello,1) (word,1)
context.write(new Text(word),new IntWritable(1));
}
}
}
注意这个地方用到了一个大小写转化的过程因为防止出现因大小写不一致导致单词统计出多个的问题(其实这个地方可以根据不同业务需求进行修改)
然后是Reducer类:
package com.mr.wc;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Reducer;
import java.io.IOException;
import java.util.Iterator;
public class WordCountReducer extends Reducer {
/*
* (hello,1) (word,1)
* (hello,1) (word,1)
* (hello,1) (word,1)
* (welcome,1)
*
* map的输出到reduce端,是按照相同的key分发到一个reduce上去执行
*
* reduce1 : (hello,1) (hello,1) (hello,1) ===> (hello,<1,1,1>)
* reduce2 : (word,1) (word,1) (word,1) ===> (word,<1,1,1>)
* reduce3 : (welcome,1) ===> (welcome,<1>)
*
* */
@Override
protected void reduce(Text key, Iterable values, Context context) throws IOException, InterruptedException {
int count = 0;
Iterator iterator = values.iterator();
while(iterator.hasNext()){
IntWritable value = iterator.next();
count += value.get();
}
context.write(key,new IntWritable(count));
}
}
最后就是主体实现类:
package com.mr.wc;
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.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
import java.net.URI;
/*
* 使用MR统计HDFS上的文件对应的词频
* */
public class WordCountApp {
public static void main(String[] args)throws Exception {
System.setProperty("HADOOP_USER_NAME","hadoop");
Configuration configuration = new Configuration();
configuration.set("fs.defaultFS","hdfs://192.168.1.230:8020");
// 创建一个Job
Job job = Job.getInstance(configuration);
// 设置一个Job对应的参数:主类
job.setJarByClass(WordCountApp.class);
// 设置Job对应的参数:设置自定义的Mapper和Reducer处理类
job.setMapperClass(WordCountMapper.class);
job.setReducerClass(WordCountReducer.class);
// 设置Job对应的参数:Mapper输出key和value的类型
job.setMapOutputKeyClass(Text.class);
job.setMapOutputValueClass(IntWritable.class);
// 设置Job对应的参数:Reducer输出key和value的类型
job.setOutputKeyClass(Text.class);
job.setOutputValueClass(IntWritable.class);
// 如果输出目录已经存在,则先删除
FileSystem fileSystem = FileSystem.get(new URI("hdfs://192.168.1.230:8020"),configuration,"hadoop");
Path outputPath = new Path("/wordcount/output2");
if(fileSystem.exists(outputPath)){
fileSystem.delete(outputPath,true);
}
// 设置Job对应的参数:Mapper输出key和value的类型:作业输入输出的路径
FileInputFormat.setInputPaths(job,new Path("/wordcount/input"));
FileOutputFormat.setOutputPath(job,outputPath);
// 提交job
boolean result = job.waitForCompletion(true);
System.exit(result ? 0 : -1);
}
}
这样一个简单的词频统计Demo就完成啦~
但是这样真的是最好的嘛?我们都知道其实MapReduce的工作方式是如图所示的:
那么 在Mapping的过程当中 如果有大量的数据呢?这样我们就会出现很多组一样的键值对数据传输过去进行Reducing的处理,所以我们可不可以在Mapping的过程中就进行一次合并操作呢?(即Reducing操作)
其实MapReduce已经有这个处理啦,就是Combiner。只需要在代码中加入一行:
// 添加Combiner的设置
job.setCombinerClass(WordCountReducer.class);
这样就行啦~