MapReduce最佳成绩统计,男生女生比比看
上一篇文章我们了解了MapReduce优化方面的知识,现在我们通过简单的项目,学会如何优化MapReduce性能
1、项目介绍
我们使用简单的成绩数据集,统计出0~20、20~50、50~100这三个年龄段的男、女学生的最高分数
2、数据集
姓名 年龄 性别 成绩
Alice 23 female 45
Bob 34 male 89
Chris 67 male 97
Kristine 38 female 53
Connor 25 male 27
Daniel 78 male 95
James 34 male 79
Alex 52 male 69
3、分析
基于需求,我们通过以下几步完成:
1、编写Mapper类,按需求将数据集解析为key=gender,value=name+age+score,然后输出
2、编写Partitioner类,按年龄段,将结果指定给不同的Reduce执行
3、编写Reducer类,分别统计出男女学生的最高分
4、编写run方法执行MapReduce作业
4、实现
package com.buaa;
import java.io.IOException; import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.conf.Configured;
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.Partitioner;
import org.apache.hadoop.mapreduce.Reducer;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
import org.apache.hadoop.util.Tool;
import org.apache.hadoop.util.ToolRunner; /**
* @ProjectName BestScoreCount
* @PackageName com.buaa
* @ClassName Gender
* @Description 统计不同年龄段内,男、女最高分数
* @Author 刘吉超
* @Date 2016-05-09 09:49:50
*/
public class Gender extends Configured implements Tool {
private static String TAB_SEPARATOR = "\t"; public static class GenderMapper extends Mapper<LongWritable, Text, Text, Text> {
/*
* 调用map解析一行数据,该行的数据存储在value参数中,然后根据\t分隔符,解析出姓名,年龄,性别和成绩
*/
public void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
/*
* 姓名 年龄 性别 成绩
* Alice 23 female 45
* 每个字段的分隔符是tab键
*/
// 使用\t,分割数据
String[] tokens = value.toString().split(TAB_SEPARATOR); // 性别
String gender = tokens[2];
// 姓名 年龄 成绩
String nameAgeScore = tokens[0] + TAB_SEPARATOR + tokens[1] + TAB_SEPARATOR + tokens[3]; // 输出 key=gender value=name+age+score
context.write(new Text(gender), new Text(nameAgeScore));
}
} /*
* 合并 Mapper输出结果
*/
public static class GenderCombiner extends Reducer<Text, Text, Text, Text> { public void reduce(Text key, Iterable<Text> values, Context context)throws IOException, InterruptedException {
int maxScore = Integer.MIN_VALUE;
int score = 0;
String name = " ";
String age = " "; for (Text val : values) {
String[] valTokens = val.toString().split(TAB_SEPARATOR);
score = Integer.parseInt(valTokens[2]);
if (score > maxScore) {
name = valTokens[0];
age = valTokens[1];
maxScore = score;
}
} context.write(key, new Text(name + TAB_SEPARATOR + age + TAB_SEPARATOR + maxScore));
}
} /*
* 根据 age年龄段将map输出结果均匀分布在reduce上
*/
public static class GenderPartitioner extends Partitioner<Text, Text> { @Override
public int getPartition(Text key, Text value, int numReduceTasks) {
String[] nameAgeScore = value.toString().split(TAB_SEPARATOR);
// 学生年龄
int age = Integer.parseInt(nameAgeScore[1]); // 默认指定分区 0
if (numReduceTasks == 0)
return 0; // 年龄小于等于20,指定分区0
if (age <= 20) {
return 0;
}else if (age <= 50) { // 年龄大于20,小于等于50,指定分区1
return 1 % numReduceTasks;
}else // 剩余年龄,指定分区2
return 2 % numReduceTasks;
}
} /*
* 统计出不同性别的最高分
*/
public static class GenderReducer extends Reducer<Text, Text, Text, Text> {
@Override
public void reduce(Text key, Iterable<Text> values, Context context) throws IOException, InterruptedException {
int maxScore = Integer.MIN_VALUE;
int score = 0;
String name = " ";
String age = " ";
String gender = " "; // 根据key,迭代 values集合,求出最高分
for (Text val : values) {
String[] valTokens = val.toString().split(TAB_SEPARATOR);
score = Integer.parseInt(valTokens[2]);
if (score > maxScore) {
name = valTokens[0];
age = valTokens[1];
gender = key.toString();
maxScore = score;
}
} context.write(new Text(name), new Text("age:" + age + TAB_SEPARATOR + "gender:" + gender + TAB_SEPARATOR + "score:" + maxScore));
}
} @SuppressWarnings("deprecation")
@Override
public int run(String[] args) throws Exception {
// 读取配置文件
Configuration conf = new Configuration(); Path mypath = new Path(args[1]);
FileSystem hdfs = mypath.getFileSystem(conf);
if (hdfs.isDirectory(mypath)) {
hdfs.delete(mypath, true);
} // 新建一个任务
Job job = new Job(conf, "gender");
// 主类
job.setJarByClass(Gender.class);
// Mapper
job.setMapperClass(GenderMapper.class);
// Reducer
job.setReducerClass(GenderReducer.class); // map 输出key类型
job.setMapOutputKeyClass(Text.class);
// map 输出value类型
job.setMapOutputValueClass(Text.class); // reduce 输出key类型
job.setOutputKeyClass(Text.class);
// reduce 输出value类型
job.setOutputValueClass(Text.class); // 设置Combiner类
job.setCombinerClass(GenderCombiner.class); // 设置Partitioner类
job.setPartitionerClass(GenderPartitioner.class);
// reduce个数设置为3
job.setNumReduceTasks(3); // 输入路径
FileInputFormat.addInputPath(job, new Path(args[0]));
// 输出路径
FileOutputFormat.setOutputPath(job, new Path(args[1])); // 提交任务
return job.waitForCompletion(true)?0:1;
} public static void main(String[] args) throws Exception {
String[] args0 = {
"hdfs://ljc:9000/buaa/gender/gender.txt",
"hdfs://ljc:9000/buaa/gender/out/"
};
int ec = ToolRunner.run(new Configuration(),new Gender(), args0);
System.exit(ec);
}
}
5、运行效果

MapReduce最佳成绩统计,男生女生比比看的更多相关文章
- 实训任务05 MapReduce获取成绩表的最高分记录
实训任务05 MapReduce获取成绩表的最高分记录 实训1:统计用户纺问次数 任务描述: 统计用户在2016年度每个自然日的总访问次数.原始数据文件中提供了用户名称与访问日期.这个任务就是要获取 ...
- 2016福州大学软件工程第二次团队作业——预则立&&他山之石成绩统计
第二次团队作业--预则立&&他山之石成绩统计结果如下: T:团队成绩 P:个人贡献比 T+P:折算个人成绩,计算公式为T+T/15*团队人数*P 学号 组别 Team P T+P 03 ...
- MapReduce实战:统计不同工作年限的薪资水平
1.薪资数据集 我们要写一个薪资统计程序,统计数据来自于互联网招聘hadoop岗位的招聘网站,这些数据是按照记录方式存储的,因此非常适合使用 MapReduce 程序来统计. 2.数据格式 我们使用的 ...
- sdut 3-5 学生成绩统计
3-5 学生成绩统计 Time Limit: 1000MS Memory limit: 65536K 题目描写叙述 通过本题目练习能够掌握对象数组的使用方法,主要是对象数组中数据的输入输出操作. 设计 ...
- (注意输入格式)bistuoj(旧)1237 成绩统计
成绩统计 Time Limit(Common/Java):1000MS/3000MS Memory Limit:65536KByteTotal Submit:88 ...
- 成绩统计程序(Java)
我的程序: package day20181018;/** * 成绩统计系统 * @author Administrator */import java.util.Scanner;//提供计算机直接扫 ...
- 体育成绩统计——20180801模拟赛T3
体育成绩统计 / Score 题目描述 正所谓“无体育,不清华”.为了更好地督促同学们进行体育锻炼,更加科学地对同学们进行评价,五道口体校的老师们在体育成绩的考核上可谓是煞费苦心.然而每到学期期末时, ...
- YTU 2798: 复仇者联盟之数组成绩统计
2798: 复仇者联盟之数组成绩统计 时间限制: 1 Sec 内存限制: 128 MB 提交: 136 解决: 96 题目描述 定义一个5行3列的二维数组,各行分别代表一名学生的高数.英语.C++ ...
- YTU 2769: 结构体--成绩统计
2769: 结构体--成绩统计 时间限制: 1 Sec 内存限制: 128 MB 提交: 1021 解决: 530 题目描述 建立一个简单的学生信息表,包括:姓名.性别.年龄及一门课程的成绩,统计 ...
随机推荐
- Day20 Django之Model多对多、中间件、缓存、信号和分页
一.Form补充 class IndexForm(forms.Form): # c = [ # (1, 'CEO'), # (2, 'CTO') # ] # 静态字段,属于IndexForm类,即使数 ...
- EntityFramework 和 linq 判断是否在指定时间段内的方法
EntityFramework: System.Data.Objects.EntityFunctions.DiffDays(DateTime.Now, inputTime)判断当前时间与指定时间相差多 ...
- Python抓取双色球数据
数据来源网站http://baidu.lecai.com/lottery/draw/list/50?d=2013-01-01 HTML解析器http://pythonhosted.org/pyquer ...
- ZeroBraneStudio之支持GBK文件编码
费了好大劲终于搞定了让ZBS支持打开GBK文件了.记录下过程: 看源码发现ZBS打开文件时会调用src\editor\commands.lua中的LoadFile函数,代码如下: local file ...
- hdu 1576 A/B 拓展欧几里得算法
A/B Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submiss ...
- nodejs服务
http://www.csser.com/board/4f55035305ee2e572400005e http://blog.fens.me/nodejs-server-forever/ http: ...
- DPI/PPI/dp/sp/px/pt 移动设计手册
转自DPI/PPI/dp/sp/px/pt 移动设计手册 做移动设计的同学,不管是原生app或者web app,应该对字体字号都是很头痛的问题.根本原因是,我们用唯一分辨率的电脑,设计各个不同尺寸大小 ...
- SQL Server 2008 数据库误删除数据的恢复
原文:SQL Server 2008 数据库误删除数据的恢复 原文:http://www.cnblogs.com/dudu/archive/2011/10/15/sql_server_recover_ ...
- 【HDOJ】2414 Chessboard Dance
简单DFS. /* 2414 */ #include <cstdio> #include <cstring> #include <cstdlib> ; ][]; i ...
- BZOJ2134: 单选错位
题目:http://www.lydsy.com/JudgeOnline/problem.php?id=2134 题解:因为每个答案之间是互不影响的,所以我们可以挨个计算. 假设当前在做 i 题目,如果 ...