MapReduce【开发】
需求:统计一堆文件中单词出现的个数。
Driver函数的流程:
- 获取配置信息,获取job对象实例
- 指定本程序jar包所在的本地路径
- 指定mapper,reducer业务类
- 指定mapper输出数据的kv类型
- 指定最终输出的数据的kv类型
- 指定job的输入原始文件所在的目录
- 指定job的输出粗结果所在的目录
- 提交作业
a. 环境搭建(WordCount入门例子)
- 创建一个maven工程
- Mapper模板
package edu.durant.hadoop;
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;
//KIN,VIN,KOUT,VOUT
public class wordcntMapper extends Mapper<LongWritable, Text, Text, IntWritable>{
Text k = new Text();
IntWritable v = new IntWritable(1);
@Override
protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
String line = value.toString();
String[] words = line.split(" ");
for (String word : words) {
k.set(word);
context.write(k,v);
}
}
}
注意:Map的<>内前两个参数必须是LongWritable, Text,这是系统定义的。
- Reducer模板
public class IntSumReducer<Key> extends Reducer