• mao/reduce实现求平均值


     1 import java.io.*;
     2 import java.util.*;
     3 
     4 import org.apache.hadoop.fs.Path;
     5 import org.apache.hadoop.io.*;
     6 import org.apache.hadoop.mapreduce.*;
     7 import org.apache.hadoop.mapreduce.lib.output.*;
     8 import org.apache.hadoop.mapreduce.lib.input.*;
     9 import org.apache.hadoop.conf.*;
    10 import org.apache.hadoop.util.*;
    11 public class Score_Process extends Configured implements Tool {
    12     /**
    13      * 程序说明:主要用来实现计算学生的平均成绩。
    14      * 数据输入:文件形式输入,每一行包含学生姓名 学生成绩。一个学生有多门成绩则有多行。例如:张三 98
    15      * 数据输出:张三 84 学生姓名 学生平均成绩
    16      * 实现思路:在map阶段<张三,(98,68,……)>
    17      * **/
    18     
    19     public static class Map extends Mapper<LongWritable,Text,Text,IntWritable>{
    20         public void map(LongWritable key,Text value,Context context)throws IOException,InterruptedException{
    21             String line=value.toString();
    22             System.out.println(line);//测试
    23             StringTokenizer tokenizer=new StringTokenizer(line);
    24             while(tokenizer.hasMoreTokens()){
    25                 String name=tokenizer.nextToken();
    26                 String strscore=tokenizer.nextToken();
    27                 int intscore=Integer.parseInt(strscore);
    28                 context.write(new Text(name), new IntWritable(intscore));
    29             }
    30             
    31         }
    32         
    33     }
    34     
    35     public static class Reduce extends Reducer<Text,IntWritable,Text,IntWritable>{
    36         public void reduce(Text key,Iterable<IntWritable>values,Context context) throws IOException, InterruptedException{
    37             int sun=0,count=0;
    38             for(IntWritable val:values){
    39                 sun+=val.get();
    40                 count++;
    41             }
    42             int averscore=(int)sun/count;
    43             context.write(key, new IntWritable(averscore));
    44         }
    45         
    46     }
    47     
    48     public int run(String[] args) throws Exception{
    49         Job job=new Job(getConf());
    50         job.setJarByClass(Score_Process.class);
    51         job.setJobName("Score_Process");
    52         
    53         job.setOutputKeyClass(Text.class);
    54         job.setOutputValueClass(IntWritable.class);
    55         
    56         job.setMapperClass(Map.class);
    57         //job.setCombinerClass(Reduce.class);
    58         job.setReducerClass(Reduce.class);
    59         
    60         job.setInputFormatClass(TextInputFormat.class);
    61         job.setOutputFormatClass(TextOutputFormat.class);
    62         
    63         FileInputFormat.setInputPaths(job, new Path(args[0]));
    64         FileOutputFormat.setOutputPath(job, new Path(args[1]));
    65         boolean success=job.waitForCompletion(true);
    66         
    67         return success?0:1;
    68     }
    69     
    70     public static void main(String[] args)throws Exception{
    71         int ret=ToolRunner.run(new Score_Process(), args);
    72         System.exit(ret);
    73     }
    74 
    75 }
  • 相关阅读:
    深入浅出 JVM ClassLoader
    JVM 线上故障排查基本操作
    深入浅出 JVM GC(3)
    MyBatis 源码分析系列文章合集
    MyBatis 源码分析
    MyBatis 源码分析
    MyBatis 源码分析
    MyBatis 源码分析
    MyBatis 源码分析
    MyBatis 源码分析系列文章导读
  • 原文地址:https://www.cnblogs.com/6tian/p/3829186.html
Copyright © 2020-2023  润新知