问题描述:

在输入文件中,有多个,其中每个输入文件代表一个学生的各科成绩,其中每行的数据形式为<科目,成绩>,你需要将每个文件中的每科目的成绩进行统计,然后求平均值。

输入文件格式:

这里有三个学生:

输出文件格式:

实例代码:

package com.test;

import java.io.IOException;

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 StudentAverage { public static void main(String[] args) throws IllegalArgumentException, IOException, ClassNotFoundException, InterruptedException {
@SuppressWarnings("deprecation")
Job job = new Job(new Configuration(), "StudentAverage");
job.setJarByClass(StudentAverage.class);
job.setMapperClass(Map.class);
job.setReducerClass(Reduce.class);
job.setMapOutputKeyClass(Text.class);
job.setMapOutputValueClass(IntWritable.class);
job.setOutputKeyClass(Text.class);
job.setOutputValueClass(IntWritable.class);
FileInputFormat.setInputPaths(job, new Path("hdfs://localhost:9000/Student/input"));
FileOutputFormat.setOutputPath(job, new Path("hdfs://localhost:9000/Student/output"));
job.waitForCompletion(true);
System.out.println("运行结束!");
} public static class Map extends Mapper<LongWritable, Text, Text, IntWritable>{
protected void map(LongWritable key, Text value,
org.apache.hadoop.mapreduce.Mapper<LongWritable, Text, Text, IntWritable>.Context context)
throws java.io.IOException, InterruptedException { String[] data = value.toString().split(" ");
context.write(new Text(data[0]), new IntWritable(Integer.parseInt(data[1])));
};
} public static class Reduce extends Reducer<Text, IntWritable, Text, IntWritable> {
protected void reduce(Text key, java.lang.Iterable<IntWritable> values, Context context)
throws java.io.IOException, InterruptedException {
int average = 0;
int sum = 0;
for (IntWritable value : values) {
sum += value.get();
}
average = sum / 3;
context.write(new Text(key), new IntWritable(average));
};
}
}

MapReduce编程练习(四),统计多个输入文件学生的平均成绩,的更多相关文章

  1. YTU 2626: B 统计程序设计基础课程学生的平均成绩

    2626: B 统计程序设计基础课程学生的平均成绩 时间限制: 1 Sec  内存限制: 128 MB 提交: 427  解决: 143 题目描述 程序设计基础课程的学生成绩出来了,老师需要统计出学生 ...

  2. 问题 C: B 统计程序设计基础课程学生的平均成绩

    题目描述 程序设计基础课程的学生成绩出来了,老师需要统计出学生个数和平均成绩.学生信息的输入如下: 学号(num)                     学生姓名(name)            ...

  3. MapReduce编程:词频统计

    首先在项目的src文件中需要加入以下文件,log4j的内容为: log4j.rootLogger=INFO, stdout log4j.appender.stdout=org.apache.log4j ...

  4. MapReduce编程实例4

    MapReduce编程实例: MapReduce编程实例(一),详细介绍在集成环境中运行第一个MapReduce程序 WordCount及代码分析 MapReduce编程实例(二),计算学生平均成绩 ...

  5. MapReduce编程实例2

    MapReduce编程实例: MapReduce编程实例(一),详细介绍在集成环境中运行第一个MapReduce程序 WordCount及代码分析 MapReduce编程实例(二),计算学生平均成绩 ...

  6. MapReduce编程实例6

    前提准备: 1.hadoop安装运行正常.Hadoop安装配置请参考:Ubuntu下 Hadoop 1.2.1 配置安装 2.集成开发环境正常.集成开发环境配置请参考 :Ubuntu 搭建Hadoop ...

  7. MapReduce编程实例5

    前提准备: 1.hadoop安装运行正常.Hadoop安装配置请参考:Ubuntu下 Hadoop 1.2.1 配置安装 2.集成开发环境正常.集成开发环境配置请参考 :Ubuntu 搭建Hadoop ...

  8. MapReduce编程实例3

    MapReduce编程实例: MapReduce编程实例(一),详细介绍在集成环境中运行第一个MapReduce程序 WordCount及代码分析 MapReduce编程实例(二),计算学生平均成绩 ...

  9. 假期学习【五】RDD编程实验四

    今天完成了实验四的第二问和第三问 第二题 对于两个输入文件 A 和 B,编写 Spark 独立应用程序,对两个文件进行合并,并剔除其 中重复的内容,得到一个新文件 C.下面是输入文件和输出文件的一个样 ...

随机推荐

  1. codeforces 1451D,一道有趣的博弈论问题

    大家好,欢迎来到codeforces专题. 今天选择的问题是Contest 1451场的D题,这是一道有趣简单的伪博弈论问题,全场通过的人有3203人.难度不太高,依旧以思维为主,坑不多,非常友好. ...

  2. 织梦dedecms自增变量autoindex标签的使用(转)

    织梦dedecms自增变量autoindex标签的使用 例1: {dede:arclist titlelen='120' row='8' typeid='2'}         <li clas ...

  3. SpringgBoot父子工程的创建

    知识:SpringBoot父子工程创建 花开堪折直需折,莫待无花空折枝 开始之前,非常非常有必要了解一下关于以及的区别,这样才可以在进行创建maven父子工程种避免一些不必要的意外错误. depend ...

  4. DB2在渗透中的应用(转载)

    原文地址:http://drops.wooyun.org/tips/16673 0x00 DB2简介 DB2是IBM公司推出关系型数据库管理系统. 现今DB2主要包含以下三个系列: DB2 for L ...

  5. String -- 从源码剖析String类

    几乎所有的 Java 面试都是以 String 开始的,String 源码属于所有源码中最基础.最简单的一个,对 String 源码的理解也反应了你的 Java 基础功底. String 是如何实现的 ...

  6. #3使用html+css+js制作网页 制作登录网页

    #3使用html+css+js制作网页 制作登录网页 本系列链接 2制作登录网页 2.1 准备 2.1.1 创建文件夹 2.1.2 创建主文件 2.2 html部分 2.2.1 网站信息 2.2.2 ...

  7. [RTMP] 国内各大视频直播CDN厂商推流抢流行为分析

    背景 当存在一个推流客户端正在向rtmp://xxx.com/live/yyy推流时,又有另外一个推流客户端同时对这个地址进行推流,会发生什么呢? 查阅了 Adobe RTMP Spec 发现规范本身 ...

  8. 【Flutter】可滚动组件之CustomScrollView

    前言 CustomScrollView是可以使用Sliver来自定义滚动模型(效果)的组件.它可以包含多种滚动模型,举个例子,假设有一个页面,顶部需要一个GridView,底部需要一个ListView ...

  9. SpringBoot配置文件(1)

    配置文件 1.配置文件 SpringBoot使用一个全局的配置文件 application.properties application.yml 配置文件名是固定的: 他的作用是修改SpringBoo ...

  10. 在.NET Core 中使用Quartz.NET

    Quartz.NET是功能齐全的开源作业调度系统,可用于最小的应用程序到大型企业系统. Quartz.NET具有三个主要概念: job:运行的后台任务 trigger:控制后台任务运行的触发器. sc ...