MapReduce之自定义OutPutFormat

MapReduce之自定义OutPutFormat

OutputFormat接口实现类

	OutputFormat是MapReduce输出的基类,所有实现MapReduce输出都实现了OutputFormat接口。下面介绍几种常见的OutputFormat实现类:
	1.文本输出TextoutputFormat
	默认的输出格式是TextOutputFormat,它把每条记录写为文本行。它的键和值可以是任意类型,因为TextOutputFormat调用toString()方法把它们转换为字符串。
	2.SequenceFileOutputFormat
	将SecquenceFileOutputFormat输出作为后续MapReduce任务的输入,这便是一种好的输出格式,因为它的格式紧凑,很容易被压缩。
	3.自定义OutputFormat
	根据用户需求,自定义实现输出。

自定义OutputFormat使用场景及步骤

使用场景

	为了实现控制最终文件的输出路径和输出格式,可以自定义OutputFormat。
	例如:要在一个MapReduce程序中根据数据的不同输出两类结果到不同目录,这类灵活的输出需求可以通过自定义OutputFormat来实现。
	自定义OutputFormat步骤
(1)自定义一个类继承FileOutputFormat。
(2)改写RecordWriter,具体改写输出数据的方法write()

自定义OutputFormat 案例实操

需求

​ 过滤输入的log日志,包含atguigu的网站输出到D:\hadoop\outputformat\atguigu.txt,不包含atguigu的网站输出到D:\hadoop\outputformat\other.txt。

什么时候需要Reduce

①合并
②需要对数据排序

所以我们这个案例不需要Reduce过程

程序类编写

LogMapper类

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;

/**
 * @author Ronin
 * 过滤输入的log日志,包含atguigu的网站输出到D:\hadoop\outputformat\atguigu.txt,
 * 不包含atguigu的网站输出到D:\hadoop\outputformat\other.txt。
 */
public class LogMapper extends Mapper<LongWritable, Text,Text, NullWritable> {
    @Override
    protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
        //直接写出
        context.write(value,NullWritable.get());
    }
}

LogRecordWriter类

import org.apache.hadoop.fs.FSDataOutputStream;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
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;

/**
 * @author Ronin
 * 过滤输入的log日志,包含atguigu的网站输出到D:\hadoop\outputformat\atguigu.txt,
 * 不包含atguigu的网站输出到D:\hadoop\outputformat\other.txt。
 */
public class LogRecordWriter extends RecordWriter<Text, NullWritable> {

    private FSDataOutputStream atguiguOut;
    private FSDataOutputStream otherOut;
    private Path atguipath = new Path("D:\\hadoop\\outputformat\\atguigu.txt");
    private Path otherpath = new Path("D:\\hadoop\\outputformat\\other.txt");
    private FileSystem fs;

    public LogRecordWriter(TaskAttemptContext job) {
        //因为写到不同的文件里,所以要弄两个输出流
        //因此需要获取文件管理系统对象,然后创建输出流,上升为属性,挺高作用域
        try {
            fs = FileSystem.get(job.getConfiguration());
            atguiguOut = fs.create(atguipath);
            otherOut = fs.create(otherpath);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void write(Text key, NullWritable value) throws IOException{
        //判断用哪个流来写出
        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 {
        if (atguiguOut != null) {
            atguiguOut.close();
        }
        if (otherOut != null) {
            otherOut.close();
        }
        if (fs != null) {
            fs.close();
        }
    }
}

LogOutPutFormat

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;

/**
 * @author Ronin
 */
public class LogOutPutFormat extends FileOutputFormat<Text, NullWritable> {
    @Override
    public RecordWriter<Text, NullWritable> getRecordWriter(TaskAttemptContext job) throws IOException, InterruptedException {
        LogRecordWriter lrw =new LogRecordWriter(job);
        return lrw;
    }
}
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;

/**
 * @author Ronin
 */
public class LogDriver {
    public static void main(String[] args) throws IOException, ClassNotFoundException, InterruptedException {
        args = new String[]{"D:\\hadoop\\input\\inputoutputformat","D:\\hadoop\\output\\inputoutputformat"};
        Configuration conf = new Configuration();
        Job job = Job.getInstance(conf);
        job.setJarByClass(LogDriver.class);
        job.setMapperClass(LogMapper.class);
        job.setMapOutputKeyClass(Text.class);
        job.setMapOutputValueClass(NullWritable.class);
        job.setOutputKeyClass(Text.class);
        job.setOutputValueClass(NullWritable.class);
        job.setOutputFormatClass(LogOutPutFormat.class);
        FileInputFormat.setInputPaths(job,new Path(args[0]));
        //虽然我们自定义了outputformat,但是因为我们的outputformat继承自fileoutputformat
        //而fileoutputformat要输出一个_SUCCESS文件,所以在这还得指定一个输出目录
        FileOutputFormat.setOutputPath(job,new Path(args[1]));
        boolean b = job.waitForCompletion(true);
        System.exit(b?0:1);
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值