Hadoop框架之——Mapreduce OutputFormat数据输出

OutputFormat是MapR educe输出的基类,所有实现MapReduce输出都实现了OutputF ormat接口。下面我们介绍几种常见的OutputFormat实现类。

1.文本输出TextOutputFormat

默认的输出格式是TextOutputFormat,它把每条记录写为文本行。它的键和值可以是任意类型,因为TextOutputF orm at调用toString0方法把它们转换为字符串。

2. SequenceFileOutputFormat

将SequenceFileOutputF orm at输出作为后续MapReduce任务的输入,这便是一种好的输出格式,因为它的格式紧凑,很容易被压缩。

3.自定义OutputFormat

根据用户需求,自定义实现输出。

4.使用场景

为了实现控制最终文件的输出路径和输出格式,可以自定YOutputFomat,例如:要在一-个MapReducc程序中根据数据的不同输出两类结果到不同目录,这类灵活的输出需求可以通过自定义outputFormat来实现。

5.自定义OutputFormat步骤

(1)自定义一个类继承FileOutputFormnat.

(2)改写RecordViter, 具体改写输出数据的方法wite0。

自定义OutputFormat案例实操

1.需求
过滤输入的log日志,包含atguigu的网站输出到e:/atguigu.log,不包含atguigu的网站输出到e:/other.log。

(1)输入数据 log.txt

http://www.baidu.com
http://www.google.com
http://cn.bing.com
http://www.atguigu.com
http://www.sohu.com
http://www.sina.com
http://www.sin2a.com
http://www.sin2desa.com
http://www.sindsafa.com

(2)期望输出数据

atguigu.log

http://www.atguigu.com

order.log

http://cn.bing.com
http://www.baidu.com
http://www.google.com
http://www.sin2a.com
http://www.sin2desa.com
http://www.sina.com
http://www.sindsafa.com
http://www.sohu.com

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)编写FilterReducer类

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 = line+"\r\n"  ;

        k.set( line );

        for (NullWritable value : values) {
            context.write( k,NullWritable.get() );
        }
    }
}

(3)自定义一个OutputFormat类

public class FilterOutputFormat extends FileOutputFormat<Text, NullWritable> {
    @Override
    public RecordWriter<Text, NullWritable> getRecordWriter(TaskAttemptContext job) throws IOException, InterruptedException {
		// 创建一个RecordWriter
        return new FRecordWriter(job);
    }
}

(4)编写RecordWriter类

public class FRecordWriter extends RecordWriter<Text, NullWritable> {


    FSDataOutputStream fosatguigu;
    FSDataOutputStream fosother;

    public FRecordWriter(TaskAttemptContext job) {

        try {
            //TODO 获取fs文件
            FileSystem fs = FileSystem.get( job.getConfiguration() );
            //TODO 创建atguigu.log输出流
            fosatguigu = fs.create( new Path( "E:\\atguigu.log" ) );
            //TODO 创建orter.log输出流
            fosother = fs.create( new Path( "E:\\other.log" ) );

        } catch (IOException e) {
            e.printStackTrace();
        }


    }

    @Override
    public void write(Text key, NullWritable value) throws IOException, InterruptedException {
        //TODO 判断key当中是否含有atguigu,如果有写出atguigu.log  , 如果没有写出到other.log
        if (key.toString().contains( "atguigu" )) {
            fosatguigu.write( key.toString().getBytes() );
        } else {
            fosother.write( key.toString().getBytes() );
        }
    }

    @Override
    public void close(TaskAttemptContext context) throws IOException, InterruptedException {

        IOUtils.closeStream( fosatguigu );
        IOUtils.closeStream( fosother );

    }
}

(5)编写FilterDriver类

public class FilterDriver {
    public static void main(String[] args) throws Exception {

        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);
    }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值