说明: 本文所涉及的JavaSE版本为1.8.0, IDEA版本为IntelliJ IDEA Community Edition 2018.3.5 x64。
需求:结合以前的MR案例(学生成绩二次排序),自定义FileOutputFormat和RecoreWriter,对其输出采用自定义文件名。
输入数据:
代码:
- map代码:
package com.gcs.SelfOutputFile; import org.apache.hadoop.io.LongWritable; import org.apache.hadoop.io.NullWritable; import org.apache.hadoop.io.Text; import org.apache.hadoop.mapreduce.Mapper; import java.io.IOException; public class SecSortMap extends Mapper<LongWritable, Text, SecSortWritable, NullWritable> { SecSortWritable ssw = new SecSortWritable(); @Override protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException { String str = value.toString(); String[] strArr = str.split(" "); ssw.setName(strArr[0]); ssw.setCourse(strArr[1]); ssw.setScore(Integer.parseInt(strArr[2])); context.write(ssw, NullWritable.get()); } }
- Reduce代码:
package com.gcs.SelfOutputFile; import org.apache.hadoop.io.NullWritable; import org.apache.hadoop.mapreduce.Reducer; import java.io.IOException; public class SecSortReduce extends Reducer<SecSortWritable, NullWritable, SecSortWritable, NullWritable> { @Override protected void reduce(SecSortWritable key, Iterable<NullWrit