请设计一个MapReduce程序用于统计某学校学生成绩的分布,按照不及格(60分以下)、一般(60~69)、中等(70~79)、良好(80~89),优秀(90以上)几个个类别进行统计,得出各自的人次。

1 篇文章 0 订阅
package com.yinke.wsw.rating;

import org.apache.hadoop.conf.Configuration;
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;
public class RatingDemo {
    private static class MyMap extends Mapper<LongWritable, Text,Text,IntWritable> {
        Text k = new Text();
        IntWritable v = new IntWritable();
        //重写map方法
        @Override
        protected void map(LongWritable key, Text value, Mapper<LongWritable, Text, Text, IntWritable>.Context context) throws IOException, InterruptedException {
            //每一行包括课程号、课程名称、学号、学生姓名、成绩(百分制),数据项由空格隔开。
            String string = value.toString();
            //按行分割
            String[] strings = string.split("\n");
            for (int i = 0; i < strings.length; i++) {
                //以空格分割
                String[] data = strings[i].split(" ");
                //数组第5个元素是成绩
                double score = Double.parseDouble(data[4]);
                //以成绩进行分割
                if (score < 60) {
                    k.set("不及格");
                    v.set(1);
                } else if (score >= 60 && score < 70) {
                    k.set("一般");
                    v.set(1);
                } else if (score >= 70 && score < 80) {
                    k.set("中等");
                    v.set(1);
                } else if (score >= 80 && score < 90) {
                    k.set("良好");
                    v.set(1);
                } else if (score >= 90) {
                    k.set("优秀");
                    v.set(1);
                }
                //生成key value键值对
                context.write(k, v);
            }
        }

        private static class MyReduce extends Reducer<Text, IntWritable, Text, IntWritable> {
            //重写reduce方法
            @Override
            protected void reduce(Text key, Iterable<IntWritable> values, Reducer<Text, IntWritable, Text, IntWritable>.Context context) throws IOException, InterruptedException {
                int count = 0;
                //遍历values
                for (IntWritable v : values) {
                    int value = v.get();
                    count += value;
                }
                //汇总每个分数段的人数
                context.write(key, new IntWritable(count));
            }
        }

        public static void main(String[] args) throws IOException, ClassNotFoundException, InterruptedException {
            // 1 获取配置信息以及封装任务
            Configuration configuration = new Configuration();
            Job job = Job.getInstance(configuration);
            // 2 设置jar加载路径
            job.setJarByClass(RatingDemo.class);
            // 3 设置map和reduce类
            job.setMapperClass(MyMap.class);
            job.setReducerClass(MyReduce.class);
            // 4 设置map输出
            job.setMapOutputKeyClass(Text.class);
            job.setMapOutputValueClass(IntWritable.class);
            // 5 设置最终输出kv类型
            job.setOutputKeyClass(Text.class);
            job.setOutputValueClass(IntWritable.class);
            // 6 设置输入和输出路径
            Path input = new Path("/input/report.txt");
            FileInputFormat.addInputPath(job, input);
            Path output = new Path("/output/result");
            //判断output文件夹是否存在,如果存在则删除
            if (output.getFileSystem(configuration).exists(output)) {
                output.getFileSystem(configuration).delete(output);
            }
            FileOutputFormat.setOutputPath(job, output);
            //等待计算完成
            boolean completion = job.waitForCompletion(true);
            if (completion) {
                System.out.println("计算成功!");
            } else {
                System.out.println("计算失败!");
            }
        }
    }
}

如果本篇文章对你有帮助,还请一键三连【开心】

  • 4
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

YOUNG.K

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值