三、MapReduce练习----学生成绩降序排列

本文介绍了一种使用Hadoop进行学生成绩排序的方法,通过自定义Mapper、Reducer和Comparator类,实现了根据学生成绩从高到低的排序。代码详细展示了如何设置Job参数,包括输入输出路径、Mapper和Reducer类以及自定义排序比较器。
摘要由CSDN通过智能技术生成

结果展示:

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();
		}
	}
}


评论 7
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

kp6i9HIY4G7WPzH

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值