结果展示:
zhouba 92
zhangsan 83
wangwu 75
lisi 71
zhengshi 69
wujiu 64
yanqi 58
zhaoliu 55
要排序的学生成绩文档:https://pan.baidu.com/s/1cOS_rqAmMg0ioo2dKXFHwQ
代码:
package com.it666.student;
import java.io.IOException;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.io.WritableComparable;
import org.apache.hadoop.io.WritableComparator;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.Mapper;
import org.apache.hadoop.mapreduce.Reducer;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
/**
* @author 吴振峰
* 编程夜当午,手握小滑鼠。
* 谁知编程辛,行行皆'心'苦。
*/
public class Core2 {
private static class ScoreA2Mapper extends Mapper<LongWritable, Text, IntWritable, Text>{
private IntWritable outputkey = new IntWritable();
private Text outputvalue = new Text();
@Override
protected void map(LongWritable key, Text value, Mapper<LongWritable, Text, IntWritable, Text>.Context context)
throws IOException, InterruptedException {
String line = value.toString();
String[] words = line.split("\\s+");
String name = words[0];
int score = Integer.parseInt(words[1]);
// 以成绩为 key,名字为 value 输出
outputkey.set(score);
outputvalue.set(name);
context.write(outputkey, outputvalue);
}
}
private static class ScoreA2SortComparator extends WritableComparator{
@SuppressWarnings("unused")
public ScoreA2SortComparator() {
super(IntWritable.class, true);
}
@SuppressWarnings("rawtypes")
public int compare(WritableComparable a, WritableComparable b) {
// 设置排序规则
// a, b 表示排序算法中需要比较的两个 key,通过返回值控制排序方式
// 返回值有 3 中情况
// 返回 0 排序结果是 ab
// 返回正数 排序结果是 ba
// 返回负数 排序结果是 ab
// 1. 把 Object 转换为 key 的类型
IntWritable akey = (IntWritable) a;
IntWritable bkey = (IntWritable) b;
// 2. 把 key 转换为方便使用的 int 类型
int aScore = akey.get();
int bScore = bkey.get();
// 3. 设置排序方式
if (aScore == bScore) {
return 0;
}else if(aScore < bScore) {
return 1;
}else {
return -1;
}
}
}
private static class ScoreA2Reducer extends Reducer<IntWritable, Text, Text, IntWritable>{
@Override
protected void reduce(IntWritable score, Iterable<Text> names,
Reducer<IntWritable, Text, Text, IntWritable>.Context context) throws IOException, InterruptedException {
for (Text name : names) {
context.write(name, score);
}
}
}
public static void main(String[] args) {
Configuration conf = new Configuration();
try {
Job job = Job.getInstance(conf);
job.setJarByClass(Core2.class);
job.setMapperClass(ScoreA2Mapper.class);
job.setReducerClass(ScoreA2Reducer.class);
// 配置使用自定义分排序方案:ScoreA2SortComparator
job.setSortComparatorClass(ScoreA2SortComparator.class);
job.setMapOutputKeyClass(IntWritable.class);
job.setMapOutputValueClass(Text.class);
job.setOutputKeyClass(Text.class);
job.setOutputValueClass(IntWritable.class);
Path inputPath = new Path("E:/java/MavenHadoopData/04-Students Grade/scoreA.txt");
FileInputFormat.addInputPath(job, inputPath);
Path outputPath = new Path("E:/java/MavenHadoopData/04-Students Grade/scoreA2");
FileSystem.get(conf).delete(outputPath, true);
FileOutputFormat.setOutputPath(job, outputPath);
job.waitForCompletion(true);
} catch (IOException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}