概述
说明: 本文所涉及的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<NullWritable> values, Context context) throws IOException, InterruptedException { context.write(key, NullWritable.get()); } }
- 自定义序列化类:
package com.gcs.SelfOutputFile; import lombok.Getter; import lombok.Setter; import org.apache.hadoop.io.WritableComparable; import java.io.DataInput; import java.io.DataOutput; import java.io.IOException; public class SecSortWritable implements WritableComparable<SecSortWritable> { private String name; private String course; private int score; public SecSortWritable(){ } public void setName(String name) { this.name = name; } public void setCourse(String course) { this.course = course; } public void setScore(int score) { this.score = score; } public String getName() { return name; } public int compareTo(SecSortWritable o) { if (this.name.hashCode() > o.name.hashCode()){ return -1; }else if (this.name.hashCode() < o.name.hashCode()){ return 1; }else{ return this.score > o.score ? -1 : 1; } } public void write(DataOutput dataOutput) throws IOException { dataOutput.writeChars(name); dataOutput.writeChar('