• Hadoop3教程(十六):MapReduce中的OutputFormat


    (105)OutputFormat概述

    我们之前讲过了Map阶段的InputFormat,对应的,Reduce阶段也有自己的OutputFormat。

    Reducer在执行完reduce()之后,接下来就会通过OutputFormat来将处理结果输出至外界环境。

    Hadoop里默认使用的是TextOutputFormat,即将reduce()的处理结果,按行输出到文件。

    而OutputFormat是MapReduce输出的基类,所有实现了MR输出的程序,都必须实现OutputFormat接口。

    OutputFormat有几种官方自带的实现类(具体功能就不展开了):

    • NullOutputFormat
    • FileOutputFormat
      • MapFileOutputFormat
      • SequenceFileOutputFormat
      • TextOutputFormat(默认)
    • FilterOutputFormat
      • LazyOutputFormat
    • DBOutputFormat

    OutputFormat类的核心方法:public abstract RecordWriter getRecordWriter(...)

    最终结果怎么写,以什么形式写,写到哪儿,等等这些,都是在getRecordWriter()里控制的。

    当然,这些自带的实现类在日常的生产中肯定是不足以满足各种情况的,所以多数情况下,我们会实现自定义的OutputFormat类

    自定义OutputFormat实现类需要:

    • 继承FileOutputFormat;
    • 改写RecordWriter,具体改写输出数据的方法write()

    (106)自定义OutputFormat案例需求分析

    需求:输入是一个日志文件,即log.txt,里面是罗列了一些访问过的网站,现在需要把其中包含atguigu的网站输出到a.log,不包含atguigu的网站输出到b.log。

    输入数据形如:

    http://www.baidu.com
    http://www.atguibu.com
    ...
    
    • 1
    • 2
    • 3

    我们需要自定义一个OutputFormat类,即创建一个类LogRecordWriter继承RecordWriter,然后创建两个文件输出流,一个是atguiguOut,一个是otherOut。如果输入数据包含atguigu,就输出到atguiguOut,反之则输出到otherOut流。

    最后还需要在驱动类里注册一下:

    job.setOutputFormatClass(LogOutputFormat.class);
    
    • 1

    附注:

    其实这个需求从直观上来讲,是可以通过分区来实现类似功能的,但是很遗憾,分区的话无法控制输出文件的名字,所以没法严格符合需求。

    (107/108)自定义OutputFormat案例实现

    这里直接复制了教程里的代码,来介绍一下,如何针对上一小节提出的需求,自定义OutputFormat。

    自定义Mapper

    首先需要创建一个自定义的Mapper类,如class LogMapper extends Mapper

    package com.atguigu.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());
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16

    自定义Reducer

    然后新建一个自定义Reducer类:

    package com.atguigu.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,NullWritable.get());
            }
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17

    自定义OutputFormat

    这里是最重要的一步,就是自定义一个OutputFormat类,继承RecordWriter:

    • 创建两个文件的输出流:atguiguOut、otherOut;
    • 如果输入数据中含有atguigu,则输出至atguiguOut,反之则输出到otherOut;

    首先自定义OutputFormat类,重写RecordWriter方法,将我们自定义的LogRecordWriter放进去。

    package com.atguigu.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 {
            //创建一个自定义的RecordWriter返回
            LogRecordWriter logRecordWriter = new LogRecordWriter(job);
            return logRecordWriter;
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18

    然后编写LogRecordWriter类,:

    package com.atguigu.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("d:/hadoop/atguigu.log"));
                otherOut = fs.create(new Path("d:/hadoop/other.log"));
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    
        @Override
        public void write(Text key, NullWritable value) throws IOException, InterruptedException {
            String log = key.toString();
            //根据一行的log数据是否包含atguigu,判断两条输出流输出的内容
            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);
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48

    Driver

    最后编写LogDriver驱动类,把我们前面自定义的的类统统在驱动类里注册上:

    package com.atguigu.mapreduce.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 LogDriver {
        public static void main(String[] args) throws IOException, ClassNotFoundException, InterruptedException {
    
            Configuration conf = new Configuration();
            Job job = Job.getInstance(conf);
    
            job.setJarByClass(LogDriver.class);
            job.setMapperClass(LogMapper.class);
            job.setReducerClass(LogReducer.class);
    
            job.setMapOutputKeyClass(Text.class);
            job.setMapOutputValueClass(NullWritable.class);
    
            job.setOutputKeyClass(Text.class);
            job.setOutputValueClass(NullWritable.class);
    
            //设置自定义的outputformat
            job.setOutputFormatClass(LogOutputFormat.class);
    
            FileInputFormat.setInputPaths(job, new Path("D:\\input"));
            //虽然我们自定义了outputformat,但是因为我们的outputformat继承自fileoutputformat
            //而fileoutputformat要输出一个_SUCCESS文件,所以在这还得指定一个输出目录
            FileOutputFormat.setOutputPath(job, new Path("D:\\logoutput"));
    
            boolean b = job.waitForCompletion(true);
            System.exit(b ? 0 : 1);
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40

    至此需求完成。

    参考文献

    1. 【尚硅谷大数据Hadoop教程,hadoop3.x搭建到集群调优,百万播放】
  • 相关阅读:
    如何使用Python抓取PDF文件并自动下载到本地
    Perceptron, Support Vector Machine and Dual Optimization Problem (3)
    手把手教你解决ClassCastException类型转换异常
    需求变化频繁的情况下,如何实施自动化测试
    laravel 多条件结合scope查询作用域优化
    sklearn实现多项式线性回归_一元/多元 【Python机器学习系列(八)】
    C++之10|50例学懂C++
    Python自动化测试之request库(五)
    面试必刷算法TOP101之回溯篇 TOP34
    【附源码】Python计算机毕业设计农业技术学习平台
  • 原文地址:https://blog.csdn.net/wlh2220133699/article/details/133871153