Hadoop之MapReduce的应用案例

Hadoop之MapReduce的应用案例,第1张

Hadoop之MapReduce的应用案例

如题:

学生id,课程id,分数
1,12,80
2,30,91
2,21,98
3,2,100
4,1,50
4,2,58
4,3,65
1,11,75

请实现:

(1)找出每门课不及格学生信息按照格式 “课程id:学生id1,学生id2”格式输出。

(2)找到所有科目都及格的学生id,按照id的升序排序。

(3)统计每门课的学生分数段按照 如下格式输出:课程id:[100,90]分数段学生人数,(90,80]分数段学生人数,(80,70]分数段学生人数,(70,60]分数段学生人数,不及格学生人数。

(1)直接上代码

public class demo01 {
	static class m extends 
	Mapper {
		@Override
		protected void map(LongWritable key, Text value,
	 Mapper.Context context)
				throws IOException, InterruptedException {
			String l = value.toString();
			String[] a = l.split(",");
			String a1 = a[0];
			String a2 = a[1];
			int a3 = Integer.parseInt(a[2]);
			if (a3 < 60) {
				context.write(new Text(a2), new Text(a1));
			}
		}
	}
		static class s extends Reducer
		 {
			@Override
			protected void reduce(Text key, 
			Iterable values, 
			Reducer.Context context)
			throws IOException, InterruptedException {
				for(Text v : values) {
				context.write(new Text(key), new Text(v));
				}
			}
		}
		public static void main(String[] args) throws
		 Exception {
			Configuration conf = new Configuration();
			Job job = Job.getInstance(conf);
			job.setJarByClass(demo01.class);
			job.setMapperClass(m.class);
			job.setReducerClass(s.class);
			job.setMapOutputKeyClass(Text.class);
			job.setMapOutputValueClass(Text.class);
			job.setOutputKeyClass(Text.class);
			job.setOutputValueClass(Text.class);
			FileInputFormat.setInputPaths(job, new Path(""));
			FileOutputFormat.setOutputPath(job, new Path(""));
			boolean res = job.waitForCompletion(true);
			System.exit(res ? 0 : 1);
		}
}

效果(代码完善后):

(2)上代码:

public class demo02 {
	static class m extends 
	Mapper {
		@Override
		protected void map(LongWritable key, Text value, 
	Mapper.Context context)
				throws IOException, InterruptedException {
			String l = value.toString();
			String[] a = l.split(",");
			String a1 = a[0];
			String a2 = a[1];
			int a3 = Integer.parseInt(a[2]);
			if (a3 >= 60) {
				context.write(new Text(a1), new Text(a2+a3));
			}
		}
	}
	static class s extends Reducer {
		@Override
		protected void 
		reduce(Text key, Iterable values, 
		Reducer.Context context)
				throws IOException, InterruptedException {
			for(Text v : values) {
			context.write(new Text(key), new Text(v));
			}
		}
	}
		public static void main(String[] args) throws 
		Exception{
			Configuration conf = new Configuration();
			Job job = Job.getInstance(conf);
			job.setJarByClass(demo02.class);
			job.setMapperClass(m.class);
			job.setReducerClass(s.class);
			job.setMapOutputKeyClass(Text.class);
			job.setMapOutputValueClass(Text.class);
			job.setOutputKeyClass(Text.class);
			job.setOutputValueClass(Text.class);
			FileInputFormat.setInputPaths(job, new Path(""));
			FileOutputFormat.setOutputPath(job, new Path(""));
			boolean res = job.waitForCompletion(true);
			System.exit(res ? 0 : 1);
		}
}

效果(完善代码过后):

(3)上代码:

public class demo03 {
	static class m extends 
	Mapper {
		@Override
		protected void map(LongWritable key, Text value, 
	Mapper.Context context)
				throws IOException, InterruptedException {
			String l = value.toString();
			String[] a = l.split(",");
			String a1 = a[0];
			String a2 = a[1];
			int a3 = Integer.parseInt(a[2]);
			
			 if (a3 >= 90 && a3 <= 100) {
				 a2 += "[90,100]";
		        } else if (a3 >= 80 && a3 < 90) {
		        	a2 += "[80,90)";
		        } else if(a3 >= 70 && a3 < 80){
		        	a2 += "[70,80)";
		        }else if(a3 >= 60 && a3 < 70){
		        	a2 += "[60,70)";
		        }else {
		        	a2 += "不及格";
				}
				context.write(new Text(a2), new Text(a1));
		}
	}
	static class s extends 
	Reducer {
		@Override
		protected void reduce(Text key, Iterable values,
	 Reducer.Context context)
				throws IOException, InterruptedException {
			int i = 0;
			for (Text  j: values) {
				i++;
			}
			context.write(new Text(key.toString()),
			 new IntWritable(i));
		}
	}
		public static void main(String[] args) throws 
		Exception {
			Configuration conf = new Configuration();
			Job job = Job.getInstance(conf);
			job.setJarByClass(demo03.class);
			job.setMapperClass(m.class);
			job.setReducerClass(s.class);
			job.setMapOutputKeyClass(Text.class);
			job.setMapOutputValueClass(Text.class);
			job.setOutputKeyClass(Text.class);
			job.setOutputValueClass(Text.class);
			FileInputFormat.setInputPaths(job, new Path(""));
			FileOutputFormat.setOutputPath(job, new Path(""));
			boolean res = job.waitForCompletion(true);
			System.exit(res ? 0 : 1);
		}
}

效果(完善代码过后):

例子挺简单的,思路就是这样,有问题欢迎交流!

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5688584.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-17
下一篇 2022-12-17

发表评论

登录后才能评论

评论列表(0条)

保存