ZBLOG

使用MapReduce计算班级每个学生的最好成绩,输入文件路径为/user/test/input,请将计算后的结果输出到/user/test/output/目录下。 测试说明 输入文件在你每次点击评测的时候,平台会为你创建,无需你自己创建,只需要启动HDFS,编写java代码即可。 输入文件的数据格式如下: 张三 12 李四 13 张三 89 李四 92 ... 依照如上格式你应该输出: 张三 89 李四 92 import java.io.IOException; import ...

根据题目要求,需要对每个学生的最高成绩进行计算。因此,在 Mapper 阶段,可以将每个学生的姓名作为 key,成绩作为 value 输出。在 Reducer 阶段,则可以将相同姓名的所有成绩进行比较,取最大值作为该学生的最好成绩。

下面是修改后的代码:

import java.io.IOException; import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.fs.Path; import org.apache.hadoop.io.*; 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;

public class BestScore {

public static class ScoreMapper

   extends Mapper<LongWritable, Text, Text, IntWritable>{

private Text name = new Text();
private IntWritable score = new IntWritable();

public void map(LongWritable key, Text value, Context context
                ) throws IOException, InterruptedException {
  String[] tokens = value.toString().split(" ");
  name.set(tokens[0]);
  score.set(Integer.parseInt(tokens[1]));
  context.write(name, score);
}

}

public static class BestScoreReducer

   extends Reducer<Text,IntWritable,Text,IntWritable> {

private IntWritable result = new IntWritable();

public void reduce(Text key, Iterable<IntWritable> values,
                   Context context
                   ) throws IOException, InterruptedException {
  int bestScore = Integer.MIN_VALUE; // 初始化为最小值
  for (IntWritable val : values) {
    bestScore = Math.max(bestScore, val.get()); // 取最大值
  }
  result.set(bestScore);
  context.write(key, result);
}

}

public static void main(String[] args) throws Exception {

Configuration conf = new Configuration();
Job job = Job.getInstance(conf, "Best Score");
job.setJarByClass(BestScore.class);
job.setMapperClass(ScoreMapper.class);
job.setReducerClass(BestScoreReducer.class);
job.setOutputKeyClass(Text.class);
job.setOutputValueClass(IntWritable.class);

// 设置输入和输出路径
FileInputFormat.addInputPath(job, new Path("/user/test/input"));
FileOutputFormat.setOutputPath(job, new Path("/user/test/output"));

System.exit(job.waitForCompletion(true) ? 0 : 1); // 提交任务并等待完成

} }

需要注意的是,每个学生的最好成绩可能不止一个,因此在 Reducer 阶段需要将 values 中的所有值进行比较。同时,由于输出结果中包含了姓名和成绩两个信息,因此 key 类型为 Text,value 类型为 IntWritable。

本站部分文章来源于网络,版权归原作者所有,如有侵权请联系站长删除。
转载请注明出处:https://golang.0voice.com/?id=5009

分享:
扫描分享到社交APP
上一篇
下一篇
发表列表
游客 游客
此处应有掌声~
评论列表

还没有评论,快来说点什么吧~

联系我们

在线咨询: 点击这里给我发消息

微信号:3007537140

上班时间: 10:30-22:30

关注我们
x

注册

已经有帐号?