MapReduce--平均分,最高,低分以及及格率的计算

计算班级的平均分,以及个人的最高最低分,以及每个班级的及格率。

来先看一下我的数据。

时间			班级		姓名		科目			成绩
20180501 1708a1 li bishi 80
20180501 1708a1 li jishi 55
20180501 1708a1 li project 90
20180501 1708a1 li2 bishi 80
20180501 1708a1 li2 jishi 20
20180501 1708a1 li2 project 90
20180501 1708a1 li3 bishi 50
20180501 1708a1 li3 jishi 70
20180501 1708a1 li3 project 60
20180501 1708a1 zhangsan bishi 88
20180501 1708a1 zhangsan jishi 55
20180501 1708a1 zhangsan project 98
20180501 1708a1 lishi bishi 18
20180501 1708a1 lishi jishi 15
20180501 1708a1 lishi project 15
20180501 1708a1 wangwu bishi 88
20180501 1708a1 wangwu jishi 76
20180501 1708a1 wangwu project 70
20180501 1708a2 li1 bishi 80
20180501 1708a2 li1 jishi 71
20180501 1708a2 li1 project 96
20180501 1708a2 li2 bishi 80
20180501 1708a2 li2 jishi 26
20180501 1708a2 li2 project 90
20180501 1708a2 li3 bishi 80
20180501 1708a2 li3 jishi 55
20180501 1708a2 li3 project 90
20180501 1708a2 zhangliang bishi 81
20180501 1708a2 zhangliang jishi 55
20180501 1708a2 zhangliang project 98
20180501 1708a2 liuli bishi 70
20180501 1708a2 liuli jishi 95
20180501 1708a2 liuli project 75
20180501 1708a2 wangwu bishi 80
20180501 1708a2 wangwu jishi 76
20180501 1708a2 wangwu project 70
20180501 1708a2 zhangxi bishi 18
20180501 1708a2 zhangxi jishi 16
20180501 1708a2 zhangxi project 10

数据之间是空格。。。。

代码来了 -- 平均分,最高分,最低分

package com.huhu.day01;

import java.io.IOException;

import org.apache.hadoop.conf.Configuration;
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; /**
* 切割文本: 平均分,最高低分
*
* @author huhu_k
*
*/
public class HomeWork2 { // map
public static class MyMapper extends Mapper<LongWritable, Text, Text, Text> {
Text keys = new Text();
Text values = new Text(); @Override
protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
// 数据切割方式(文本中的内容)
// 按行分
String[] line = value.toString().split(" ");
keys.set(line[0] + ":" + line[2]);
values.set(line[3] + ":" + line[4]);
context.write(keys, values);
}
} // reduce
public static class MyReducer extends Reducer<Text, Text, Text, Text> { @Override
protected void reduce(Text key, Iterable<Text> value, Context context)
throws IOException, InterruptedException {
int max = Integer.MIN_VALUE;
int min = Integer.MAX_VALUE;
// 和
int sum = 0;
// 人数
int count = 0;
// 分数
int score = 0;
String classs = "";
for (Text t : value) {
classs = t.toString().split(":")[0];
score = Integer.parseInt(t.toString().split(":")[1]);
if (max < score)
max = score;
if (min > score)
min = score;
switch (classs) {
case "bishi":
score += score * 0.4;
break;
case "jishi":
score += score * 0.3;
break;
case "project":
score += score * 0.3;
break;
}
sum += score;
count++;
}
int avg = (int) sum / count;
String[] student = key.toString().split(":");
Text ky = new Text(student[0] + "\t" + student[1]);
context.write(ky, new Text("平均分 " + avg));
context.write(ky, new Text("最高值为 " + max));
context.write(ky, new Text("最低值 " + min));
} } public static void main(String[] args) throws Exception { // 配置容器
Configuration conf = new Configuration();
// 创建一个job
@SuppressWarnings("deprecation")
Job job = new Job(conf, "MyMapReduce Two");
// 配置job
job.setJarByClass(HomeWork2.class);
job.setMapperClass(MyMapper.class);
job.setMapOutputKeyClass(Text.class);
job.setMapOutputValueClass(Text.class); job.setReducerClass(MyReducer.class);
job.setOutputKeyClass(Text.class);
job.setOutputValueClass(Text.class); // 输入输出
FileInputFormat.addInputPath(job, new Path(args[0]));
FileOutputFormat.setOutputPath(job, new Path(args[1])); // 执行程序
boolean waitForCompletion = job.waitForCompletion(true);
System.exit(waitForCompletion ? 0 : 1); } }

运行结果:

2.及格率

package com.huhu.day01;

import java.io.IOException;
import java.text.DecimalFormat;
import java.util.HashMap;
import java.util.Map; import org.apache.hadoop.conf.Configuration;
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; /**
* 切割文本:及格率
*
* @author huhu_k
*
*/
public class HomeWork3 { // map
public static class MyMapper extends Mapper<LongWritable, Text, Text, Text> {
Text keys = new Text();
Text values = new Text(); @Override
protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
// 数据切割方式(文本中的内容)
// 按行分
String[] line = value.toString().split(" ");
keys.set(line[0] + ":" + line[1]);
context.write(keys, value);
}
} // reduce
public static class MyReducer extends Reducer<Text, Text, Text, Text> {
Map<String, Double> map = new HashMap<>();
Map<String, String> maps = new HashMap<>(); @Override
protected void reduce(Text key, Iterable<Text> value, Context context)
throws IOException, InterruptedException {
for (Text t : value) {
String[] values = t.toString().split(" ");
String student = values[2] + ":" + values[0] + ":" + values[1];
String subject = values[3];
double score = Integer.valueOf(values[4]);
if ("bishi".equals(subject)) {
score *= 0.4;
} else {
score *= 0.3;
}
// 如果map中有学生,累加学生的没门课程的分数
if (map.containsKey(student)) {
double scores = map.get(student);
scores += score;
map.put(student, scores);
} else {
// 第一次进入时不包含,则直接添加
map.put(student, score);
}
} for (Map.Entry<String, Double> m : map.entrySet()) {
String classname = m.getKey().split(":")[2];
Double score = m.getValue();
if (maps.containsKey(classname) && score >= 60) {
String k = Integer.parseInt(maps.get(classname).split(":")[0]) + 1 + "";
String v = Integer.parseInt(maps.get(classname).split(":")[1]) + 1 + "";
maps.put(classname, k + ":" + v);
} else if (maps.containsKey(classname) && score < 60) {
String k = Integer.parseInt(maps.get(classname).split(":")[0]) + 1 + "";
String v = Integer.parseInt(maps.get(classname).split(":")[1]) + "";
maps.put(classname, k + ":" + v);
} else if (!maps.containsKey(classname) && score < 60) {
maps.put(classname, "1:0");
} else if (!maps.containsKey(classname) && score >= 60) {
maps.put(classname, "1:1");
}
} } @Override
protected void cleanup(Reducer<Text, Text, Text, Text>.Context context)
throws IOException, InterruptedException {
for (Map.Entry<String, String> m : maps.entrySet()) {
DecimalFormat d = new DecimalFormat("0.00%");
double pass = Double.valueOf(m.getValue().split(":")[1]) / Double.valueOf(m.getValue().split(":")[0]);
context.write(new Text(m.getKey()), new Text("及格率为:" + d.format(pass)));
}
}
} public static void main(String[] args) throws Exception { // 配置容器
Configuration conf = new Configuration();
// 创建一个job
@SuppressWarnings("deprecation")
Job job = new Job(conf, "MyMapReduce Count");
// 配置job
job.setJarByClass(HomeWork3.class);
job.setMapperClass(MyMapper.class);
job.setMapOutputKeyClass(Text.class);
job.setMapOutputValueClass(Text.class); job.setReducerClass(MyReducer.class);
job.setOutputKeyClass(Text.class);
job.setOutputValueClass(Text.class); // 输入输出
FileInputFormat.addInputPath(job, new Path(args[0]));
FileOutputFormat.setOutputPath(job, new Path(args[1])); // 执行程序
boolean waitForCompletion = job.waitForCompletion(true);
System.exit(waitForCompletion ? 0 : 1); } }

MapReduce一个分布式并行离线计算框架。我们只需要知道map(),reduce(),input,output,剩下的由框架完成

基于yarn的工作流程

MapReduce--平均分,最高,低分以及及格率的计算的更多相关文章

  1. MapReduce Input Split 输入分/切片

    MapReduce Input Split(输入分/切片)详解 public static long getMaxSplitSize(JobContext context) { return cont ...

  2. mysql计算时间差-本例为计算分钟差然后/60计算小时保留一位小数,由于直接得小时只会取整

    -- ORDER_TIME datetime NOT NULL(字段类型)SELECTso.`ID`,so.`ORDER_TIME`,NOW(),CONCAT(ROUND(TIMESTAMPDIFF( ...

  3. sql面试50题------(11-20)

    文章目录 11.查询至少有一门课与学号为'01'的学生所学课程相同的学生的学号和姓名 12.查询和'01'号同学所学课程完全相同的其他同学的学号 13.查询两门及其以上不及格课程的同学的学号,姓名及其 ...

  4. MapReduce原理与设计思想

    简单解释 MapReduce 算法 一个有趣的例子 你想数出一摞牌中有多少张黑桃.直观方式是一张一张检查并且数出有多少张是黑桃? MapReduce方法则是: 给在座的所有玩家中分配这摞牌 让每个玩家 ...

  5. MapReduce: 一种简化的大规模集群数据处理法

    (只有文字没有图,图请参考http://research.google.com/archive/mapreduce.html) MapReduce: 一种简化的大规模集群数据处理法 翻译:风里来雨里去 ...

  6. MapReduce极简教程

    一个有趣的例子 你想数出一摞牌中有多少张黑桃.直观方式是一张一张检查并且数出有多少张是黑桃?   MapReduce方法则是: 给在座的所有玩家中分配这摞牌 让每个玩家数自己手中的牌有几张是黑桃,然后 ...

  7. 大数据 --> MapReduce原理与设计思想

    MapReduce原理与设计思想 简单解释 MapReduce 算法 一个有趣的例子:你想数出一摞牌中有多少张黑桃.直观方式是一张一张检查并且数出有多少张是黑桃? MapReduce方法则是: 给在座 ...

  8. Hadoop(17)-MapReduce框架原理-MapReduce流程,Shuffle机制,Partition分区

    MapReduce工作流程 1.准备待处理文件 2.job提交前生成一个处理规划 3.将切片信息job.split,配置信息job.xml和我们自己写的jar包交给yarn 4.yarn根据切片规划计 ...

  9. 转:MapReduce原理与设计思想

    转自:http://www.cnblogs.com/wuyudong/p/mapreduce-principle.html 简单解释 MapReduce 算法 一个有趣的例子 你想数出一摞牌中有多少张 ...

随机推荐

  1. 批处理bat标准化获取当前系统日期的几种方法,补0

    首先有两个推荐的方案. 一: for /f "tokens=2 delims==" %%a in ('wmic path win32_operatingsystem get Loc ...

  2. Gym 100247I Meteor Flow(优先队列)

    https://vjudge.net/problem/Gym-100247I 题意:有一艘飞船,现在有n颗流星坠落会攻击到飞船,每颗流星会在t时刻降落,对飞船造成d的伤害,飞船会有一个保护盾,初始值为 ...

  3. Codeforces 600E. Lomsat gelral(Dsu on tree学习)

    题目链接:http://codeforces.com/problemset/problem/600/E n个点的有根树,以1为根,每个点有一种颜色.我们称一种颜色占领了一个子树当且仅当没有其他颜色在这 ...

  4. 【BZOJ】1832: [AHOI2008]聚会

    题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=1832 省选出出了CF的感觉..... 显然一发贪心,如果两个点显然就是他们的$LCA$(不 ...

  5. git 修改文件夹名字后如何提交

    将文件夹名字从 v1.0.1 修改为 v1.0.2 git add --ignore-removal "v1.0.2/xsxsx"

  6. Android开发代码规范总结

    本篇开始总结Android开发中的一些注意事项,提高代码质量(仅供参考): 1.  Activity间的数据通信,对于数据量比较大的,避免使用 Intent + Parcelable 的方式,可以考虑 ...

  7. Centos 7系统挂载NTFS格式移动硬盘

    有些时候做大数据量迁移时,为了快速迁移大数据,有可能在Linux服务器上临时挂载NTFS格式的移动硬盘, 一般情况下,linux是识别不了NTFS格式移动硬盘的(需要重编译Linux核心才能,加挂NT ...

  8. Axure 动态面板的状态与尺寸

    知识点: 1.动态面板的状态下相当动态面版的模式,当符合某种条件的话就选择对应的动态面板的状态 2.动态面板的尺寸可以根据内容自动调整大小 本节教程主要分为一下图片两种场景: 出现动态面板中的两种.一 ...

  9. python爬虫学习(三):使用re库爬取"淘宝商品",并把结果写进txt文件

    第二个例子是使用requests库+re库爬取淘宝搜索商品页面的商品信息 (1)分析网页源码 打开淘宝,输入关键字“python”,然后搜索,显示如下搜索结果 从url连接中可以得到搜索商品的关键字是 ...

  10. HTML第七章总结

    Getting started with CSS 前言 CSS 的 rule 作者做了一个非常形象的比喻,将 CSS 必做 renovate the house,在这里,CSS 包括了三个部分: Se ...