MapReduce编程实例:
MapReduce编程实例(一),详细介绍在集成环境中运行第一个MapReduce程序 WordCount及代码分析
MapReduce编程实例(五),MapReduce实现单表关联
实例二,计算学生的平均成绩,每个文件包括所有的学生成绩,格式为 姓名 成绩,有多少个科目,就有多少个输入文件。
如下
小明 23
小强 57
小红 80
小飞 93
小刚 32
小木 99
实现代码:
- 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.FloatWritable;
- 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;
- import org.apache.hadoop.util.GenericOptionsParser;
- /**
- * 计算学生的平均成绩
- * 学生成绩以每科一个文件输入
- * 文件内容为:姓名 成绩
- * @author daT dev.tao@gmail.com
- *
- */
- public class AverageScore {
- public static class AverageMapper extends Mapper<Object, Text, Text, FloatWritable>{
- @Override
- protected void map(Object key, Text value, Context context)
- throws IOException, InterruptedException {
- String line = value.toString();
- StringTokenizer tokens = new StringTokenizer(line," ");
- while(tokens.hasMoreTokens()){
- String tmp = tokens.nextToken();
- StringTokenizer sz = new StringTokenizer(tmp);
- String name = sz.nextToken();
- float score = Float.valueOf(sz.nextToken());
- Text outName = new Text(name);//new新的,set老是不对,具体为什么现在也不太清楚。
- FloatWritable outScore = new FloatWritable(score);
- context.write(outName, outScore);
- }
- }
- }
- public static class AverageReducer extends Reducer<Text, FloatWritable, Text, FloatWritable>{
- @Override
- protected void reduce(Text key, Iterable<FloatWritable> value,Context context)
- throws IOException, InterruptedException {
- float sum = 0;
- int count = 0;
- for(FloatWritable f:value){
- sum += f.get();
- count ++;//shuffle之后肯定是<名字,<成绩1,成绩2,成绩3....>>故一个value肯定是一门学科
- }
- FloatWritable averageScore = new FloatWritable(sum/count);////new新的,set老是不对,具体为什么现在也不太清楚。
- context.write(key, averageScore);
- }
- }
- public static void main(String[] args) throws IOException, ClassNotFoundException, InterruptedException{
- System.out.println("Begin");
- Configuration conf = new Configuration();
- String[] otherArgs = new GenericOptionsParser(conf, args).getRemainingArgs();
- if(otherArgs.length<2){
- System.out.println("please input at least 2 arguments");
- System.exit(2);
- }
- Job job = new Job(conf,"Average Score");
- job.setJarByClass(AverageScore.class);
- job.setMapperClass(AverageMapper.class);
- job.setCombinerClass(AverageReducer.class);
- job.setReducerClass(AverageReducer.class);
- job.setOutputKeyClass(Text.class);
- job.setOutputValueClass(FloatWritable.class);
- FileInputFormat.addInputPath(job, new Path(otherArgs[0]));
- FileOutputFormat.setOutputPath(job, new Path(otherArgs[1]));
- System.exit(job.waitForCompletion(true)?0:1);
- System.out.println("End");
- }
- }
配置输入输出参数:
- hdfs://localhost:9000/user/dat/average_score_input hdfs://localhost:9000/user/dat/average_score_output
得到输出结果:
小刚 65.333336
小强 80.333336
小明 48.333332
小木 92.333336
小红 83.333336
小飞 83.0
版权声明:本文为博主原创文章,未经博主允许不得转载。