六十一、Hadoop——Hadoop中求每个学生的总分(MapReduce)

一、Student

package nj.zb.kb15.demo3;
//ctrl + 点击  进入
//ctrl + T 查看方法
//F4右侧展现方法
//查看当前类方法结构ctrl +alt +h
import org.apache.hadoop.io.WritableComparable;

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

public class Student implements WritableComparable<Student> {
    private  long stuId;     //蛇形命名法 stu_id
    private  String stuName;
    private  int score;

    public Student() {
    }

    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 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;
    }

    @Override
    public int compareTo(Student o) {
      // if (o.stuName==this.stuName && o.stuId==this.stuId)
     //    return 1;
        return 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();
    }
}

二、StudentScoreMapper

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 StudentScoreMapper extends Mapper<LongWritable, Text,LongWritable,Student> {
    @Override
    protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
        System.out.println(key.get()+" "+value);
        String[] split=value.toString().split(",");
        LongWritable stuid=new LongWritable(Long.parseLong(split[0]));
        Student student=new Student(stuid.get(),split[1],Integer.parseInt(split[3]));
        context.write(stuid,student);  //输出结果<1,student[1,名字,成绩]
    }
}

三、StudentScoreReducer

package nj.zb.kb15.demo3;

import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.mapreduce.Reducer;

import java.io.IOException;


public class StudentScoreReducer extends Reducer<LongWritable, Student, LongWritable, Student> {

   Student student=new Student();
    @Override
    protected  void reduce(LongWritable key, Iterable<Student> values, Context context) throws  IOException,InterruptedException{
        int sum=0;
        String  stuName="";
        for(Student stu:values){
            if(stuName.equals(""))
            stuName=stu.getStuName();
            sum+=stu.getScore();
        }
        student.setStuName(stuName);
        student.setStuId(key.get());
        student.setScore(sum);

        System.out.println(key.get()+"总成绩:"+sum);
        context.write(key,student);
    }
}

四、StudentScoreDriver

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.LongWritable;
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 StudentScoreDriver {
    public static void main(String[] args) throws IOException, ClassNotFoundException, InterruptedException {
        Configuration configuration=new Configuration();
        Job job=Job.getInstance(configuration);

        job.setJarByClass(StudentScoreDriver.class);

        job.setMapperClass(StudentScoreMapper.class);
        job.setMapOutputKeyClass(LongWritable.class);
        job.setMapOutputValueClass(Student.class);

        job.setReducerClass(StudentScoreReducer.class);
        job.setOutputKeyClass(LongWritable.class);
        job.setOutputValueClass(Student.class);

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

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

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

五、运行结果

1   Student{stuId=1, stuName='鸿飞', score=174}
2  Student{stuId=2, stuName='鹏海', score=177}
3  Student{stuId=3, stuName='铭晨', score=180}
4  Student{stuId=4, stuName='霑千昌', score=183}
5  Student{stuId=5, stuName='智宇', score=186}
6  Student{stuId=6, stuName='景彰', score=189}
7  Student{stuId=7, stuName='辰铭', score=192}
8  Student{stuId=8, stuName='曜灿', score=195}
9  Student{stuId=9, stuName='昊苍', score=198}
10 Student{stuId=10, stuName='子昂', score=201}
11 Student{stuId=11, stuName='景行', score=204}
12 Student{stuId=12, stuName='昆皓', score=207}
13 Student{stuId=13, stuName='文昂', score=210}
14 Student{stuId=14, stuName='昊苍', score=164}
15 Student{stuId=15, stuName='德泽', score=167}
16 Student{stuId=16, stuName='鸿远', score=170}
17 Student{stuId=17, stuName='昌燎', score=173}
18 Student{stuId=18, stuName='昌翰', score=176}
19 Student{stuId=19, stuName='鸿振', score=179}
20 Student{stuId=20, stuName='鸿卓', score=182}
21 Student{stuId=21, stuName='浩初', score=185}
22 Student{stuId=22, stuName='运鹏', score=188}
23 Student{stuId=23, stuName='新曦', score=191}
24 Student{stuId=24, stuName='智阳', score=194}
25 Student{stuId=25, stuName='杨伟', score=197}
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
### 回答1: MapReduce 是一个用于处理大规模数据的分布式编程框架。在 HBase 中,MapReduce 可以用来统计成绩表中的单科排名和总分排名。 下面是一个简单的 MapReduce 示例,用于统计 HBase 中成绩表中的单科排名: 1. 定义 Mapper 类,其中实现 map() 方法,接收输入的 key-value 对(即 HBase 中的行键和列族),并处理成绩数据。 2. 定义 Reducer 类,其中实现 reduce() 方法,接收 Mapper 输出的 key-value 对,并对成绩数据进行排序。 3. 使用 HBase API 读取成绩表中的数据,并将其传递给 Mapper 类。 4. 运行 MapReduce 程序,并输出统计结果。 统计总分排名的方法类似,只需要在 Mapper 和 Reducer 中对所有科目的成绩进行求和,然后在 Reducer 中对总分进行排序即可。 ### 回答2: MapReduce是一种用于处理大规模数据集的分布式计算框架。HBase是一种分布式、面向列的开源数据库,可以在Hadoop集群上进行横向扩展和高可靠性存储。提供了对实时读写的支持。 在MapReduce项目中,我们可以使用HBase作为数据源,通过MapReduce作业来统计HBase成绩表中的单科排名和总分排名。 首先,我们需要定义输入格式,即将HBase的数据转化为适合MapReduce处理的键值对格式。可以使用HBase提供的TableInputFormat类来读取HBase表中的数据,并将其转化为key-value对。 接下来,我们需要实现Mapper类。Mapper负责将输入的键值对进行处理,提取出需要的数据,并以键值对的形式输出给Reducer。在本例中,我们可以将学生的姓名作为键,将成绩作为值进行输出。 Reducer类负责对Mapper输出的键值对进行处理,计算每个学生总分,并将结果进行排序。在本例中,我们可以使用TreeMap来对学生总分进行排序。 最后,我们还需要定义输出格式,将Reducer的输出写入到HBase表中。可以使用HBase提供的TableOutputFormat类来将结果写入到HBase表中,以更新学生的排名信息。 综上所述,通过以上步骤,我们可以实现MapReduce项目来统计HBase成绩表中的单科排名和总分排名。 ### 回答3: MapReduce是一种用于处理大规模数据集的编程模型和算法。在Hadoop生态系统中,MapReduce被广泛用于并行处理和分析大数据。 对于统计HBase成绩表中的单科排名和总分排名,我们可以使用MapReduce来完成。 首先,我们需要编写Mapper来读取HBase表中的数据,并按照学生ID作为键,成绩数据作为值进行映射。这样可以保证每个Mapper处理一行数据。然后,我们可以在Mapper中计算单科分数或总分,作为中间结果。 接下来,我们需要编写Reducer来合并和处理Mapper的输出。在Reducer中,我们可以根据需要对中间结果进行排序和聚合操作。对于单科排名,我们可以根据每个学生的成绩进行排序,并分配排名。对于总分排名,我们可以按照学生总分进行排序,并为每个学生分配排名。 最后,我们将Reducer的输出写回到HBase表中,以便我们可以在需要时查询排名结果。 整个过程中,MapReduce能够充分利用分布式计算的优势来高效地处理大数据集。通过适当的数据处理和运算,我们可以得到HBase成绩表中的单科排名和总分排名。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

天地风雷水火山泽

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

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

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

打赏作者

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

抵扣说明:

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

余额充值