1.创建Mapper类,发送(单词,1)到reduce端。
package com.aura.hadoop.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;
/**
* @author panghu
* @description wordcount Mapper类,负责把单词拆分为(单词,1)的形式发送给reducer
* 四个泛型分别表示:输入的key类型,输入的value类型,输出的key类型,输出的value类型
* @create 2021-02-14-11:58
*/
public class WcMapper extends Mapper<LongWritable,Text,Text,IntWritable> {
private Text word = new Text();
private IntWritable one = new IntWritable(1);
/**
* map方法每行就会调用一次
* @param key 偏移量
* @param value 每一行的数据
* @param context 上下文对象,任务本身
* @throws IOException
* @throws InterruptedException
*/
@Override
protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
// 获取每行数据
String line = value.toString();
// 切分单词
String[] words = line.split(" ");
for (String w : words) {
// 包装并发送给框架
this.word.set(w);
context.write(word,one);
}
}
}
2.创建Reducer类,统计单词数量。
package com.aura.hadoop.wc;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Reducer;
import java.io.IOException;
/**
* @author panghu
* @description wordcount的reducer类,负责接收mapper类的输出,并进行汇总计算
* 四个泛型类型分别为:map端的输出key类型,map端的输出value类型,reduce端的输出key类型,reduce端的输出value类型
* @create 2021-02-14-11:58
*/
public class WcReducer extends Reducer<Text,IntWritable,Text,IntWritable>{
private Text word = new Text();
private IntWritable cnt = new IntWritable();
/**
* 汇总统计map端的输出,接收到的数据类似于 单词:[1,1,1,1...]这种形式
* reduce方法每组调用一次,即每个不同的key调用一次
* @param key
* @param values
* @param context
* @throws IOException
* @throws InterruptedException
*/
@Override
protected void reduce(Text key, Iterable<IntWritable> values, Context context) throws IOException, InterruptedException {
System.out.println("---------------------------------");
int sum = 0;
for (IntWritable value : values) {
sum += value.get();
}
// 发送数据
this.word.set(key);
this.cnt.set(sum);
context.write(word,cnt);
}
}
3.创建Driver类,串联Mapper和Reducer。
package com.aura.hadoop.wc;
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.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
import java.io.IOException;
/**
* @author panghu
* @description wordcount的Driver类,用于串联Mapper类和Reducer类
* @create 2021-02-14-11:58
*/
public class WcDriver {
public static void main(String[] args) throws IOException, ClassNotFoundException, InterruptedException {
// 1.获取job对象
Job job = Job.getInstance(new Configuration());
// 2.设置jar包
job.setJarByClass(WcDriver.class);
// 3.设置Mapper类和Reducer类
job.setMapperClass(WcMapper.class);
job.setReducerClass(WcReducer.class);
// 4.设置map端和reduce端输出类型
job.setMapOutputKeyClass(Text.class);
job.setMapOutputValueClass(IntWritable.class);
job.setOutputKeyClass(Text.class);
job.setOutputValueClass(IntWritable.class);
// 5.设置输入输出路径
FileInputFormat.setInputPaths(job, new Path(args[0]));
FileOutputFormat.setOutputPath(job, new Path(args[1]));
// 6.提交任务
boolean res = job.waitForCompletion(true);
System.exit(res ? 0:1);
}
}