1.编写成绩表A.txt文本
A.txt:
语文 96
数学 102
英语 130
物理 19
化学 44
生物 44
语文 109
数学 118
英语 141
物理 72
化学 21
生物 7
语文 92
数学 103
英语 139
物理 20
化学 58
生物 12
语文 107
数学 112
英语 133
物理 88
化学 11
生物 22
2.编写FindMax.java代码
//mapper
public static class FindMaxMapper extends Mapper<LongWritable, Text,Text,IntWritable>{
Text course = new Text();
IntWritable score = new IntWritable();
@Override
protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
String [] values = value.toString().trim().split(" ");
course.set(values[0]);
score.set(Integer.parseInt(values[1]));
context.write(course,score);
}
}
//reducer
public static class FindMaxReducer extends Reducer<Text,IntWritable,Text,IntWritable>{
@Override
protected void reduce(Text key, Iterable<IntWritable> values, Context context) throws IOException, InterruptedException {
int maxScore = -1;
Text course = new Text();
for(IntWritable score:values){
if (score.get()>maxScore){
maxScore = score.get();
course = key;
}
}
context.write(course,new IntWritable(maxScore));
}
}
//driver
public static void main(String [] args) throws Exception{
if (args.length != 2){
System.out.println("FindMax <input> <output>");
System.exit(-1);
}
Configuration conf = new Configuration();
Job job = Job.getInstance(conf,"findmax");
job.setJarByClass(FindMax.class);
job.setMapperClass(FindMaxMapper.class);
job.setReducerClass(FindMaxReducer.class);
job.setMapOutputKeyClass(Text.class);
job.setMapOutputValueClass(IntWritable.class);
job.setOutputKeyClass(Text.class);
job.setOutputValueClass(IntWritable.class);
job.setNumReduceTasks(1);
FileInputFormat.addInputPath(job,new Path(args[0]));
FileSystem.get(conf).delete(new Path(args[1]),true);
FileOutputFormat.setOutputPath(job,new Path(args[1]));
System.out.println(job.waitForCompletion(true) ? 0 : 1);
}