package hadoop_test.avg_demo_03;import org.apache.hadoop.conf.Configuration;import org.apache.hadoop.fs.Path;import org.apache.hadoop.io.DoubleWritable;import org.apache.hadoop.io.IntWritable;import org.apache.hadoop.io.Text;import org.apache.hadoop.mapreduce.Job;import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;public class AvgDriver { public static void main(String[] args) throws Exception { System.setProperty("HADOOP_USER_NAME", "root"); Configuration conf = new Configuration(); Job job = Job.getInstance(conf); job.setJarByClass(AvgDriver.class); job.setMapperClass(AvgMapper.class); job.setReducerClass(AvgReducer.class); job.setMapOutputKeyClass(Text.class); job.setMapOutputValueClass(IntWritable.class); job.setOutputKeyClass(Text.class); job.setOutputValueClass(DoubleWritable.class); FileInputFormat.setInputPaths(job, new Path("/hadoop_test/avg/avg.txt")); FileOutputFormat.setOutputPath(job, new Path("/hadoop_test/avg/result")); job.waitForCompletion(true); }}
2、AvgMapperpackage hadoop_test.avg_demo_03;import org.apache.hadoop.io.IntWritable;import org.apache.hadoop.io.LongWritable;import org.apache.hadoop.io.Text;import org.apache.hadoop.mapreduce.Mapper;import java.io.IOException;public class AvgMapper extends Mapper
package hadoop_test.avg_demo_03;import org.apache.hadoop.io.DoubleWritable;import org.apache.hadoop.io.IntWritable;import org.apache.hadoop.io.Text;import org.apache.hadoop.mapreduce.Reducer;import java.io.IOException;public class AvgReducer extends Reducer
package hadoop_test.avg_hmk_03;import hadoop_test.Utils_hadoop;import hadoop_test.word_count_demo_01.WordCountCombine;import org.apache.hadoop.conf.Configuration;import org.apache.hadoop.fs.Path;import org.apache.hadoop.io.DoubleWritable;import org.apache.hadoop.io.IntWritable;import org.apache.hadoop.io.Text;import org.apache.hadoop.mapreduce.Job;import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;public class AvgDriver { public static void main(String[] args) throws Exception { System.setProperty("HADOOP_USER_NAME", "root"); Configuration conf = new Configuration(); Job job = Job.getInstance(conf); job.setJarByClass(AvgDriver.class); job.setMapperClass(AvgMapper.class); job.setCombinerClass(AvgCombine.class); job.setReducerClass(AvgReducer.class); job.setMapOutputKeyClass(Text.class); job.setMapOutputValueClass(Text.class); // 注意Map输出类型 job.setOutputKeyClass(Text.class); job.setOutputValueClass(DoubleWritable.class); FileInputFormat.setInputPaths(job, new Path("/hadoop_test/avg/avg.txt")); //FileOutputFormat.setOutputPath(job, new Path("/hadoop_test/avg/homework_result")); if( Utils_hadoop.testExist(conf,"/hadoop_test/avg/homework_result")){ Utils_hadoop.rmDir(conf,"/hadoop_test/avg/homework_result"); } FileOutputFormat.setOutputPath(job, new Path("/hadoop_test/avg/homework_result")); job.waitForCompletion(true); }}
2、AvgMapperpackage hadoop_test.avg_hmk_03;import org.apache.hadoop.io.IntWritable;import org.apache.hadoop.io.LongWritable;import org.apache.hadoop.io.Text;import org.apache.hadoop.mapreduce.Mapper;import java.io.IOException;public class AvgMapper extends Mapper
注意:将Map输出的Value变为Text。因为目标Combine格式为:人名 : 总成绩_考试次数 , 因为Combine使用的Context与Mapper保持一致,因此Map输出的value也需设为Text。
3、AvgCombinepackage hadoop_test.avg_hmk_03;import org.apache.hadoop.io.Text;import org.apache.hadoop.mapreduce.Reducer;import java.io.IOException;public class AvgCombine extends Reducer
package hadoop_test.avg_hmk_03;import org.apache.hadoop.io.DoubleWritable;import org.apache.hadoop.io.IntWritable;import org.apache.hadoop.io.Text;import org.apache.hadoop.mapreduce.Reducer;import java.io.IOException;public class AvgReducer extends Reducer