一、outputFormat概述
由之前的MapReduce数据流向可知,数据从inputFormat输入到Mapper端,经过Shuffle后到达Reduce,再经过OutputFormat输出到文件。 而这outputFormat 和InputFormat是相对应的东西。相比于InputFormat,outPutFormat 细节没有那么多,仅仅完成对输出数据的格式化。
而InputFormat 涉及到对输入数据的解析和划分,继而影响到Map任务的数目,以及map任务的调度。
1.1 相关方法
- getRecordWriter
RecordWriter getRecordWriter(TaskAttemptContext context);
getRecordWriter用于返回一个RecordWriter的实例,Reduce任务在执行的时候就是利用这个实例来输出Key/Value的。(如果Job不需要Reduce,那么Map任务会直接使用这个实例来进行输出。
- checkOutSpecs
void checkOutputSpecs(JobContext context);
在JobClient 提交job之前被调用的,用于检测Job的输出路径,比如路径不存在就会创造路径
3)getOutputCommitter
OutputCommitter getOutputCommitter(TaskAttemptContext context);
commit 用于提供Job的输出环境。
1.2 子抽象类FileOutPutFormat:
checkOutPutSpecs():对该方法做了可具体的实现
1.3 具体的实现类:
ctrl+h
- TextOutputFormat :hadoop默认使用的。
写出去会调用toString方法,它把每条记录写为文本行。它的键和值可以是任意类型,因为TextOutputFormat调用toString()方法把他们转换为字符串。
- SequeceFileOutPutFormat
最终写出的文件是二进制格式的,是kv存储的。将SequeceFileOutPutFormat输出作为后续的MapReduce任务的输入,这便是一种好的输出格式,因为它的格式紧凑,很容易压缩。
- 自定义的outputFormat
- 自定义类继承FileOutOutFormat
- 自定义RecordWriter对象,完成数据的写出操作
二、自定义OutputFormat案例实操
1、需求
过滤输入的log日志,包含baidu的网站输出到baidu.log ,其他输出到other.log
数据集:
http://www.google.com
http://cn.bing.com
http://www.sohu.com
http://www.sina.com
http://www.sin2a.com
http://www.baidu.com
http://www.sin2desa.com
http://www.sindsafa.com
https://juejin.cn
2、案例实操
(1)编写FilterMapper类
public class FilterMapper extends Mapper<LongWritable, Text,Text, NullWritable> {
@Override
protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
//不做处理,直接输出
context.write(value,NullWritable.get());
}
}
(2)编写Reducer类
public class FilterReducer extends Reducer<Text, NullWritable,Text,NullWritable> {
Text k=new Text();
@Override
protected void reduce(Text key, Iterable<NullWritable> values, Context context) throws IOException, InterruptedException {
String line = key.toString();
line+="\r\n";
k.set(line);
context.write(k,NullWritable.get());
}
}
(3) 自定义outputFormat,重写RecordWrtier
public class FilterOutputFormat extends FileOutputFormat<Text, NullWritable> {
@Override
public RecordWriter<Text, NullWritable> getRecordWriter(TaskAttemptContext taskAttemptContext) throws IOException, InterruptedException {
return new FilterRecordWriter(taskAttemptContext);
}
}
(4)关键:自定义类继承RecordWriter
public class FilterRecordWriter extends RecordWriter<Text, NullWritable> {
FSDataOutputStream baiduOut = null;
FSDataOutputStream otherOut = null;
public FilterRecordWriter(TaskAttemptContext job){
//获取文件系统
FileSystem fs;
try {
fs=FileSystem.get(job.getConfiguration());
Path baiduPath =new Path("D:\\cs\\writable\\baiduout.log");
Path otherPath =new Path("D:\\cs\\writable\\other.log");
//3.创建输出流
baiduOut=fs.create(baiduPath);
otherOut=fs.create(otherPath);
} catch (IOException e) {
e.printStackTrace();
}
}
@Override
public void write(Text text, NullWritable nullWritable) throws IOException, InterruptedException {
// 判断是否包含“atguigu”输出到不同文件
if (text.toString().contains("baidu")) {
baiduOut.write(text.toString().getBytes());
} else {
otherOut.write(text.toString().getBytes());
}
}
@Override
public void close(TaskAttemptContext taskAttemptContext) throws IOException, InterruptedException {
// 关闭资源
IOUtils.closeStream(baiduOut);
IOUtils.closeStream(otherOut);
}
}
(5)Driver
public class FilterDriver {
public static void main(String[] args) throws IOException, InterruptedException, ClassNotFoundException {
// 输入输出路径需要根据自己电脑上实际的输入输出路径设置
args = new String[]{ "D:/cs/writable/input3", "D:/cs/writable/out4"};
Configuration conf = new Configuration();
Job job =Job.getInstance(conf);
job.setJarByClass(FilterDriver.class);
job.setMapperClass(FilterMapper.class);
job.setReducerClass(FilterReducer.class);
job.setMapOutputKeyClass(Text.class);
job.setMapOutputValueClass(NullWritable.class);
job.setOutputKeyClass(Text.class);
job.setOutputValueClass(NullWritable.class);
//要将自定义的输出格式组件设置到job中
job.setOutputFormatClass(FilterOutputFormat.class);
FileInputFormat.setInputPaths(job, new Path(args[0]));
// 虽然我们自定义了outputformat,但是因为我们的outputformat继承自fileoutputformat
// 而fileoutputformat要输出一个_SUCCESS文件,所以,在这还得指定一个输出目录
FileOutputFormat.setOutputPath(job, new Path(args[1]));
boolean result = job.waitForCompletion(true);
System.exit(result ? 0 : 1);
}
}