MyEclipse+MapReduce小作业

数据:
computer,huangxiaoming,85,86,41,75,93,42,85
computer,xuzheng,54,52,86,91,42
computer,huangbo,85,42,96,38
english,zhaobenshan,54,52,86,91,42,85,75
english,liuyifei,85,41,75,21,85,96,14
algorithm,liuyifei,75,85,62,48,54,96,15
computer,huangjiaju,85,75,86,85,85
english,liuyifei,76,95,86,74,68,74,48
english,huangdatou,48,58,67,86,15,33,85
algorithm,huanglei,76,95,86,74,68,74,48
algorithm,huangjiaju,85,75,86,85,85,74,86
computer,huangdatou,48,58,67,86,15,33,85
english,zhouqi,85,86,41,75,93,42,85,75,55,47,22
english,huangbo,85,42,96,38,55,47,22
algorithm,liutao,85,75,85,99,66
computer,huangzitao,85,86,41,75,93,42,85
math,wangbaoqiang,85,86,41,75,93,42,85
computer,liujialing,85,41,75,21,85,96,14,74,86
computer,liuyifei,75,85,62,48,54,96,15
computer,liutao,85,75,85,99,66,88,75,91
computer,huanglei,76,95,86,74,68,74,48
english,liujialing,75,85,62,48,54,96,15
math,huanglei,76,95,86,74,68,74,48
math,huangjiaju,85,75,86,85,85,74,86
math,liutao,48,58,67,86,15,33,85
english,huanglei,85,75,85,99,66,88,75,91
math,xuzheng,54,52,86,91,42,85,75
math,huangxiaoming,85,75,85,99,66,88,75,91
math,liujialing,85,86,41,75,93,42,85,75
english,huangxiaoming,85,86,41,75,93,42,85
algorithm,huangdatou,48,58,67,86,15,33,85
algorithm,huangzitao,85,86,41,75,93,42,85,75
题目:
利用map-reduce操作统计每门课程的参考人数和课程平均分

package 竞赛.studentScore;
import java.io.IOException; 
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
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 CourseOne { 
	public static class MyMapper extends Mapper<LongWritable, Text, Text, Text>{
		@Override
		protected void map(LongWritable key, Text value,Context context)
				throws IOException, InterruptedException {
			String[] lines = value.toString().split(",");
			long sum = 0L;
			//computer,huangxiaoming,85,86,41,75,93,42,85	
			long totalTimes = lines.length-2;			
			for (int i = 2; i < lines.length; i++) {
				sum += Long.parseLong(lines[i]);
			}	
			context.write(new Text(lines[0]), new Text(totalTimes+"_"+sum));//computer 7_sum
		}
	}	
	public static class MyReducer extends Reducer<Text,Text, Text, Text>{
		@Override
		protected void reduce(Text key, Iterable<Text> values,Context context)
				throws IOException, InterruptedException {		
			int count = 0;		
			int totalScore = 0;			
			int examTimes = 0;
			for (Text t : values) {
				String[] arrs = t.toString().split("_");
				count++;
				totalScore += Integer.parseInt(arrs[1]);
				examTimes += Integer.parseInt(arrs[0]);
			}					
			float avg = totalScore*1.0F/examTimes;		 
			context.write(key, new Text(count+"\t"+avg));//科目 参考人数 平均成绩
		}
	}	
	public static void main(String[] args) throws Exception {		
		Configuration conf = new Configuration();		
		Job job = Job.getInstance(conf);		
		job.setJarByClass(CourseOne.class);
		job.setMapperClass(MyMapper.class);
		job.setReducerClass(MyReducer.class);		
		job.setOutputKeyClass(Text.class);
		job.setOutputValueClass(Text.class);		
		FileInputFormat.setInputPaths(job, new Path("hdfs://192.168.216.128:9000/home/master/hadoop/input/studentsScore.txt"));
		FileOutputFormat.setOutputPath(job,new Path("hdfs://192.168.216.128:9000/home/master/hadoop/output/") );	
		boolean isDone = job.waitForCompletion(true);
		System.exit(isDone ? 0:1);
	}
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值