一、SubjectScore
package nj.zb.kb15.demo3; //每个科目总分 import org.apache.hadoop.io.WritableComparable; import java.io.DataInput; import java.io.DataOutput; import java.io.IOException; public class SubjectScore implements WritableComparable<SubjectScore> { private String subjectName; private int score; public SubjectScore() { } public SubjectScore(String subjectName, int score) { this.subjectName = subjectName; this.score = score; } @Override public String toString() { return "SubjectScore{" + "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(SubjectScore 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(); } }
二、SubjectScoreMapper
package nj.zb.kb15.demo3; import org.apache.hadoop.io.LongWritable; import org.apache.hadoop.io.Text; import org.apache.hadoop.mapreduce.Mapper; import java.io.IOException; public class SubjectScoreMapper extends Mapper<LongWritable, Text,Text,SubjectScore> { @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 subject=split[2]; Text text=new Text(subject); SubjectScore subjectScore=new SubjectScore(subject,Integer.parseInt(split[3])); context.write(text,subjectScore); } }
三、SubjectScoreReducer
package nj.zb.kb15.demo3; import org.apache.hadoop.io.NullWritable; import org.apache.hadoop.io.Text; import org.apache.hadoop.mapreduce.Reducer; import java.io.IOException; public class SubjectScoreReducer extends Reducer<Text,SubjectScore,SubjectScore, NullWritable> { SubjectScore subjectScore=new SubjectScore(); @Override protected void reduce(Text key, Iterable<SubjectScore> values, Context context) throws IOException, InterruptedException { String subName=""; int sum=0; // int count=0; for(SubjectScore ss:values){ sum +=ss.getScore(); // count++; if(subName.equals("")) subName=ss.getSubjectName(); } // int avg=(int)sum/count; subjectScore.setScore(sum); subjectScore.setSubjectName(subName); context.write(subjectScore,NullWritable.get()); } }
四、SubjectScoreDriver
package nj.zb.kb15.demo3; import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.fs.FileSystem; import org.apache.hadoop.fs.Path; import org.apache.hadoop.io.NullWritable; 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 SubjectScoreDriver { public static void main(String[] args) throws IOException, ClassNotFoundException, InterruptedException { Configuration configuration = new Configuration(); Job job = Job.getInstance(configuration); job.setJarByClass(SubjectScoreDriver.class); job.setMapperClass(SubjectScoreMapper.class); job.setMapOutputKeyClass(Text.class); job.setMapOutputValueClass(SubjectScore.class); job.setReducerClass(SubjectScoreReducer.class); job.setOutputKeyClass(SubjectScore.class); job.setOutputValueClass(NullWritable.class); FileInputFormat.setInputPaths(job, new Path("D:\\test_20211026\\in\\demo3\\stuscore.csv")); Path path = new Path("D:\\test_20211026\\in\\out4"); FileOutputFormat.setOutputPath(job, path); FileSystem fs = FileSystem.get(path.toUri(), configuration); if (fs.exists(path)) { fs.delete(path, true); } job.waitForCompletion(true); } }
五、运行结果
SubjectScore{subjectName='数学', score=1975} SubjectScore{subjectName='英语', score=875} SubjectScore{subjectName='语文', score=1812}