您的位置:首页 > 其它

MapReduce案例2——求学生平均成绩

2018-03-16 20:12 281 查看
题目:计算学生考试平均成绩 源数据:
张三 98
李四 96
王五 95
张三 90
李四 92
王五 99
张三 80
李四 90
王五 94
张三 82
李四 92

最终答案:
张三 98
李四 87
王五 86
上面的答案仅是格式,非正确数据思路:姓名作为key,成绩作为value,在map分组,在reduce中求平均值
类似于SQL中的select name, avg(score) from studentscore group by name;
代码:/**
* @author: lpj
* @date: 2018年3月16日 下午7:16:47
* @Description:
*/
package lpj.reduceWork;

import java.io.IOException;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileSystem;
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.Reducer;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
/**
*
*/
public class AverageSocreMR {

public static void main(String[] args) throws Exception {
Configuration conf = new Configuration();
// conf.addResource("hdfs-site.xml");//使用配置文件
// System.setProperty("HADOOP_USER_NAME", "hadoop");//使用集群
FileSystem fs = FileSystem.get(conf);//默认使用本地

Job job = Job.getInstance(conf);
job.setJarByClass(AverageSocreMR.class);
job.setMapperClass(AverageSocreMR_Mapper.class);
job.setReducerClass(AverageSocreMR_Reducer.class);

job.setMapOutputKeyClass(Text.class);
job.setMapOutputValueClass(Text.class);
job.setOutputKeyClass(Text.class);
job.setOutputValueClass(Text.class);
//
// String inputpath = args[0];
// String outpath = args[1];

Path inputPath = new Path("d:/a/homework2.txt");
Path outputPath = new Path("d:/a/homework2");
if (fs.exists(inputPath)) {
fs.delete(outputPath, true);
}

FileInputFormat.setInputPaths(job, inputPath);
FileOutputFormat.setOutputPath(job, outputPath);
boolean isdone = job.waitForCompletion(true);
System.exit(isdone ? 0 : 1);
}

public static class AverageSocreMR_Mapper extends Mapper<LongWritable, Text, Text, Text>{
Text kout = new Text();
Text valueout = new Text();
@Override
protected void map(LongWritable key, Text value,Context context)throws IOException, InterruptedException {
//李四 92
String [] reads = value.toString().trim().split(" ");
String kk = reads[0];
String vv = reads[1];
kout.set(kk);
valueout.set(vv);
context.write(kout, valueout);

}
}
public static class AverageSocreMR_Reducer extends Reducer<Text, Text, Text, Text>{
Text kout = new Text();
Text valueout = new Text();
@Override
protected void reduce(Text key, Iterable<Text> values, Context context)throws IOException, InterruptedException {
int sum = 0;
int count = 0;
int avg = 0;
for(Text text : values){
sum += Integer.parseInt(text.toString());
count++;
}
avg = sum / count;
valueout.set(avg + "");
context.write(key, valueout);

}

}

}
结果:张三 87
李四 92
王五 96
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
相关文章推荐