前提準備:
1.hadoop安裝運行正常。Hadoop安裝配置請參考:Ubuntu下 Hadoop 1.2.1 配置安裝
2.集成開發環境正常。集成開發環境配置請參考 :Ubuntu 搭建Hadoop源碼閱讀環境
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 [email protected]
*
*/
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,"\n");
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