package com.doit.hdp.day02;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.DoubleWritable;
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 ScoreDemo {
static class ScoreMap extends Mapper<LongWritable , Text , Text,DoubleWritable >{
Text k= new Text();
DoubleWritable v = new DoubleWritable();
@Override
protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
try {
String line = value.toString();
String[] arr = line.split(",");
String name=arr[1];
Double score =Double.parseDouble(arr[2]);
k.set(name);
v.set(score);
context.write(k,v);
} catch (Exception e) {
e.printStackTrace();
}
}
}
static class ScoreReduce extends Reducer<Text,DoubleWritable,Text,DoubleWritable>{
DoubleWritable v=new DoubleWritable();
protected void reduce(Text key, Iterable<DoubleWritable> values, Context context) throws IOException, InterruptedException {
int count=0;
double sum=0d;
for (DoubleWritable value : values) {
double score = value.get();
// System.out.println(score);
sum+=score;
count++;
}
double avg=sum/count;
v.set(avg);
context.write(key,v);
}
}
public static void main(String[] args) throws Exception {
Configuration conf = new Configuration();
Job job = Job.getInstance(conf,ScoreDemo.class.getSimpleName());
job.setMapperClass(ScoreMap.class);
job.setReducerClass(ScoreReduce.class);
job.setMapOutputKeyClass(Text.class);
job.setMapOutputValueClass(DoubleWritable.class);
job.setOutputKeyClass(Text.class);
job.setOutputValueClass(DoubleWritable.class);
FileInputFormat.addInputPath(job,new Path("D:\\doit_wc\\stu\\"));
FileOutputFormat.setOutputPath(job,new Path("D:\\doit_wc\\stu_01\\"));
job.waitForCompletion(true);
}
}