OutputFormat接口實(shí)現(xiàn)類
OutputFormat是MapReduce輸出的基類,所有實(shí)現(xiàn)MapReduce輸出都實(shí)現(xiàn)了OutputFormat接口梅垄。
3.1 OutputFormat接口實(shí)現(xiàn)類.png
默認(rèn)輸出格式TextOutputFormat
自定義OutputFormat
1)應(yīng)用場景
輸出數(shù)據(jù)到MySQL/HBase/ES等存儲(chǔ)框架中。
2)自定義OutputFormat步驟
自定義一個(gè)類繼承FileOutputFormat刮刑。
重寫RecordWriter毅戈,具體重寫輸出數(shù)據(jù)的方法write()夯秃。
實(shí)戰(zhàn)
LogRecordWriter.java
public class LogRecordWriter extends RecordWriter<Text, NullWritable> {
private FSDataOutputStream oneOut;
private FSDataOutputStream otherOut;
public LogRecordWriter(TaskAttemptContext job) {
// 創(chuàng)建流
try {
FileSystem fileSystem = FileSystem.get(job.getConfiguration());
oneOut = fileSystem.create(
new Path(System.getProperty("user.dir")+"/output/outputfromat/one.txt"));
otherOut = fileSystem.create(
new Path(System.getProperty("user.dir")+"/output/outputfromat/other.txt"));
} catch (IOException e) {
e.printStackTrace();
}
}
@Override
public void write(Text key, NullWritable value) throws IOException, InterruptedException {
// 具體寫
String line = key.toString();
if(line.contains("https")){
oneOut.writeBytes(line+"\n");
}else{
otherOut.writeBytes(line+"\n");
}
}
@Override
public void close(TaskAttemptContext context) throws IOException, InterruptedException {
IOUtils.closeStream(oneOut);
IOUtils.closeStream(otherOut);
}
}
LogOutputFormat.java
public class LogOutputFormat extends FileOutputFormat<Text, NullWritable> {
@Override
public RecordWriter<Text, NullWritable> getRecordWriter(TaskAttemptContext job)
throws IOException, InterruptedException {
LogRecordWriter logRecordWriter = new LogRecordWriter(job);
return logRecordWriter;
}
}
LogMapper.java
public class LogMapper extends Mapper<LongWritable, Text, Text, NullWritable> {
@Override
protected void map(LongWritable key, Text value,
Mapper<LongWritable, Text, Text, NullWritable>.Context context)
throws IOException, InterruptedException {
context.write(value, NullWritable.get());
}
}
LogReducer.java
public class LogReducer extends Reducer<Text, NullWritable, Text, NullWritable> {
@Override
protected void reduce(Text key, Iterable<NullWritable> values,
Reducer<Text, NullWritable, Text, NullWritable>.Context context)
throws IOException, InterruptedException {
for (NullWritable value : values) {
context.write(key,NullWritable.get());
}
}
}
LogDriver.java
public class LogDriver {
public static void main(String[] args) throws Exception {
Configuration conf = new Configuration();
Job job = Job.getInstance(conf);
job.setJarByClass(LogDriver.class);
job.setMapperClass(LogMapper.class);
job.setReducerClass(LogReducer.class);
job.setMapOutputKeyClass(Text.class);
job.setMapOutputValueClass(NullWritable.class);
job.setOutputKeyClass(Text.class);
job.setOutputValueClass(NullWritable.class);
// 設(shè)置自定義的OutputFormat
job.setOutputFormatClass(LogOutputFormat.class);
FileInputFormat.setInputPaths(job, new Path(System.getProperty("user.dir")+"/input/outputfromat"));
// 雖然我們定義類OutputFormat,但是因?yàn)槲覀兊腛utputFormat繼承自FileOutputFormat
// 而FileOutputFormat要輸出一個(gè)_SUCCESS文件徽惋,所以還得指定一個(gè)輸出目錄
FileOutputFormat.setOutputPath(job, new Path(System.getProperty("user.dir")+"/output/outputfromat"));
boolean b = job.waitForCompletion(true);
System.exit(b?0:1);
}
}
小結(jié)
本節(jié)我們了解到了OutputFormat數(shù)據(jù)輸出疆拘,認(rèn)識(shí)到自定義輸出流。在實(shí)際應(yīng)用中寂曹,F(xiàn)ileOutputFormat并不能完全滿足需求,可以通過繼承OutputFormat來自定義輸出回右。