需求

​ 背景:学校的学生的是一个非常大的生成数据的集体,比如每次考试的成绩

​ 现有一个班级的学生一个月的考试成绩数据。

​ 科目 姓名 分数

​ 需求:求出每门成绩中属于甲级的学生人数和总人数

​ 乙级的学生人数和总人数

​ 丙级的学生人数和总人数

​ 甲级(90及以上)乙级(80到89)丙级(0到79)

​ 处理数据结果:

                    甲级分区

​ 课程\t甲级\t学生1,学生2,...\t总人数

                    乙级分区

​ 课程\t乙级\t学生1,学生2,...\t总人数

                    丙级分区

​ 课程\t丙级\t学生1,学生2,...\t总人数

文档格式

English,liudehua,80
English,lijing,79
English,nezha,85
English,jinzha,60
English,muzha,71
English,houzi,99
English,libai,88
English,hanxin,66
English,zhugeliang,95
Math,liudehua,74
Math,lijing,72
Math,nezha,95
Math,jinzha,61
Math,muzha,37
Math,houzi,37
Math,libai,84
Math,hanxin,89
Math,zhugeliang,93
Computer,liudehua,54
Computer,lijing,73
Computer,nezha,86
Computer,jinzha,96
Computer,muzha,76
Computer,houzi,92
Computer,libai,73
Computer,hanxin,82
Computer,zhugeliang,100

代码示例

StuDriver
import org.apache.hadoop.io.Text;
import stuScore.JobUtils; public class StuDriver {
public static void main(String[] args) {
String[] paths = {"F:/stu_score.txt", "F:/output"}; JobUtils.commit(paths, true, 3, false, StuDriver.class,
StuMapper.class, Text.class, Text.class, null, StuPartitioner.class, StuReduce.class,
Text.class, Text.class); }
}
JobUtils
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat; import java.io.File;
import java.io.IOException; public class JobUtils {
private static Configuration conf; static {
conf = new Configuration();
} /**
* 提交job
*
* @param paths 输入输出路径数组
* @param isPartition 是否包含自定义分区类
* @param reduceNumber reduce数量(若自定义分区为true,则此项必须>=自定义分区数)
* @param isGroup 是否分组
* @param params 可变参数
*/
public static void commit(String[] paths, boolean isPartition, int reduceNumber, boolean isGroup, Class... params) {
try {
Job job = Job.getInstance(conf);
job.setJarByClass(params[0]); job.setMapperClass(params[1]);
job.setMapOutputKeyClass(params[2]);
job.setMapOutputValueClass(params[3]); if(isGroup) {
job.setGroupingComparatorClass(params[4]);
} if (isPartition) {
job.setPartitionerClass(params[5]);//设置自定义分区;
} if (reduceNumber > 0) {
job.setNumReduceTasks(reduceNumber);
job.setReducerClass(params[6]);
job.setOutputKeyClass(params[7]);
job.setOutputValueClass(params[8]);
} else {
job.setNumReduceTasks(0);
}
FileInputFormat.setInputPaths(job, new Path(paths[0]));
FileOutputFormat.setOutputPath(job, new Path(paths[1]));
job.waitForCompletion(true);
} catch (InterruptedException | ClassNotFoundException | IOException e) {
e.printStackTrace();
}
}
}
StuMapper
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Mapper; import java.io.IOException; public class StuMapper extends Mapper<LongWritable, Text, Text, Text> { Text k = new Text();
Text v = new Text(); @Override
protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
String line = value.toString();
String[] splits = line.split(",");
int score = Integer.parseInt(splits[2]);
String level;
if (score >= 90) {
level = "甲级";
} else if (score < 90 && score >= 80) {
level = "乙级";
} else {
level = "丙级";
}
k.set(splits[0] + "\t" + level);
v.set(splits[1]);
context.write(k, v);
}
}
StuReduce
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Reducer; import java.io.IOException; public class StuReduce extends Reducer<Text,Text,Text, Text> {
@Override
protected void reduce(Text key, Iterable<Text> values, Context context) throws IOException, InterruptedException {
StringBuilder builder = new StringBuilder();
int count =0;
for (Text v : values) {
builder.append(v+",");
count++;
}
builder.replace(builder.length()-1,builder.length(),"\t");
builder.append(count);
context.write(key,new Text(builder.toString()));
}
}
StuPartitioner
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Partitioner; public class StuPartitioner extends Partitioner<Text, Text> {
@Override
public int getPartition(Text text, Text text2, int i) {
String line = text.toString();
if(line.contains("甲级")){
return 0;
}else if(line.contains("乙级")){
return 1;
}else{
return 2;
}
}
}
输出结果

【Hadoop】MapReduce练习:分科目等级并按分区统计学生以及人数的更多相关文章

  1. Hadoop MapReduce编程 API入门系列之分区和合并(十四)

    不多说,直接上代码. 代码 package zhouls.bigdata.myMapReduce.Star; import java.io.IOException; import org.apache ...

  2. Hadoop MapReduce编程 API入门系列之薪水统计(三十一)

    不多说,直接上代码. 代码 package zhouls.bigdata.myMapReduce.SalaryCount; import java.io.IOException; import jav ...

  3. Hadoop Mapreduce分区、分组、二次排序过程详解[转]

    原文地址:Hadoop Mapreduce分区.分组.二次排序过程详解[转]作者: 徐海蛟 教学用途 1.MapReduce中数据流动   (1)最简单的过程:  map - reduce   (2) ...

  4. Hadoop mapreduce自定义分区HashPartitioner

    本文发表于本人博客. 在上一篇文章我写了个简单的WordCount程序,也大致了解了下关于mapreduce运行原来,其中说到还可以自定义分区.排序.分组这些,那今天我就接上一次的代码继续完善实现自定 ...

  5. Hadoop MapReduce执行过程详解(带hadoop例子)

    https://my.oschina.net/itblog/blog/275294 摘要: 本文通过一个例子,详细介绍Hadoop 的 MapReduce过程. 分析MapReduce执行过程 Map ...

  6. Hadoop MapReduce 二次排序原理及其应用

    关于二次排序主要涉及到这么几个东西: 在0.20.0 以前使用的是 setPartitionerClass setOutputkeyComparatorClass setOutputValueGrou ...

  7. 三种方法实现Hadoop(MapReduce)全局排序(1)

    我们可能会有些需求要求MapReduce的输出全局有序,这里说的有序是指Key全局有序.但是我们知道,MapReduce默认只是保证同一个分区内的Key是有序的,但是不保证全局有序.基于此,本文提供三 ...

  8. hadoop MapReduce

    简单介绍 官方给出的介绍是hadoop MR是一个用于轻松编写以一种可靠的.容错的方式在商业化硬件上的大型集群上并行处理大量数据的应用程序的软件框架. MR任务通常会先把输入的数据集切分成独立的块(可 ...

  9. Hadoop Mapreduce 案例 wordcount+统计手机流量使用情况

    mapreduce设计思想 概念:它是一个分布式并行计算的应用框架它提供相应简单的api模型,我们只需按照这些模型规则编写程序,即可实现"分布式并行计算"的功能. 案例一:word ...

随机推荐

  1. CSS基础学习 17.CSS动画

  2. web form 服务器控件表单验证

    <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="ValidationDemo ...

  3. HDU 6060 - RXD and dividing | 2017 Multi-University Training Contest 3

    /* HDU 6060 - RXD and dividing [ 分析,图论 ] | 2017 Multi-University Training Contest 3 题意: 给一个 n 个节点的树, ...

  4. vue中使用v-chart改变柱状图颜色以及X轴Y轴的文字颜色和大小以及标题

    1.html部分 <ve-histogram :tooltip-visible="true" :x-axis="xAxis" :y-axis=" ...

  5. i3wm脚本

    exec 执行命令 --no-startup-id 有些脚本或者程序不支持启动通知,不加命令,鼠标会长时间空转,60秒左右 exec_always 每次重启i3,使用该命令启动的程序都会重新执行一次, ...

  6. MySQL多表查询总结

    MySQL术语: Redundacncy(冗余):存储两次或多次数据,以便实现快速查询. Primary Key(主键):主键是唯一的.表中每条记录的唯一标识. Foreign Key(外键):用于连 ...

  7. Cogs 746. [网络流24题] 骑士共存(最大独立集)

    [网络流24题] 骑士共存 ★★☆ 输入文件:knight.in 输出文件:knight.out 简单对比 时间限制:1 s 内存限制:128 MB 骑士共存问题 «问题描述: 在一个n*n个方格的国 ...

  8. Makefile规则介绍

    Makefile 一个规则 三要素:目标,依赖,命令     目标:依赖 命令 1.第一条规则是用来生成终极目标的规则     如果规则中的依赖不存在,向下寻找其他的规则 更新机制:比较的是目标文件和 ...

  9. 前端导出pdf

    html2canvas文档地址 http://html2canvas.hertzen.com/configuration 方式一:使用html2canvas和jspdf插件实现 该方式是通过html2 ...

  10. The Road to SDN: An Intellectual History of Programmable Networks

    文章名称:The Road to SDN: An Intellectual History of Programmable Networks 文章来源:Feamster N , Rexford J , ...