求学生成绩-统计每门课程的参考人数和课程平均分

有如下数据


computer,huangxiaoming,85,86,41,75,93,42,85

computer,xuzheng,54,52,86,91,42

computer,huangbo,85,42,96,38

english,zhaobenshan,54,52,86,91,42,85,75

english,liuyifei,85,41,75,21,85,96,14

algorithm,liuyifei,75,85,62,48,54,96,15

computer,huangjiaju,85,75,86,85,85

english,liuyifei,76,95,86,74,68,74,48

english,huangdatou,48,58,67,86,15,33,85

algorithm,huanglei,76,95,86,74,68,74,48

algorithm,huangjiaju,85,75,86,85,85,74,86

computer,huangdatou,48,58,67,86,15,33,85

english,zhouqi,85,86,41,75,93,42,85,75,55,47,22

english,huangbo,85,42,96,38,55,47,22

algorithm,liutao,85,75,85,99,66

computer,huangzitao,85,86,41,75,93,42,85

math,wangbaoqiang,85,86,41,75,93,42,85

computer,liujialing,85,41,75,21,85,96,14,74,86

computer,liuyifei,75,85,62,48,54,96,15

computer,liutao,85,75,85,99,66,88,75,91

computer,huanglei,76,95,86,74,68,74,48

english,liujialing,75,85,62,48,54,96,15

math,huanglei,76,95,86,74,68,74,48

math,huangjiaju,85,75,86,85,85,74,86

math,liutao,48,58,67,86,15,33,85

english,huanglei,85,75,85,99,66,88,75,91

math,xuzheng,54,52,86,91,42,85,75

math,huangxiaoming,85,75,85,99,66,88,75,91

math,liujialing,85,86,41,75,93,42,85,75

english,huangxiaoming,85,86,41,75,93,42,85

algorithm,huangdatou,48,58,67,86,15,33,85

algorithm,huangzitao,85,86,41,75,93,42,85,75

一、数据解释

数据字段个数不固定:

第一个是课程名称,总共四个课程,computer,math,english,algorithm,

第二个是学生姓名,后面是每次考试的分数

二、统计需求:

1、统计每门课程的参考人数和课程平均分,

public class pinjunfenP {
static class mymapper extends Mapper<LongWritable, Text, Text, Text>{
        @Override
        protected void map(LongWritable key, Text value,  Context context)  throws IOException, InterruptedException {
            String[] fields = value.toString().split(",");
            for (String string : fields) {
                System.out.print(string + "  ");
            }
            //取出科目
            String kemu = fields[0];            
            //取出成绩放到新数组中去
            StringBuffer cjstr=new StringBuffer();
            for (int i = 2; i < fields.length; i++) {
                cjstr.append(fields[i]);
                 //不是最后一个成绩的时候append一个逗号
                 if (i!=fields.length-1) {
                     cjstr.append(",");
                }
            }
            context.write(new Text(kemu), new Text(cjstr.toString()));
        }
    }

    static class myreducer extends Reducer<Text, Text, Text, Text>{
        @Override
        protected void reduce(Text key, Iterable<Text> values, Context context) throws IOException, InterruptedException {
            //记录人数
            int count=0;
            double avgofone;
            double avg;
            int sumall=0;
            for (Text text : values) {
                //一个人的平均成绩
                String[] str = text.toString().split(",");
                int sum=0;
                int num=0;
                for (String string : str) {
                    sum+=Integer.parseInt(string);
                    num++;
                }
                avgofone=(sum/num);
                count++;
                sumall+=avgofone;
            }
            //一个科目的平均成绩
            avg=sumall/count;
            String outString=count+","+avg;
            Text t=new Text(outString);
            context.write(key, t);
        }
    }

    public static void main(String[] args) throws Exception {
        Configuration conf = new Configuration();
        Job job=Job.getInstance(conf);
        //设置主函数的入口
        job.setJarByClass(pinjunfenP.class);
        //设置job的mapper的类
        job.setMapperClass(mymapper.class);
        //设置job的reduce类
        job.setReducerClass(myreducer.class);
        //设置map输出key的类型
        job.setMapOutputKeyClass(Text.class);
        //设置map输出value的类型
        job.setMapOutputValueClass(Text.class);     
        //设置reduce输出key的类型
        job.setOutputKeyClass(Text.class);
        //设置reduce输出value的类型
        job.setOutputValueClass(Text.class);        
        //文件输入类
        Path inpath=new Path("hdfs://master:9000/hadoop/chengji");
        FileInputFormat.addInputPath(job, inpath);
        //文件输出类
        Path outpath=new Path("hdfs://master:9000/hadoop/chengjiout");
        FileOutputFormat.setOutputPath(job,outpath);
        //提交job
        job.waitForCompletion(true);
    }
}

运行的时候报空指针,发现是stringbuffer没有new对象
最后统计结果如下
algorithm    6,71.0
computer    10,69.0
english    9,66.0
math    7,72.0


2、在1的结果上,按照平均分降序排列

3、统计每一个学生的每一门课程的平均分,在对结果按照科目 在按照平均分进行降序排序
 

猜你喜欢

转载自blog.csdn.net/u012580143/article/details/84615325