package com.guang.task23;
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.Text;
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;
import java.io.IOException;
public class Problem1 {
public static void main(String[] args) throws IOException, InterruptedException, ClassNotFoundException {
// 1.获取配置信息
Configuration conf = new Configuration();
// 2.获取job对象
Job job = Job.getInstance(conf);
// 3.关联Driver类的jar
job.setJarByClass(Problem1.class);
// 4.关联Mapper和Reducer的jar
job.setMapperClass(WordCountMapper.class);
job.setReducerClass(WordCountReducer.class);
// 5.设置Mapper输出的kv类型
job.setMapOutputKeyClass(Text.class);
job.setMapOutputValueClass(MyKey.class);
// 6.设置最终输出的kv类型
job.setOutputKeyClass(Text.class);
job.setOutputValueClass(MyKey.class);
String inputPath = "MapReduce/src/main/java/com/guang/task23/input";
String outputPath = "MapReduce/src/main/java/com/guang/task23/output";
FileSystem fs = FileSystem.get(conf);
Path p = new Path(outputPath);
if (fs.exists(p)) {
fs.delete(p, true);
}
// 7.设置输入和输出路径
FileInputFormat.setInputPaths(job, new Path(inputPath));
FileOutputFormat.setOutputPath(job, new Path(outputPath));
// 8.提交job
System.exit(job.waitForCompletion(true) ? 0 : 1);
}
public static class WordCountMapper extends Mapper<LongWritable, Text, Text, MyKey> {
@Override
protected void map(LongWritable key, Text value, Mapper<LongWritable, Text, Text, MyKey>.Context context) throws IOException, InterruptedException {
String line = value.toString();
String[] words = line.split(",");
context.write(new Text(words[1]), new MyKey(words[0], Integer.parseInt(words[2])));
}
}
public static class WordCountReducer extends Reducer<Text, MyKey, Text, MyKey> {
@Override
protected void reduce(Text key, Iterable<MyKey> values, Reducer<Text, MyKey, Text, MyKey>.Context context) throws IOException, InterruptedException {
MyKey outV = null;
int grade = Integer.MIN_VALUE;
for (MyKey value : values) {
if (value.getGrade() > grade) {
outV = new MyKey(value.getSno(), value.getGrade());
grade = value.getGrade();
}
}
context.write(key, outV);
}
}
}
MapReduce自定义Key实现获取学生最高成绩 课程设计
需积分: 1 182 浏览量
2024-01-06
21:57:14
上传
评论
收藏 1.5MB ZIP 举报
海洋之心
- 粉丝: 9w+
- 资源: 91
最新资源
- WS2-32.lib,在编译程序中可以链接使用
- 秒懂傅里叶变换matlab程序实现过程
- ZEND解密dezender12
- sony 索尼IMX334摄像头模组电路板AD版硬件PCB图(6层板).zip
- 基于flask和echarts融合交易策略的bitfinex可视化微服务.zip
- 包含了wvp-assist.tar wvp-talk.tar zlmediakit.tar .
- 3r4efgh53wgrf43tw
- 2024新版Java基础从入门到精通全套视频+资料下载
- Spring AI大模型视频教程+ChatGPT视频教程+OpenAI大模型视频教程(资料+视频教程)
- ABB工业机器人教程PDF版本
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈