学生各科平均成绩报表

2022-01-09  本文已影响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种方法꒰๑'ꀾ'๑꒱

上一篇下一篇

猜你喜欢

热点阅读