一、TopScore
package nj.zb.kb15.demo4; import org.apache.hadoop.io.WritableComparable; import java.io.DataInput; import java.io.DataOutput; import java.io.IOException; public class TopScore implements WritableComparable<TopScore> { private String subjectName; private int score; public TopScore() { } public TopScore(String subjectName, int score) { this.subjectName = subjectName; this.score = score; } @Override public String toString() { return "TopScore{" + "subjectName='" + subjectName + '\'' + ", score=" + score + '}'; } public String getSubjectName() { return subjectName; } public void setSubjectName(String subjectName) { this.subjectName = subjectName; } public int getScore() { return score; } public void setScore(int score) { this.score = score; } @Override public int compareTo(TopScore o) { return 0; } @Override public void write(DataOutput dataOutput) throws IOException { dataOutput.writeUTF(subjectName); dataOutput.writeInt(score); } @Override public void readFields(DataInput dataInput) throws IOException { this.subjectName=dataInput.readUTF(); this.score=dataInput.readInt(); } }
二、TopScoreMapper
package nj.zb.kb15.demo4; import org.apache.hadoop.io.LongWritable; import org.apache.hadoop.io.Text; import org.apache.hadoop.mapreduce.Mapper; import java.io.IOException; public class TopScoreMapper extends Mapper<LongWritable, Text,Text,TopScore> { @Override protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException { System.out.println(key.get()+" "+value); String[] split=value.toString().split(","); String subjectName=split[2]; Text text=new Text(subjectName); //赋值Text TopScore topScore=new TopScore(subjectName,Integer.parseInt(split[3])); context.write(text,topScore); } }
三、TopScoreReduce
package nj.zb.kb15.demo4; import org.apache.hadoop.io.Text; import org.apache.hadoop.mapreduce.Reducer; import java.io.IOException; public class TopScoreReducer extends Reducer<Text,TopScore,Text,TopScore> { TopScore topScore=new TopScore(); @Override protected void reduce(Text key, Iterable<TopScore> values, Context context) throws IOException, InterruptedException { String subjectName=""; int max=0; for(TopScore score:values){ if(max<score.getScore()){ max=score.getScore(); }else if(subjectName.equals("")){ subjectName=score.getSubjectName(); } } Text text=new Text(subjectName); topScore.setScore(max); topScore.setSubjectName(subjectName); context.write(text,topScore); } }
四、TopScoreDriver
package nj.zb.kb15.demo4; import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.fs.FileSystem; import org.apache.hadoop.fs.Path; 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; public class TopScoreDriver { public static void main(String[] args) throws IOException, ClassNotFoundException, InterruptedException { Configuration configuration = new Configuration(); Job job = Job.getInstance(configuration); job.setJarByClass(TopScoreDriver.class); job.setMapperClass(TopScoreMapper.class); job.setMapOutputKeyClass(Text.class); job.setMapOutputValueClass(TopScore.class); job.setReducerClass(TopScoreReducer.class); job.setOutputKeyClass(Text.class); job.setOutputValueClass(TopScore.class); FileInputFormat.setInputPaths(job, new Path("D:\\test_20211026\\in\\demo3\\stuscore.csv")); Path path = new Path("D:\\test_20211026\\in\\out5"); FileOutputFormat.setOutputPath(job, path); FileSystem fs = FileSystem.get(path.toUri(), configuration); if (fs.exists(path)) { fs.delete(path, true); } job.waitForCompletion(true); } }
五、运行结果
数学 TopScore{subjectName='数学', score=91} 英语 TopScore{subjectName='英语', score=47} 语文 TopScore{subjectName='语文', score=96}