SubjectScore
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;
}
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 String toString() {
return "SubjectScore{" +
"subjectName='" + subjectName + '\'' +
", 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();
}
}
MaxScoreMapper
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Mapper;
import java.io.IOException;
public class MaxScoreMapper extends Mapper<LongWritable, Text,Text,LongWritable> {
Text subject=new Text();
LongWritable score=new LongWritable();
@Override
protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
String[] split = value.toString().split(",");
subject.set(split[2]);
score.set(Integer.parseInt(split[3]));
context.write(subject,score);
}
}
MaxScoreReducer
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Reducer;
import java.io.IOException;
public class MaxScoreReducer extends Reducer<Text, LongWritable,Text,LongWritable> {
@Override
protected void reduce(Text key, Iterable<LongWritable> values, Context context) throws IOException, InterruptedException {
long max=0;
for(LongWritable longWritable:values){
if(longWritable.get()>max)
max=longWritable.get();
}
System.out.println(key+"最高成绩为"+max);
context.write(key,new LongWritable(max));
}
}
MinScoreReducer
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Reducer;
import java.io.IOException;
public class MaxScoreReducer extends Reducer<Text, LongWritable,Text,LongWritable> {
@Override
protected void reduce(Text key, Iterable<LongWritable> values, Context context) throws IOException, InterruptedException {
long min=100;
for(LongWritable longWritable:values){
if(longWritable.get()<min)
min=longWritable.get();
}
System.out.println(key+"最低成绩为"+min);
context.write(key,new LongWritable(min));
}
}
MaxScoreDriver
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.LongWritable;
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 MaxScoreDriver {
public static void main(String[] args) throws IOException, ClassNotFoundException, InterruptedException {
Configuration configuration = new Configuration();
Job job = Job.getInstance(configuration);
job.setJarByClass(MaxScoreDriver.class);
job.setMapperClass(MaxScoreMapper.class);
job.setMapOutputKeyClass(Text.class);
job.setMapOutputValueClass(LongWritable.class);
job.setReducerClass(MaxScoreReducer.class);
job.setOutputKeyClass(Text.class);
job.setOutputValueClass(LongWritable.class);
FileInputFormat.setInputPaths(job, new Path("E:\\hadoopstu\\in\\demo3\\stuscore.csv"));
Path path = new Path("E:\\hadoopstu\\in\\out8");
FileSystem fs = FileSystem.get(path.toUri(), configuration);
if (fs.exists(path))
fs.delete(path,true);
FileOutputFormat.setOutputPath(job, path);
job.waitForCompletion(true);
System.out.println("program run over");
}
}