要求:利用MapReduce计算学生的平均成绩???
分析:
Mapper阶段
将学生的name和grade成绩context.write(name,grade),
实现<k1,v1>---> <k2,v2>的转换;
Reduce阶段
计算每个学生的平均成绩;【这个阶段实现了<name,[g1,g2,g3..】>,对k2已经进行分区和排序了;
代码阶段:如下
1.Mapper.class
public class MyAvgMapper extends Mapper<LongWritable,Text,Text,IntWritable>{
private Text name=new Text(); //全局变量
private IntWritable grade=new IntWritable();
@Override
protected void map(LongWritable key, Text value, Context context)
throws IOException, InterruptedException {
StringTokenizer line=new StringTokenizer(value.toString());
while(line.hasMoreTokens()){
String _name=line.nextToken();
String _grade=line.nextToken();
/*IntWritable cj=new IntWritable(Integer.parseInt(fs));
*context.write(new Text(name), cj);
*/
grade.set(Integer.parseInt(_grade));
name.set(_name);
context.write(name, grade); //实现拆分
}
}
}
StringTokenizer是用来拆分字符串的类;
StringTokenizer("字符串" ,"分隔符"):构造函数的参数第一个是拆分的字符串,第二个是分隔的字符,不写的话,默认用空格拆分。
2.Reduce.class
public class MyAvgReduce extends Reducer<Text,IntWritable,Text,IntWritable> {
private IntWritable avggrade=new IntWritable();
private int sum=0;
private int count=0;
@Override
protected void reduce(Text key, Iterable<IntWritable> values, Context context)
throws IOException, InterruptedException {
for(IntWritable grade:values){
sum+=grade.get();
count++;
}
avggrade.set(sum/count); //计算平均值
context.write(key, avggrade); //写入输出的结果
}
}
3.Driver.class
public class MyAvgDriver {
public static void main(String[] args) throws IllegalArgumentException, IOException, ClassNotFoundException, InterruptedException {
Configuration conf = new Configuration();
Path outfile=new Path("file:///D:/Out_Data");
FileSystem fs=outfile.getFileSystem(conf);
if(fs.exists(outfile)){
fs.delete(outfile,true);
}
Job job = Job.getInstance(conf);
job.setJarByClass(MyAvgDriver.class);
job.setJobName("mysort");
job.setMapperClass(MyAvgMapper.class);//输入数据方法
job.setReducerClass(MyAvgReduce.class);//计算结果
job.setOutputKeyClass(Text.class);
job.setOutputValueClass(IntWritable.class);
FileInputFormat.addInputPath(job, new Path("file:///D:/测试数据/平均成绩"));
FileOutputFormat.setOutputPath(job,outfile);
System.exit(job.waitForCompletion(true) ? 0 : 1);
//线程各个方法 封装每个方法到该类
}
}
4.输出结果
lisi 90
wangwu 86
zhangsan 84
zhaoliu 82
以上就是就是实现计算平均成绩的一段简单代码!!!望采纳!!