博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
MapReduce最佳成绩统计,男生女生比比看
阅读量:7059 次
发布时间:2019-06-28

本文共 6098 字,大约阅读时间需要 20 分钟。

上一篇文章我们了解了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
{ /* * 调用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
{ public void reduce(Text key, Iterable
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
{ @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
{ @Override public void reduce(Text key, Iterable
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、运行效果

 

如果,您认为阅读这篇博客让您有些收获,不妨点击一下右下角的【推荐】。

如果,您希望更容易地发现我的新博客,不妨点击一下左下角的【关注我】。
如果,您对我的博客所讲述的内容有兴趣,请继续关注我的后续博客,我是【】。

本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。

实现代码及数据:

转载于:https://www.cnblogs.com/codeOfLife/p/5474115.html

你可能感兴趣的文章
配置eclipse使能打开当前文件所在目录
查看>>
Repeater内RadioButton.GroupName失效
查看>>
【算法学习笔记】17.暴力求解法05 隐式图搜索1 迭代加深搜索 埃及分数
查看>>
X-Frame-Options,X-XSS-Protection,X-Content-Type-Options
查看>>
每日一句(15)
查看>>
读书笔记--SQL必知必会--建立练习环境
查看>>
捕获、冒泡
查看>>
P3369 【模板】普通平衡树(Treap/SBT)
查看>>
托管代码的进程注入&CLR宿主
查看>>
SQL参数化查询的另一个理由——命中执行计划
查看>>
绕任意轴旋转
查看>>
iphone网络编程总结一
查看>>
window.open() target方法
查看>>
转 SVN中tag branch trunk用法详解
查看>>
springmvc.xml 放的位置
查看>>
HDU 1964 Pipes(插头DP)
查看>>
listview中item改变默认点击样式
查看>>
01-- sharepoint 2010开发概述
查看>>
在OSGI(felix)里整合hibernate
查看>>
ORACLE -- RAC OCFS连接产生的错误
查看>>