map处理的是一个纯文本文件,文件中存放的是数据时每一行标识一个学生的姓名和他相应一科成绩。mapper处理的数据由inputFormat分解过的数据集,其中inputFormat的作用是将数据集切割成小数据集inputsplit,每一个inputsplit将有 一个mapper负责处理,此外inputFormat中还提供了recordreader的实现,并将一个inputsplit解析成
import java.io.IOException
import java.util.Iterator
import java.util.StringTokenier;
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.Readucer
import org.apache.hadoop.mapreduc.lib.input.FileInputFormat
import org.apache.hadoop.maoreduce.lib.input.TextInputFormat
import org.apache.hadoop.....
public static class Map extends Mapper<LongWritable,Text,Text,IntWritable>{
//实现map函数
public void map(LongWritable key,Text value,Context context){
String ling=value.toString();
StringTokenizer tokenizerArticle =new StringTokenizer(line,"\n");
while(tokenierArticle.hashMoreElements()){
StringTokenizer tokenizerLine=new StringTokenizer(tokenizerArticle.nextToken());
String strName=tokenizerLine.nextToken();
String strScore=tokenizerLine.nextToken();
Text name=new Text(strName);
int socreInt=Integer.parseInt(strScore);
context.write(name,new IntWritable(scoreInt));
}
}
}
public static class Reduce extends Reducer <Text,IntWritable,Text,IntWritable>{
public void reduce(Text key,Itertor<IntWritable> values,Context context){
int sum=0;
int count=0;
Iterator <IntWritable> iterator =values.iterator();
while(iterator.hashNext()){
sum+=iterator.next().get();
count++;
}
int average=(int)sum/count;
context.write(key,new IntWritable(average));
}
}
public static void main(String args[]){
Configuration conf=new Configuration();
conf.set("mapred.job.tracker","192.168.224.100");
String [] ioArgs=new String []{"score_in","score_out"};
String [] otherArgs=new GenericOptionParser(conf,ioArgs).getRemainingArgs();
if (otherArgs.length != 2) {
System.err.println("Usage: Score Average <in> <out>");
System.exit(2);
}
Job job = new Job(conf, "Score Average");
job.setJarByClass(Score.class);
// 设置Map、Combine和Reduce处理类
job.setMapperClass(Map.class);
job.setCombinerClass(Reduce.class);
job.setReducerClass(Reduce.class);
// 设置输出类型
job.setOutputKeyClass(Text.class);
job.setOutputValueClass(IntWritable.class);
// 将输入的数据集分割成小数据块splites,提供一个RecordReder的实现
job.setInputFormatClass(TextInputFormat.class);
// 提供一个RecordWriter的实现,负责数据输出
job.setOutputFormatClass(TextOutputFormat.class);
// 设置输入和输出目录
FileInputFormat.addInputPath(job, new Path(otherArgs[0]));
FileOutputFormat.setOutputPath(job, new Path(otherArgs[1]));
System.exit(job.waitForCompletion(true) ? 0 : 1);
}
}