资讯

精准传达 • 有效沟通

从品牌网站建设到网络营销策划,从策略到执行的一站式服务

Hadoop如何实现求平均成绩

这篇文章主要介绍Hadoop如何实现求平均成绩,文中介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们一定要看完!

创新互联公司专业为企业提供博罗网站建设、博罗做网站、博罗网站设计、博罗网站制作等企业网站建设、网页设计与制作、博罗企业网站模板建站服务,十多年博罗做网站经验,不只是建网站,更提供有价值的思路和整体网络服务。

 
//思路根据hadoop原理归并相同人名,以人名为key,以各科成绩为value容器元素,计算容器值的和,除以科目数。
public class AverageScore {
public static class TokenizerMapper extends Mapper{
 
 private Text word = new Text();
   
 public void map(Object key, Text value, Context context) throws IOException, InterruptedException {
   //按照行分割
  StringTokenizer line = new StringTokenizer(value.toString(),"\n");
   
   while (line.hasMoreElements()) {
      //按照空格分割
      StringTokenizer lineBlock = new StringTokenizer(line.nextToken());
   String stuName = lineBlock.nextToken();
   int stuScore = Integer.parseInt(lineBlock.nextToken());  
         word.set(stuName);
         context.write(word, new IntWritable(stuScore));
   }
 }
}
public static class IntSumReducer extends Reducer {
 private IntWritable result = new IntWritable();
 public void reduce(Text key, Iterable values, Context context) throws IOException, InterruptedException {
   int sum = 0;
   int count =0;
   while(values.iterator().hasNext()){
    sum+=values.iterator().next().get();
    count++;
   }
   int average = sum/count;
   result.set(average);
   context.write(key, result);
 }
}
public static void main(String[] args) throws Exception {
 Configuration conf = new Configuration();
 String[] otherArgs = new GenericOptionsParser(conf, args).getRemainingArgs();
 if (otherArgs.length != 2) {
   System.err.println("Usage: wordcount  ");
   System.exit(2);
 }
 Job job = new Job(conf, "word count");
 job.setJarByClass(AverageScore.class);
 job.setMapperClass(TokenizerMapper.class);
 job.setCombinerClass(IntSumReducer.class);
 job.setReducerClass(IntSumReducer.class);
 job.setOutputKeyClass(Text.class);
 job.setOutputValueClass(IntWritable.class);
 FileInputFormat.addInputPath(job, new Path(otherArgs[0]));
 FileOutputFormat.setOutputPath(job, new Path(otherArgs[1]));
 System.exit(job.waitForCompletion(true) ? 0 : 1);
}
}

以上是“Hadoop如何实现求平均成绩”这篇文章的所有内容,感谢各位的阅读!希望分享的内容对大家有帮助,更多相关知识,欢迎关注创新互联行业资讯频道!


网站题目:Hadoop如何实现求平均成绩
标题链接:http://cdkjz.cn/article/piiieg.html
多年建站经验

多一份参考,总有益处

联系快上网,免费获得专属《策划方案》及报价

咨询相关问题或预约面谈,可以通过以下方式与我们联系

大客户专线   成都:13518219792   座机:028-86922220