栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 前沿技术 > 大数据 > 大数据系统

Hadoop之MapReduce的应用案例

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);
		}
}

效果(完善代码过后):

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

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/688072.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号