hadoop 平均成绩

3.1 实例描述

  对输入文件中数据进行就算学生平均成绩。输入文件中的每行内容均为一个学生姓名和他相应的成绩,如果有多门学科,则每门学科为一个文件。要求在输出中每行有两个间隔的数据,其中,第一个代表学生的姓名第二个代表其平均成绩

    样本输入

  1)math:

 

张三    88

李四    99

王五    66

赵六    77

 

    2)china:

 

张三    78

李四    89

王五    96

赵六    67

 

    3)english:

 

张三    80

李四    82

王五    84

赵六    86

 

    样本输出

 

张三    82

李四    90

王五    82

赵六    76

3.2 设计思路

    计算学生平均成绩是一个仿"WordCount"例子,用来重温一下开发MapReduce程序的流程。程序包括两部分的内容:Map部分和Reduce部分,分别实现了map和reduce的功能。

    Map处理的是一个纯文本文件, 文件中存放的数据时每一行表示一个学生的姓名和他相应一科成绩。Mapper处理的数据是由InputFormat分解过的数据集,其中 InputFormat的作用是将数据集切割成小数据集InputSplit,每一个InputSlit将由一个Mapper负责处理。此 外,InputFormat中还提供了一个RecordReader的实现,并将一个InputSplit解析成<key,value>对提 供给了map函数。InputFormat的默认值是TextInputFormat,它针对文本文件,按行将文本切割成InputSlit,并用 LineRecordReader将InputSplit解析成<key,value>对,key是行在文本中的位置,value是文件中的 一行。

    Map的结果会通过partion分发到Reducer,Reducer做完Reduce操作后,将通过以格式OutputFormat输出。

    Mapper最终处理的结果对<key,value>,会送到Reducer中进行合并,合并的时候,有相同key的键/值对则送到同一个 Reducer上。Reducer是所有用户定制Reducer类地基础,它的输入是key和这个key对应的所有value的一个迭代器,同时还有 Reducer的上下文。Reduce的结果由Reducer.Context的write方法输出到文件中。

3.3 程序代码

    程序代码如下所示:

  

package test;

import java.io.IOException;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.DoubleWritable;
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;

public class Score {

	public static class Map extends Mapper<Object, Text, Text, DoubleWritable>{
		private static Text name = new Text();
		private static DoubleWritable score = new DoubleWritable();
		
		protected void map(Object key, Text value, Context context) 
				throws java.io.IOException ,InterruptedException {
			String[] splits = value.toString().split("\t");
			if(splits.length != 2){
				return;
			}
			name.set(splits[0]);
			score.set(Double.parseDouble(splits[1]));
			context.write(name, score);
		};
	}
	public static class Reduce extends Reducer<Text, DoubleWritable, Text, DoubleWritable>{
		private static DoubleWritable avg = new DoubleWritable();
		
		protected void reduce(Text name, Iterable<DoubleWritable> scores, Context context) 
				throws java.io.IOException ,InterruptedException {
			double sum = 0;
			int count = 0;
			for (DoubleWritable score : scores) {
				sum += score.get();
				count++;
			}
			avg.set(sum/count);
			context.write(name, avg);
		};
	}
	public static void main(String[] args) throws Exception {
		Configuration conf = new Configuration();
		String[] otherArgs = new GenericOptionsParser(conf,args).getRemainingArgs();
		if(otherArgs.length != 2){
			System.err.println("Usage:Score Avg");
			System.exit(2);
		}
		Job job = new Job(conf, "Score Avg");
		job.setJarByClass(Score.class);
		
		job.setMapperClass(Map.class);
		job.setReducerClass(Reduce.class);
		
		job.setMapOutputKeyClass(Text.class);
		job.setMapOutputValueClass(DoubleWritable.class);
		
		job.setOutputKeyClass(Text.class);
		job.setOutputValueClass(DoubleWritable.class);
		
		FileInputFormat.addInputPath(job, new Path(args[0]));
		FileOutputFormat.setOutputPath(job, new Path(args[1]));
		
		System.exit(job.waitForCompletion(true) ? 0 : 1);
	}
}

 3.4 准备测试数据

  上传数据到hdfs上,设置myeclipse的运行输入参数,运行

3.5

  输出结果,不用了吧。

  备注:文本文件的编码为"UTF-8",默认为"ANSI",可以另存为时选择,不然中文会出现乱码

 

参考:http://penghuaiyi.iteye.com/blog/1943464

附注:

  原文采用了(这里未使用)

  

        // 将输入的数据集分割成小数据块splites,提供一个RecordReder的实现

        job.setInputFormatClass(TextInputFormat.class);

        // 提供一个RecordWriter的实现,负责数据输出

        job.setOutputFormatClass(TextOutputFormat.class);

  这两句话不知是什么意思。起什么作用?如果您看到,可帮我解释下啦。

 

posted @ 2014-06-17 21:19  jseven  阅读(1210)  评论(0编辑  收藏  举报