一、重写三个方法Mapper、Reducer和Driver
二、对于Mapper
package my.study.mapreduce.wordcount;
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的类型 :LongWritable;
* VALUEIN,map阶段输入value类型:Text(String);
* KEYOUT, map阶段输出的Key类型:Text;
* VALUEOUT, map阶段输出的value类型:IntWritable(Int)
*
* */
public class WordCountMapper extends Mapper<LongWritable, Text, Text, IntWritable> {
private Text outK = new Text();
private IntWritable outV = new IntWritable(1);
@Override
protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
//1获取一行
String line = value.toString();
//2切割
String[] words = line.split(" ");
//3循环写出
for(String word : words){
//封装
outK.set(word);
//写出
context.write(outK,outV);
}
}
}
三、对于Reducer
package my.study.mapreduce.wordcount;
/*
* KEYIN:,reduce阶段输入的Key的类型 :Text;
* VALUEIN,reduce阶段输入value类型:IntWritable;
* KEYOUT, reduce阶段输出的Key类型:Text;
* VALUEOUT, reduce阶段输出的value类型:IntWritable(Int)
*
* */
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Reducer;
import java.io.IOException;
public class WordCountReducer extends Reducer<Text, IntWritable,Text,IntWritable> {
private IntWritable outV = new IntWritable();
@Override
protected void reduce(Text key, Iterable<IntWritable> values, Context context) throws IOException, InterruptedException {
int sum = 0;
//ss,(1,1)
for (IntWritable value : values) {
sum += value.get();
}
outV.set(sum);
//写出
context.write(key, outV);
}
}
四、对于driver
package my.study.mapreduce.wordcount;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
import java.io.IOException;
public class WordCountDriver {
public static void main(String[] args) throws IOException, ClassNotFoundException, InterruptedException {
//1获取jop
Configuration conf = new Configuration();
Job job = Job.getInstance(conf);
//2获取jar包路径
job.setJarByClass(WordCountDriver.class);
//3关联mapper和reducer
job.setMapperClass(WordCountMapper.class);
job.setReducerClass(WordCountReducer.class);
//4设置map输出的kv类型
job.setMapOutputKeyClass(Text.class);
job.setMapOutputValueClass(IntWritable.class);
//5设置最终输出的kv类型
job.setOutputKeyClass(Text.class);
job.setOutputValueClass(IntWritable.class);
//6设置输入路径和输出路径
FileInputFormat.setInputPaths(job, new Path("D:\\hadoop\\input\\wordCount\\words.txt"));
FileOutputFormat.setOutputPath(job, new Path("D:\\hadoop\\output\\wordCount"));
//7提交job
boolean result = job.waitForCompletion(true);
System.exit(result ? 0 : 1);
}
}
五、对于Combiner
(1)作用范围在Mapper后Reducer之前
(2)代码实现,在driver中添加
job.setCombinerClass(WordCountCombiner.class);
六、debug调试节点设置
1.map方法:String line = value.toString();
mapper中run方法:setup(context); 和 cleanup(context);
2.reduce方法:int sum = 0;
Reducer中run方法:setup(context); 和 cleanup(context);
七、集群运行
hadoop jar jar包 driver的copy_Reference 输入路径 输出路径
参考尚硅谷Hadoop视频!原视频连接:尚硅谷【官网】谷粉与老学员力挺的Java培训|大数据培训|前端培训|UI设计培训