mapreduce计算平均值
2017-09-17 21:10
302 查看
当我们有每一位同学的每一科成绩时,我们计算他们的平均成绩,用传统的方法比较麻烦,如果我们用hadoop中MapReduce组件的话就比较简单了。
测试数据如下:
从上面的数据可以看到,计算每一位同学的平均成绩,在map阶段,我们可以用同学的姓名作为key,成绩作为value;在reduce阶段,key值相同的value值相加计算出总成绩,并且计算出科目的数量,然后用总成绩来除以科目数量就可以得出每一位同学的平均成绩了。
代码如下:
运行结果:
程序还是有没有考虑到的情况,比如成绩时小数的话,应该用double或float来定义变量,但是该程序只是提供一个算法思想。
测试数据如下:
从上面的数据可以看到,计算每一位同学的平均成绩,在map阶段,我们可以用同学的姓名作为key,成绩作为value;在reduce阶段,key值相同的value值相加计算出总成绩,并且计算出科目的数量,然后用总成绩来除以科目数量就可以得出每一位同学的平均成绩了。
代码如下:
import java.io.IOException; import java.util.Iterator; import java.util.StringTokenizer; import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.fs.Path; import org.apache.hadoop.io.IntWritable; 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; public class Socre { public static class Map extends Mapper<Object,Text,Text,IntWritable>{ public void map(Object key,Text value,Context context){ //把读入的每一行转换为String String line=value.toString(); //以“\n”切分数据 StringTokenizer tokenizer=new StringTokenizer(line,"\n"); while(tokenizer.hasMoreElements()){ //以空格切分数据 StringTokenizer tokenizerLine=new StringTokenizer(tokenizer.nextToken()); //获取姓名 String nameString=tokenizerLine.nextToken(); //获取成绩 String scoreString=tokenizerLine.nextToken(); int scoreInt=Integer.parseInt(scoreString); try { //key:姓名,value:成绩 context.write(new Text(nameString),new IntWritable(scoreInt)); } catch (IOException e) { e.printStackTrace(); } catch (InterruptedException e) { e.printStackTrace(); } } } } public static class Reduce extends Reducer<Text,IntWritable,Text,IntWritable>{ public void reduce(Text key,Iterable<IntWritable> values,Context context){ //计算给同学的总成绩 int sum=0; //获取科目数目 int count=0; Iterator<IntWritable> iterable=values.iterator(); while(iterable.hasNext()){ sum+=iterable.next().get(); count++; } int avg=(int)sum/count; try { context.write(key,new IntWritable(avg)); } catch (IOException e) { e.printStackTrace(); } catch (InterruptedException e) { e.printStackTrace(); } } } public static void main(String[] args) throws IllegalArgumentException, IOException, ClassNotFoundException, InterruptedException{ Configuration conf = new Configuration(); Job job = new Job(conf); job.setJarByClass(Socre.class); job.setJobName("avgscore1"); job.setMapperClass(Map.class); job.setReducerClass(Reduce.class); job.setOutputKeyClass(Text.class); job.setOutputValueClass(IntWritable.class); FileInputFormat.addInputPath(job, new Path("/avgscoreinput")); FileOutputFormat.setOutputPath(job, new Path("/avgscoreoutput")); job.waitForCompletion(true); } }
运行结果:
程序还是有没有考虑到的情况,比如成绩时小数的话,应该用double或float来定义变量,但是该程序只是提供一个算法思想。
相关文章推荐
- MapReduce编程基础(二)——数值概要(计算最大值、最小值、平均值)
- MapReduce功能实现七---小综合(多个job串行处理计算平均值)
- 计算3个整数的平均值---java
- Spark vs. MapReduce 时间节约66%,计算节约40%
- 使用MapReduce计算Pi
- Hadoop 稀疏矩阵乘法的MapReduce计算
- Ruby 计算数组中对象的属性的平均值
- 用结构计算N个有理数的平均值
- MapReduce端的二次排序以及对移动计算而不是移动数据的理解
- hadoop MapReduce分布式计算架构
- Hive的UDAF编程:计算几何平均值
- mapreduce计算分词权重
- 计算二叉树每层平均值
- php 计算任意多个结构相同的多维数组的每个元素的平均值
- hadoop-python:计算平均值分布式程序编写
- MapReduce求平均值
- 一维数组计算多个数的平均值
- 读取文件中的数据,并且计算其平均值和标准偏差
- 大规模文档相似度计算—基于MapReduce框架
- mapreduce编程:求平均值