目录
1.OutputFormat概述
2.自定义OutputFormat
2.1应用场景
2.2 自定义OutputFormat步骤
3.自定义OutputFormat案例
3.1需求
?3.2代码实现
(1)编写LogMapper类
(2)编写LogReducer类
(3)编写自定义LogOutputFormat继承OutputFormat
? (4)? 编写LogRecordWriter类
(5)编写Driver类
OutputFormat是MapReduce输出的基类,所有实现了MapReduces输出都实现了OutputFormat接口。以下为OutputFormat的相关实现类。默认输出格式TextOutputFormat。
2.1应用场景
? ? ? ? 例如:输出数据到到MySql/Hbase等存储框架中
? ? ? ? 首先自定义一个类继承FileOutputFormat
? ? ? ? 然后RecordWriter,具体改写输出数据的方法write()
3.1需求
?3.2代码实现
(1)编写LogMapper类
package com.yangmin.mapreduce.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 LogMapper extends Mapper<LongWritable, Text,Text, NullWritable> {
@Override
protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
//不做任何处理,直接写出一行 log 数据
context.write(value, NullWritable.get());
}
}
(2)编写LogReducer类
package com.yangmin.mapreduce.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 LogReducer extends Reducer<Text, NullWritable,Text,NullWritable> {
@Override
protected void reduce(Text key, Iterable<NullWritable> values, Context context) throws IOException, InterruptedException {
for (NullWritable value : values) {
// 防止有相同的数据,迭代写出
context.write(key, value);
}
}
}
package com.yangmin.mapreduce.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 LogOutputFormat extends FileOutputFormat<Text, NullWritable> {
@Override
public RecordWriter<Text, NullWritable> getRecordWriter(TaskAttemptContext job) throws IOException, InterruptedException {
LogRecordWriter logRecordWriter = new LogRecordWriter(job);
return logRecordWriter;
}
}
? (4)? 编写LogRecordWriter类
package com.yangmin.mapreduce.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 LogRecordWriter extends RecordWriter<Text, NullWritable> {
private FSDataOutputStream atguiguOut;
private FSDataOutputStream otherOut;
public LogRecordWriter(TaskAttemptContext job){
//创建两条流
try {
FileSystem fs = FileSystem.get(job.getConfiguration());
atguiguOut = fs.create(new Path("C:\\ZProject\\bigdata\\output\\output-define-outputformat\\atguigu.log"));
this.otherOut = fs.create(new Path("C:\\ZProject\\bigdata\\output\\output-define-outputformat\\other.log"));
FSDataOutputStream otherOut = this.otherOut;
} catch (IOException e) {
e.printStackTrace();
}
}
@Override
public void write(Text key, NullWritable value) throws IOException, InterruptedException {
String log = key.toString();
if (log.contains("atguigu")){
atguiguOut.writeBytes(log+"\n");
}else {
otherOut.writeBytes(log+"\n");
}
}
@Override
public void close(TaskAttemptContext context) throws IOException, InterruptedException {
IOUtils.closeStream(atguiguOut);
IOUtils.closeStream(otherOut);
}
}
(5)编写Driver类
package com.yangmin.mapreduce.outputFormat;
import com.yangmin.mapreduce.wordcount.WordCountDriver;
import com.yangmin.mapreduce.wordcount.WordCountMapper;
import com.yangmin.mapreduce.wordcount.WordCountReducer;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IntWritable;
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 Driver {
public static void main(String[] args) throws IOException, ClassNotFoundException, InterruptedException {
// 1. 获取配置信息以及获取job对象
Configuration conf = new Configuration();
Job job = Job.getInstance(conf);
// 2. 设置jar包路径
job.setJarByClass(Driver.class);
//3. 关联mapper和reducer
job.setMapperClass(LogMapper.class);
job.setReducerClass(LogReducer.class);
//4.设置map输出的kv类型
job.setMapOutputKeyClass(Text.class);
job.setMapOutputValueClass(NullWritable.class);
//5. 设置最终输出的kv类型
job.setOutputKeyClass(Text.class);
job.setOutputValueClass(NullWritable.class);
//设置outputformat
job.setOutputFormatClass(LogOutputFormat.class);
//6.设置输出路径和输出路径
FileInputFormat.setInputPaths(job, new Path("C:\\ZProject\\bigdata\\input\\inputoutputformat"));
FileOutputFormat.setOutputPath(job, new Path("C:\\ZProject\\bigdata\\output\\output-define-outputformat\\111"));
//7.提交作业
boolean result = job.waitForCompletion(true);
System.exit(result ? 0 : 1);
}
}
|