1. mapreduce实现
Mapper类
public class WordCountMapper extends Mapper<LongWritable,Text,Text,IntWritable>{
/*
* LongWritable key:输入的每一行的偏移量 框架读取的
* Text value:输入的每一行的内容
* Context context:上下文对象 用于向reduce发送数据 读取框架给的东西
*
* 调用频率:一行调用一次
* 一个文本100行 这个方法就会调用100次
*/
@Override
protected void map(LongWritable key, Text value, Context context)
throws IOException, InterruptedException {
//读取每一行内容,转换为String
String line = value.toString();
String[] words = line.split("/t");
/*
* 如果要统计 只能统计一行的 并不能统计一个文件的
* 全部发送到reduce端进行统计
* key:单词
* value:1
*/
for(String w:words) {
Text k = new Text(w);
IntWritable v = new IntWritable(1);
context.write(k, v);
}
}
}
Reducer类
public class WordCountReducer extends Reducer<Text,IntWritable,Text,IntWritable>{
/*map到reduce之间有一个shuffle的过程(洗牌/将map输出的数据进行打乱 重洗)
Context context 上下文对象 向上承接map 向下输出结果 hdfs/本地
这个函数的调用频率:一组调用一次 有多少组就调用几次
*/
@Override
protected void reduce(Text key, Iterable<IntWritable> values,
Context context) throws IOExc