package com.yinke.wsw.rating;
import org.apache.hadoop.conf.Configuration;
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;
import java.io.IOException;
public class RatingDemo {
private static class MyMap extends Mapper<LongWritable, Text,Text,IntWritable> {
Text k = new Text();
IntWritable v = new IntWritable();
//重写map方法
@Override
protected void map(LongWritable key, Text value, Mapper<LongWritable, Text, Text, IntWritable>.Context context) throws IOException, InterruptedException {
//每一行包括课程号、课程名称、学号、学生姓名、成绩(百分制),数据项由空格隔开。
String string = value.toString();
//按行分割
String[] strings = string.split("\n");
for (int i = 0; i < strings.length; i++) {
//以空格分割
String[] data = strings[i].split(" ");
//数组第5个元素是成绩
double score = Double.parseDouble(data[4]);
//以成绩进行分割
if (score < 60) {
k.set("不及格");
v.set(1);
} else if (score >= 60 && score < 70) {
k.set("一般");
v.set(1);
} else if (score >= 70 && score < 80) {
k.set("中等");
v.set(1);
} else if (score >= 80 && score < 90) {
k.set("良好");
v.set(1);
} else if (score >= 90) {
k.set("优秀");
v.set(1);
}
//生成key value键值对
context.write(k, v);
}
}
private static class MyReduce extends Reducer<Text, IntWritable, Text, IntWritable> {
//重写reduce方法
@Override
protected void reduce(Text key, Iterable<IntWritable> values, Reducer<Text, IntWritable, Text, IntWritable>.Context context) throws IOException, InterruptedException {
int count = 0;
//遍历values
for (IntWritable v : values) {
int value = v.get();
count += value;
}
//汇总每个分数段的人数
context.write(key, new IntWritable(count));
}
}
public static void main(String[] args) throws IOException, ClassNotFoundException, InterruptedException {
// 1 获取配置信息以及封装任务
Configuration configuration = new Configuration();
Job job = Job.getInstance(configuration);
// 2 设置jar加载路径
job.setJarByClass(RatingDemo.class);
// 3 设置map和reduce类
job.setMapperClass(MyMap.class);
job.setReducerClass(MyReduce.class);
// 4 设置map输出
job.setMapOutputKeyClass(Text.class);
job.setMapOutputValueClass(IntWritable.class);
// 5 设置最终输出kv类型
job.setOutputKeyClass(Text.class);
job.setOutputValueClass(IntWritable.class);
// 6 设置输入和输出路径
Path input = new Path("/input/report.txt");
FileInputFormat.addInputPath(job, input);
Path output = new Path("/output/result");
//判断output文件夹是否存在,如果存在则删除
if (output.getFileSystem(configuration).exists(output)) {
output.getFileSystem(configuration).delete(output);
}
FileOutputFormat.setOutputPath(job, output);
//等待计算完成
boolean completion = job.waitForCompletion(true);
if (completion) {
System.out.println("计算成功!");
} else {
System.out.println("计算失败!");
}
}
}
}
如果本篇文章对你有帮助,还请一键三连【开心】