hadoop学习:mapreduce入门案例二:统计学生成绩

这里相较于 wordcount,新的知识点在于学生实体类的编写以及使用

数据信息:

  1. Student 实体类
java 复制代码
import org.apache.hadoop.io.WritableComparable;

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

public class Student implements WritableComparable<Student> {
//    Object
    private long stuid;
    private String stuName;
    private int score;

    public Student(long stuid, String stuName, int score) {
        this.stuid = stuid;
        this.stuName = stuName;
        this.score = score;
    }

    @Override
    public String toString() {
        return "Student{" +
                "stuid=" + stuid +
                ", stuName='" + stuName + '\'' +
                ", score=" + score +
                '}';
    }

    public Student() {
    }

    public long getStuid() {
        return stuid;
    }

    public void setStuid(long stuid) {
        this.stuid = stuid;
    }

    public String getStuName() {
        return stuName;
    }

    public void setStuName(String stuName) {
        this.stuName = stuName;
    }

    public int getScore() {
        return score;
    }

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

    // 自动整理文件格式 ctrl + shift + f  英文输放状态
    @Override
    public int compareTo(Student o) {
        return this.score > o.score ? 1 : 0;
    }

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

    @Override
    public void readFields(DataInput dataInput) throws IOException {
        this.stuid = dataInput.readLong();
        this.stuName = dataInput.readUTF();
        this.score = dataInput.readInt();
    }
}
  1. mapper 阶段,StudentMapper 类
java 复制代码
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Mapper;

import java.io.IOException;

/**
 * 输出  key:学生id   value:Student对象
 */
public class StudentMapper extends Mapper<LongWritable, Text,LongWritable,Student> {
    @Override
    protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
        String[] split = value.toString().split(",");
        LongWritable stuidKey = new LongWritable(Long.parseLong(split[0]));
        Student stuValue = new Student(Long.parseLong(split[0]),split[1],Integer.parseInt(split[2]));
        context.write(stuidKey,stuValue);
    }
}
  1. reduce 阶段,StudentReduce 类
java 复制代码
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.NullWritable;
import org.apache.hadoop.mapreduce.Reducer;

import java.io.IOException;

public class StudentReducer extends Reducer<LongWritable,Student,Student, NullWritable> {
    @Override
    protected void reduce(LongWritable key, Iterable<Student> values, Context context) throws IOException,
            InterruptedException {
        Student stuOut = new Student();
        int sumScore = 0;
        String stuName = "";
        for (Student stu :
                values) {
            sumScore+=stu.getScore();
            stuName = stu.getStuName();
        }
        stuOut.setScore(sumScore);
        stuOut.setStuid(key.get());
        stuOut.setStuName(stuName);
        System.out.println(stuOut.toString());
        context.write(stuOut, NullWritable.get());
    }
}
  1. 驱动类,studentDriver 类
java 复制代码
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.NullWritable;
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 StudentDriver {
    public static void main(String[] args) throws IOException, ClassNotFoundException, InterruptedException {
        Configuration conf = new Configuration();
        Job job = Job.getInstance(conf);

        job.setJarByClass(StudentDriver.class);

        //配置 job中map阶段处理类和map阶段的输出类型
        job.setMapperClass(StudentMapper.class);
        job.setMapOutputKeyClass(LongWritable.class);
        job.setMapOutputValueClass(Student.class);


        //配置 job中deduce阶段处理类和reduce阶段的输出类型
        job.setReducerClass(StudentReducer.class);
        job.setOutputKeyClass(Student.class);
        job.setOutputValueClass(NullWritable.class);

        // 输入路径配置  "hdfs://kb131:9000/kb23/hadoopstu/stuscore.csv"
        Path inpath = new Path(args[0]);  // 外界获取文件输入路径
        FileInputFormat.setInputPaths(job, inpath);
        // 输出路径配置  "hdfs://kb131:9000/kb23/hadoopstu/out2"
        Path path = new Path(args[1]);    //
        FileSystem fs = FileSystem.get(path.toUri(), conf);
        if (fs.exists(path))
            fs.delete(path,true);
        FileOutputFormat.setOutputPath(job,path);

        job.waitForCompletion(true);
    }
}
相关推荐
深蓝海拓2 小时前
基于QtPy (PySide6) 的PLC-HMI工程项目(十五)后续完善和改进:PLC端数据解析的防粘包、残包、废包、拆包
笔记·学习·plc
小黄蚁2 小时前
LVGL学习笔记(六)
笔记·学习
zyf1044163 小时前
暑期实践日志 Day33:整体复盘核查视频,完善剪辑成果
学习·计算机网络·剪辑·暑期实践·课题任务
01_ice3 小时前
前端学习css
前端·css·学习
for_ever_love__4 小时前
PySpark学习: 数据输出
开发语言·python·学习·spark
liulilittle5 小时前
llmx 学习手册 06 —— CPU 指令集优化(AVX-512 三层演进)
c++·学习·算法·ai·llm
XiaQ09195 小时前
Linux 常用命令学习笔记(权限管理篇):看懂 rwx,管好每一份文件的生杀大权
linux·笔记·学习
hsjiasb5 小时前
FreeRTOS学习(三十八)——性能优化
学习·学习笔记·freertos
山甫aa5 小时前
JavaWeb后端开发学习手册
java·开发语言·数据库·学习·mysql·springboot·web
BYSJMG6 小时前
计算机毕业设计选题推荐:基于大数据的心脏病风险数据可视化与分析(Hadoop+Spark+PySpark)
大数据·hadoop·信息可视化·数据分析·spark·课程设计