wordcount 學生成績普通版

package com.Practice.StudentScores;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IntWritable;
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.Reducer;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * 題目:學生成績普通版
 *
 *   computer,huangxiaoming,85
     computer,xuzheng,54
     computer,huangbo,86
     computer,liutao,85
     computer,huanglei,99
     computer,liujialing,85
     computer,liuyifei,75
     computer,huangdatou,48
     computer,huangjiaju,88
     computer,huangzitao,85
     english,zhaobenshan,57
     english,liuyifei,85
     english,liuyifei,76
     english,huangdatou,48
     english,zhouqi,85
     english,huangbo,85
     english,huangxiaoming,96
     english,huanglei,85
     english,liujialing,75
     algorithm,liuyifei,75
     algorithm,huanglei,76
     algorithm,huangjiaju,85
     algorithm,liutao,85
     algorithm,huangdou,42
     algorithm,huangzitao,81
     math,wangbaoqiang,85
     math,huanglei,76
     math,huangjiaju,85
     math,liutao,48
     math,xuzheng,54
     math,huangxiaoming,85
     math,liujialing,85
 *
 * 1、每一個course的最高分,最低分,平均分
 返回結果格式:
 course max=95  min=22  avg=55
 例子:
 computer   max=99  min=48  avg=75

 解題思路:在map以course作爲key值,其餘部分作爲value,在reduce中設置變量max,min,avg,通過累計求出,並設置格式

 2、求該成績表當中出現了相同分數的分數,還有次數,以及該分數的人
 返回結果的格式:
 科目 分數  次數  該分數的人
 例子:
 computer   85  3   huangzitao,liujialing,huangxiaoming

 解題思路:求某科目中出現系統分數的人數以及分數,map以科目和分數作爲key值,進行分組,在reduce中進行計數,當計數結果大於1時,輸出分數,人數和人名
 */
public class StudentScores1 {

    public static void main(String[] args) throws IOException, ClassNotFoundException, InterruptedException {
        Configuration conf = new Configuration();
        FileSystem fs = FileSystem.get(conf);

        Job job = Job.getInstance(conf);
        job.setJar("wordcountJar/wordcount.jar");
//        job.setMapperClass(StudentScoresMapper.class);
//        job.setReducerClass(StudentScoresReducer.class);
        job.setMapperClass(StudentScoresMapper2.class);
        job.setReducerClass(StudentScoresReducer2.class);

//        job.setOutputKeyClass(Text.class);
//        job.setOutputValueClass(IntWritable.class);
        job.setOutputKeyClass(Text.class);
        job.setOutputValueClass(Text.class);

        Path inputPath = new Path("input/studentScores");
        Path outputPath = new Path("output/studentScores");

        if(fs.isDirectory(outputPath)){
            fs.delete(outputPath,true);
        }

        FileInputFormat.setInputPaths(job,inputPath);
        FileOutputFormat.setOutputPath(job,outputPath);

        Boolean waitForCompletion = job.waitForCompletion(true);
        System.exit(waitForCompletion ? 0 : 1);


    }

    /**
     * 第一題
     */
    public static class StudentScoresMapper extends Mapper<LongWritable,Text,Text,IntWritable> {
        private IntWritable outValue = new IntWritable();
        private Text outKey = new Text();
        @Override
        protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
            String[] splits = value.toString().split(",");
            outKey.set(splits[0]);
            outValue.set(Integer.parseInt(splits[2]));
            context.write(outKey,outValue);
        }
    }

    /**
     * 第一題
     */
    public static class StudentScoresReducer extends Reducer<Text,IntWritable,Text,Text> {
        private Text outValue = new Text();
        @Override
        protected void reduce(Text key, Iterable<IntWritable> values, Context context) throws IOException, InterruptedException {
            int min = 1000 ;
            int max = 0 ;
            int avg  ;
            int sum = 0 ;
            int count = 0 ;
            //方法一:最大最小通過逐一比較得到
//            for (IntWritable val:
//                 values) {
//                int score = val.get();
//                if(max  < score){
//                    max = score;
//                }
//                if(min > score){
//                    min = score ;
//                }
//                sum += score ;
//                count++ ;
//            }
//                avg = sum /count ;
//                String outStr = "max="+max+" min="+min+" avg="+avg;
//                outValue.set(outStr);

            //方法二:最大最小值通過集合數組得到
            List<Integer> scores = new ArrayList<>();
            for (IntWritable val :
                    values) {
                scores.add(val.get());
                sum += val.get();
                count++;
            }
            Collections.sort(scores);
            min = scores.get(0);
            max = scores.get(scores.size()-1);
            avg = sum / count ;
            String outStr = "max="+max+" min="+min+" avg="+avg;
            outValue.set(outStr);
            context.write(key,outValue);
        }
    }

    /**
     * 第二題
     */
    public static class StudentScoresMapper2 extends Mapper<LongWritable,Text,Text,Text>{
        private Text outKey = new Text();
        private Text outValue = new Text();
        @Override
        protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
            String[] splits = value.toString().split(",");
            outKey.set(splits[0]+"\t"+splits[2]);
            outValue.set(splits[1]);
            context.write(outKey,outValue);
        }
    }

    /**
     * 第二題
     */
    public static class StudentScoresReducer2 extends Reducer<Text,Text,Text,Text>{
        private Text outValue = new Text();
        @Override
        protected void reduce(Text key, Iterable<Text> values, Context context) throws IOException, InterruptedException {
            StringBuilder sb = new StringBuilder();
            int count = 0 ;
            for (Text text :
                    values) {
                if(sb.length()!=0){
                    sb.append(",");
                }
                sb.append(text);
                count++;
            }
            //如果count大於等於2,說明有分數重合的
            if(count >= 2 ){
                outValue.set(count+" "+sb.toString());
                context.write(key,outValue);
            }
        }
    }
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章