六十二、Hadoop——Hadoop中求每门科目的总分(MapReduce)

一、SubjectScore

package nj.zb.kb15.demo3;
//每个科目总分
import org.apache.hadoop.io.WritableComparable;

import java.io.DataInput;
import java.io.DataOutput;
import java.io.IOException;

public class SubjectScore implements WritableComparable<SubjectScore> {

    private  String subjectName;
    private  int score;

    public SubjectScore() {
    }

    public SubjectScore(String subjectName, int score) {
        this.subjectName = subjectName;
        this.score = score;
    }

    @Override
    public String toString() {
        return "SubjectScore{" +
                "subjectName='" + subjectName + '\'' +
                ", score=" + score +
                '}';
    }

    public String getSubjectName() {
        return subjectName;
    }

    public void setSubjectName(String subjectName) {
        this.subjectName = subjectName;
    }

    public int getScore() {
        return score;
    }

    public void setScore(int score) {
        this.score = score;
    }



    @Override
    public int compareTo(SubjectScore o) {
        return 0;
    }

    @Override
    public void write(DataOutput dataOutput) throws IOException {
        dataOutput.writeUTF(subjectName);
        dataOutput.writeInt(score);
    }

    @Override
    public void readFields(DataInput dataInput) throws IOException {
       this.subjectName=dataInput.readUTF();
        this.score=dataInput.readInt();

    }
}

二、SubjectScoreMapper

package nj.zb.kb15.demo3;

import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Mapper;

import java.io.IOException;

public class SubjectScoreMapper extends Mapper<LongWritable, Text,Text,SubjectScore> {
    @Override
    protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
        System.out.println(key.get()+" "+value);
        String[] split=value.toString().split(",");
        String subject=split[2];
        Text text=new Text(subject);
        SubjectScore subjectScore=new SubjectScore(subject,Integer.parseInt(split[3]));

        context.write(text,subjectScore);
    }
}

三、SubjectScoreReducer

package nj.zb.kb15.demo3;

import org.apache.hadoop.io.NullWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Reducer;

import java.io.IOException;


public class SubjectScoreReducer extends Reducer<Text,SubjectScore,SubjectScore, NullWritable> {
    SubjectScore subjectScore=new SubjectScore();

    @Override
    protected void reduce(Text key, Iterable<SubjectScore> values, Context context) throws IOException, InterruptedException {
        String subName="";
        int sum=0;
     //   int count=0;
        for(SubjectScore ss:values){
            sum +=ss.getScore();
       //     count++;
            if(subName.equals(""))
                subName=ss.getSubjectName();
        }
    //    int avg=(int)sum/count;

        subjectScore.setScore(sum);
        subjectScore.setSubjectName(subName);

        context.write(subjectScore,NullWritable.get());

    }
}

四、SubjectScoreDriver

package nj.zb.kb15.demo3;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.NullWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;

import java.io.IOException;

public class SubjectScoreDriver {
    public static void main(String[] args) throws IOException, ClassNotFoundException, InterruptedException {
        Configuration configuration = new Configuration();
        Job job = Job.getInstance(configuration);

        job.setJarByClass(SubjectScoreDriver.class);

        job.setMapperClass(SubjectScoreMapper.class);
        job.setMapOutputKeyClass(Text.class);
        job.setMapOutputValueClass(SubjectScore.class);

        job.setReducerClass(SubjectScoreReducer.class);
        job.setOutputKeyClass(SubjectScore.class);
        job.setOutputValueClass(NullWritable.class);

        FileInputFormat.setInputPaths(job, new Path("D:\\test_20211026\\in\\demo3\\stuscore.csv"));

        Path path = new Path("D:\\test_20211026\\in\\out4");
        FileOutputFormat.setOutputPath(job, path);
        FileSystem fs = FileSystem.get(path.toUri(), configuration);

        if (fs.exists(path)) {
            fs.delete(path, true);
        }
        job.waitForCompletion(true);
    }
}

五、运行结果

SubjectScore{subjectName='数学', score=1975}
SubjectScore{subjectName='英语', score=875}
SubjectScore{subjectName='语文', score=1812}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

天地风雷水火山泽

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

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

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

打赏作者

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

抵扣说明:

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

余额充值