【大数据之Hadoop】十五、MapReduce之输出数据OutputFormat

【大数据之Hadoop】十五、MapReduce之输出数据OutputFormat_第1张图片
OutputFormat是MapReduce输出的基类,所有实现MapReduce输出都实现了OutputFormat接口。默认输出格式TextOutputFormat。

自定义OutputFormat:
应用与输出数据到指定的存储框架中存储。如:MySQL、HBase等。

步骤:

  1. 自定义一个类继承FileOutputFormat。
  2. 改写RecordWriter,具体改写输出数据的方法write()。
    例子:过滤输入的log日志,包含某个关键词的行输出到一个log,不包含关键词的行输出到另外一个log。
    【大数据之Hadoop】十五、MapReduce之输出数据OutputFormat_第2张图片
    LogMapper类
package com.study.mapreduce.logOutputFormat;

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

    }
}

LogReducer类

package com.study.mapreduce.logOutputFormat;

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

LogOutputFormat类继承FileOutputFormat

package com.study.mapreduce.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;

public class LogOutputFormat extends FileOutputFormat<Text, NullWritable> {
    @Override
    public RecordWriter<Text, NullWritable> getRecordWriter(TaskAttemptContext taskAttemptContext) throws IOException, InterruptedException {
        //创建一个自定义的RecordWriter返回
        LogRecordWriter logRecordWriter = new LogRecordWriter(taskAttemptContext);
        return logRecordWriter;
    }
}

编写LogRecordWriter类

package com.study.mapreduce.logOutputFormat;

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;

public class LogRecordWriter extends RecordWriter<Text, NullWritable> {

    private FSDataOutputStream baiduOut;
    private FSDataOutputStream otherOut;

    public LogRecordWriter(TaskAttemptContext taskAttemptContext) throws IOException {
        //获取文件系统对象
        FileSystem fs = FileSystem.get(taskAttemptContext.getConfiguration());
        //用文件系统对象创建两个输出流对应不同的目录
        baiduOut = fs.create(new Path("D:\\Liao_StudyProject\\MapReduceDemo\\src\\main\\attachment\\logOutputFormat\\baidu.log"));
        otherOut = fs.create(new Path("D:\\Liao_StudyProject\\MapReduceDemo\\src\\main\\attachment\\logOutputFormat\\other.log"));
    }
    
    @Override
    public void write(Text text, NullWritable nullWritable) throws IOException, InterruptedException {
    String log = text.toString();
    //根据一行的log数据是否包含atguigu,判断两条输出流输出的内容
    if(log.contains("baidu"))
    {
        baiduOut.writeBytes(log+"\n");
    }
    else
    {
        otherOut.writeBytes(log+"\n");
    }
    }

    @Override
    public void close(TaskAttemptContext taskAttemptContext) throws IOException, InterruptedException {
    // 关流
    baiduOut.close();
    otherOut.close();
    }
}

LogDriver类

package com.study.mapreduce.logOutputFormat;

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 {
        // 1 获取配置信息以及获取job对象
        Configuration conf = new Configuration();
        Job job = Job.getInstance(conf);

        // 2 关联本Driver程序的jar
        job.setJarByClass(LogDriver.class);

        // 3 关联Mapper和Reducer的jar
        job.setMapperClass(LogMapper.class);
        job.setReducerClass(LogReducer.class);

        // 4 设置Mapper输出的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("D:\\loginput"));
        //虽然我们自定义了outputformat,但是因为我们的outputformat继承自fileoutputformat
        //而fileoutputformat要输出一个_SUCCESS文件,所以在这还得指定一个输出目录
        FileOutputFormat.setOutputPath(job, new Path("D:\\logoutput"));

        // 7 提交job
        boolean b = job.waitForCompletion(true);
        System.exit(b ? 0 : 1);
    }
}

你可能感兴趣的:(hadoop,hadoop,大数据,mapreduce)