美文网首页
学生各科平均成绩报表

学生各科平均成绩报表

作者: 徐嘉迪 | 来源:发表于2022-01-09 19:03 被阅读0次

题干:

编写三个类,一个 map类,一个reduce类,一个mian方法类。

一、map类

二、reduce

三、mian

四、结果

以下是书本上的方法:

package com.mapreducer;

//8.学生各科平均成绩报表

import java.io.IOException;

import java.util.Iterator;

import java.util.StringTokenizer;

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;

//测试主类

public class TestMapReducer08 {

public static void main(String[] args) throws Exception {

  // 获取作业对象

  Job job = Job.getInstance(new Configuration());

  // 设置主类

  job.setJarByClass(TestMapReducer08.class);

  // 设置job参数

  job.setMapperClass(AvgScoreMapper08.class);

  job.setReducerClass(AvgScoreReducer08.class);

  job.setOutputKeyClass(Text.class);

  job.setOutputValueClass(IntWritable.class);

  // 设置job输入输出

FileInputFormat.addInputPath(job, new Path("file:///usr/source08.txt"));

FileOutputFormat.setOutputPath(job, new Path("file:///usr/output08"));

System.exit(job.waitForCompletion(true) ? 0 : 1);

}

}

// 让类AvgScoreMapper08继承类Mapper同时指定需要的参数类型,修改map类的内容

class AvgScoreMapper08 extends Mapper<LongWritable, Text, Text, IntWritable> {

@Override

protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {

// 读取—行数据

String val = value.toString();// 把读取的数据以换行作为分割符

StringTokenizer stringTokenizer = new StringTokenizer(val, "\n");

while (stringTokenizer.hasMoreElements()) {

StringTokenizer tmp = new StringTokenizer(stringTokenizer.nextToken());

// 对读取的一行的名称和成绩进行切分并写入到context对象中

String username = tmp.nextToken();

String score = tmp.nextToken();

context.write(new Text(username), new IntWritable(Integer.valueOf(score)));

}

}

}

// 让类AvgScoreReducer08继承类Reducer同时指定需要的参数类型,修改reducer类的内容

class AvgScoreReducer08 extends Reducer<Text, IntWritable, Text, IntWritable> {

@Override

protected void reduce(Text key, Iterable<IntWritable> values, Context context)

throws IOException, InterruptedException {

// 获取对键值集合遍历对象

Iterator<IntWritable> Iterator = values.iterator();

int count = 0;

int sum = 0;

// 循环获取相同键的所有值并计算和

while (Iterator.hasNext()) {

int v = Iterator.next().get();

sum += v;

count++;

}

int avg = sum / count;

context.write(key, new IntWritable(avg));

}

}

最后还是建议使用第1种方法꒰๑'ꀾ'๑꒱

相关文章

  • 学生各科平均成绩报表

    题干: 编写三个类,一个 map类,一个reduce类,一个mian方法类。 一、map类 二、reduce 三、...

  • 图解面试题:如何分组比较?

    ​【题目】 现有“成绩表”,记录了每个学生各科的成绩。表内容如下: 问题:查找单科成绩高于该科目平均成绩的学生名单...

  • mysql多表分组查询

    前置条件 1、多表分组查询学生各科的成绩、总分、平均分 统计平均分大于70分 HAVING AVG(grade.s...

  • 2020-11-19 初二班级家长会上的发言

    一、分析成绩: 我想:先看总成绩,再看各科成绩,了解在班级以及在年级的排名,学会分析,看学生的各科成绩,特别是学生...

  • 学生案例~李

    学生状况,初一入班成绩中等,或者中等偏下,各科成绩及格及格线上,疫情期间网课学习习惯不好,各科成绩均有所下降,...

  • SQL

    1. 查询各科成绩均在80分以上的学生姓名

  • 嘴巴

    刚接一个小四的班级,行为有些懒散,看以往成绩很低,各科平均分不到四十,我有些头痛。通过观察我发现学生上课窃窃私语,...

  • 数组-求平均值

    计算一个班5个学生的平均成绩,然后输出低于平均成绩的分数和人数

  • 2018-12-28数据库练习

    -- 1、查询平均成绩大于等于60分的同学的学生编号和学生姓名和平均成绩SELECT a.s_id,b.s_nam...

  • 复学前最后一次居家月考考情分析

    各科老师阅卷结束后,我汇总了成绩。进行了单科排名和总排名,统计了各科均分和学段分布,然后发给了任课老师。 学生成绩...

网友评论

      本文标题:学生各科平均成绩报表

      本文链接:https://www.haomeiwen.com/subject/eyzucrtx.html