zoukankan      html  css  js  c++  java
  • Hadoop 学生平均成绩

    1.实例描述

      通过一个计算学生平均成绩的例子来讲解开发MapReduce程序的流程。输入文件都是纯文本文件,输入文件中的每行内容均为一个学生的姓名和他相应的成绩,如果有多门学科,则每门学科为一个文件。输出文件每行包含学生的姓名和平均成绩。下面给出样本输入文件,以及跑MapReduce程序过后的输出文件。代码亲测可用。注意:本人的开发环境是在Ubuntu+Eclipse下跑的。

      1)math

      2)china

      3)english

      4)输出文件

     2.程序代码

     1 import java.io.IOException;
     2 
     3 import org.apache.hadoop.conf.Configuration;
     4 import org.apache.hadoop.fs.Path;
     5 import org.apache.hadoop.io.DoubleWritable;
     6 import org.apache.hadoop.io.Text;
     7 import org.apache.hadoop.mapreduce.Job;
     8 import org.apache.hadoop.mapreduce.Mapper;
     9 import org.apache.hadoop.mapreduce.Reducer;
    10 import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
    11 import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
    12 import org.apache.hadoop.util.GenericOptionsParser;
    13 
    14 
    15 public class AvgScore {
    16 
    17     public static class Map extends Mapper<Object, Text, Text, DoubleWritable>{
    18         private static Text name = new Text();
    19         private static DoubleWritable score = new DoubleWritable();
    20         @Override
    21         protected void map(Object key, Text value,Mapper<Object, Text, Text, DoubleWritable>.Context context)
    22                 throws IOException, InterruptedException {
    23             // TODO Auto-generated method stub
    24             //  super.map(key, value, context);
    25             String[] splits = value.toString().split("	");    //  源文件一定要用tab键分割,不然会出错。
    26             if(splits.length!=2){
    27                 return ;
    28             }
    29             name.set(splits[0]);
    30             score.set(Double.parseDouble(splits[1]));
    31 //            System.out.println(name);
    32 //            System.out.println(score);
    33             context.write(name, score);
    34         }
    35     }
    36     
    37     public static class Reduce extends Reducer<Text, DoubleWritable, Text, DoubleWritable>{
    38         private static DoubleWritable avg = new DoubleWritable();
    39         @Override
    40         protected void reduce(Text name, Iterable<DoubleWritable> scores,Reducer<Text, DoubleWritable, Text, DoubleWritable>.Context context)
    41                 throws IOException, InterruptedException {
    42             // TODO Auto-generated method stub
    43             //   super.reduce(arg0, arg1, arg2);
    44             double sum = 0;
    45             int count = 0;
    46             for(DoubleWritable score:scores){
    47                 sum += score.get();
    48                 count ++;
    49             }
    50             avg.set(sum/count);
    51 //            System.out.println(avg);
    52             context.write(name, avg);
    53         }
    54     }
    55     public static void main(String[] args) throws IOException, ClassNotFoundException, InterruptedException {
    56         // TODO Auto-generated method stub
    57         Configuration conf = new Configuration();
    58         String[] otherArgs = new GenericOptionsParser(conf,args).getRemainingArgs();
    59         if(otherArgs.length!=2){
    60             System.out.println("Usage:Score Avg");
    61             System.exit(2);
    62         }
    63         Job job = new Job(conf,"Score Avg");
    64         job.setJarByClass(AvgScore.class);
    65         job.setMapperClass(Map.class);
    66         job.setReducerClass(Reduce.class);
    67         job.setMapOutputKeyClass(Text.class);
    68         job.setMapOutputValueClass(DoubleWritable.class);
    69         job.setOutputKeyClass(Text.class);
    70         job.setOutputValueClass(DoubleWritable.class);
    71         
    72         FileInputFormat.addInputPath(job, new Path(args[0]));
    73         FileOutputFormat.setOutputPath(job, new Path(args[1]));
    74         
    75         System.exit(job.waitForCompletion(true)?0:1);
    76     }
    77 
    78 }

    3.程序解释

      Mapper处理的数据是由InputFormat分解过的数据集,其中 InputFormat的作用是将数据集切割成小数据集InputSplit,每一个InputSlit将由一个Mapper负责处理。此 外,InputFormat中还提供了一个RecordReader的实现,并将一个InputSplit解析成<key,value>对提 供给了map函数。InputFormat的默认值是TextInputFormat,它针对文本文件,按行将文本切割成InputSlit,并用 LineRecordReader将InputSplit解析成<key,value>对,key是行在文本中的位置,value是文件中的 一行。

      Map的结果会通过partion分发到Reducer,Reducer做完Reduce操作后,将通过以格式OutputFormat输出。

      Mapper最终处理的结果对<key,value>,会送到Reducer中进行合并,合并的时候,有相同key的键/值对则送到同一个 Reducer上。Reducer是所有用户定制Reducer类地基础,它的输入是key和这个key对应的所有value的一个迭代器,同时还有 Reducer的上下文。Reduce的结果由Reducer.Context的write方法输出到文件中。

  • 相关阅读:
    fullCalendar改造计划之带农历节气节假日的万年历(转)
    Linked List Cycle
    Remove Nth Node From End of List
    Binary Tree Inorder Traversal
    Unique Binary Search Trees
    Binary Tree Level Order Traversal
    Binary Tree Level Order Traversal II
    Plus One
    Remove Duplicates from Sorted List
    Merge Two Sorted Lists
  • 原文地址:https://www.cnblogs.com/xiaoyh/p/9313713.html
Copyright © 2011-2022 走看看