Mapreduce中自定义OutputFormat
程序员文章站
2022-06-30 09:10:30
...
Mapreduce中自定义OutputFormat
OutputFormat接口实现类
OutputFormat是MapReduce输出的基类,所有实现MapReduce输出都实现了OutputFormat接口
- 文本输出TextOutputFormat
默认的输出格式是TextOutputFormat,它把每条记录写为文本行。它的键和值可以是任意类型,因为TextOutputFormat调用toString0方法把它们转换为字符串。
- SequenceFileOutputFormat
将SequenceFileOutputFormat输出作为后续MapReduce任务的输入,这便是-种好的输出格式,因为它的格式紧凑,很容易被压缩。
- 自定义OutputFormat
为了实现控制最终文件的输出路径和输出格式,可以自定义OutputFormat。比如要在一个MR程序中根据数据的不同,输出两类结果到不同目录。这类灵活的输出需求可以通过自定义OutputFormat来实现。
自定义OutputFormat步骤:
(1) 自定义一个类继承FileOutputFormat
(2) 改写RecordWriter,具体改写输出数据的方法write()
自定义OutputFormat
-
需求分析
过滤输入的数据,将包含atguigu的网站输出到一个文件,不包含atguigu的网站输出到另一个文件 -
输入数据
-
编写Mapper类
package com.atguigu.mr.outputformat;
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 FileMapper extends Mapper<LongWritable, Text, Text, NullWritable> {
@Override
protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
context.write(value,NullWritable.get());
}
}
- 编写Reducer类
package com.atguigu.mr.outputformat;
import org.apache.hadoop.io.NullWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Reducer;
import java.io.IOException;
public class FileReducer extends Reducer<Text, NullWritable,Text,NullWritable> {
Text k = new Text();
@Override
protected void reduce(Text key, Iterable<NullWritable> values, Context context) throws IOException, InterruptedException {
// 1 获取一行
String line = key.toString();
// 2 拼接
line = line + "\r\n";
// 3 设置key
k.set(line);
// 4 输出
context.write(k, NullWritable.get());
}
}
- 编写RecordWriter类
package com.atguigu.mr.outputformat;
import org.apache.hadoop.fs.FSDataOutputStream;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IOUtils;
import org.apache.hadoop.io.NullWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.RecordWriter;
import org.apache.hadoop.mapreduce.TaskAttemptContext;
import java.io.IOException;
public class FileRecordWriter extends RecordWriter<Text, NullWritable> {
FSDataOutputStream atguiguOut = null;
FSDataOutputStream otherOut = null;
public FileRecordWriter(TaskAttemptContext job) {
// 1 获取文件系统
FileSystem fs;
try {
fs = FileSystem.get(job.getConfiguration());
// 2 创建输出文件路径
Path atguiguPath = new Path("D:\\idea project\\MapReduce\\atguigu.log");
Path otherPath = new Path("D:\\idea project\\MapReduce\\other.log");
// 3 创建输出流
atguiguOut = fs.create(atguiguPath);
otherOut = fs.create(otherPath);
} catch (IOException e) {
e.printStackTrace();
}
}
@Override
public void write(Text key, NullWritable value) throws IOException, InterruptedException {
// 判断是否包含“atguigu”输出到不同文件
if (key.toString().contains("atguigu")) {
atguiguOut.write(key.toString().getBytes());
} else {
otherOut.write(key.toString().getBytes());
}
}
@Override
public void close(TaskAttemptContext context) throws IOException, InterruptedException {
// 关闭资源
IOUtils.closeStream(atguiguOut);
IOUtils.closeStream(otherOut); }
}
- 自定义一个OutputFormat类
package com.atguigu.mr.outputformat;
import org.apache.hadoop.io.NullWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.RecordWriter;
import org.apache.hadoop.mapreduce.TaskAttemptContext;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
import java.io.IOException;
public class File_OutputFormat extends FileOutputFormat<Text, NullWritable> {
@Override
public RecordWriter<Text, NullWritable> getRecordWriter(TaskAttemptContext job)throws IOException, InterruptedException {
// 创建一个RecordWriter
return new FileRecordWriter(job);
}
}
- 编写Driver类
package com.atguigu.mr.outputformat;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.NullWritable;
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;
import java.io.IOException;
public class FileDriver {
public static void main(String[] args) throws Exception {
//1 获取Job对象
Configuration conf = new Configuration();
Job job = Job.getInstance(conf);
//2 设置jar存储位置
job.setJarByClass(FileDriver.class);
//3 关联Mapper和Reducer类
job.setMapperClass(FileMapper.class);
job.setReducerClass(FileReducer.class);
//4 设置map阶段输出的kv类型
job.setMapOutputKeyClass(Text.class);
job.setMapOutputValueClass(NullWritable.class);
//5 设置最终的输出类型
job.setOutputKeyClass(Text.class);
job.setOutputValueClass(NullWritable.class);
//6 要将自定义的输出格式组件设置到job中
job.setOutputFormatClass(File_OutputFormat.class);
//7 设置输入输出路径
FileInputFormat.setInputPaths(job, new Path("D:\\idea project\\MapReduce\\web.txt"));
// 虽然我们自定义了outputformat,但是因为我们的outputformat继承自fileoutputformat
// 而fileoutputformat要输出一个_SUCCESS文件,所以在这还得指定一个输出目录
FileOutputFormat.setOutputPath(job, new Path("D:\\idea project\\MapReduce\\output777"));
//8 提交Job
boolean result = job.waitForCompletion(true);
System.exit(result ? 0 : 1);
}
}
- 查看结果
上一篇: PHP实现合并discuz用户
下一篇: 用PS制作1寸证件照方法介绍