博客
关于我
【MapReduce】基础案例 ---- 自定义OutputFormat <根据内容输出到指定文件目录中>
阅读量:318 次
发布时间:2019-03-04

本文共 4487 字,大约阅读时间需要 14 分钟。


文章目录


OutputFormat是MR输出的基类,所有实现MR输出都实现了OutputFormat接口。

常见的OutputFormat实现类

1.文本输出TestOutputFormat

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

2.SequenceFileOutputFormat

  • 将SequenceFileOutputFormat输出作为后续MR任务的输入,这便是一种好的输出格式,因为它格式紧凑,很容易被压缩

3.自定义OutputFormat

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

  ☠ 自定义OutputFormat

在这里插入图片描述

案例

▪ 需求分析

在这里插入图片描述


▪ 代码实现

自定义FilterOutputFormat
  • 继承自FileOutputFormat,同时泛型应当与Reduce一致
  • 重写RecordWriter,自定义输出流。
public class FilterOutputFormat extends FileOutputFormat 
{ @Override public RecordWriter
getRecordWriter(TaskAttemptContext job) throws IOException, InterruptedException { return new FliterRecordWriter(job); }}
  • 重写FliterRecordWriter()方法,创建输出流,指定文件输出路径
  • 重写write()方法,定义文件输出时评判指标,根据判定使用指定的流输出到指定目录(文件中)
  • 关闭资源IO流
public class FliterRecordWriter extends RecordWriter
{ // 创建流对象 FSDataOutputStream fosatguigu; FSDataOutputStream fosother; // 构造输出流 public FliterRecordWriter(TaskAttemptContext job) { try { // 1.获取文件系统 FileSystem fs = FileSystem.get(job.getConfiguration()); // 2.创建输出到atguigu.log的输出流 fosatguigu = fs.create(new Path("G:\\Projects\\IdeaProject-C\\MapReduce\\src\\main\\java\\第三章_MR框架原理\\OutputFormat数据输出\\atguigu.log")); // 3.创建输出到other.log的输出流 fosother = fs.create(new Path("G:\\Projects\\IdeaProject-C\\MapReduce\\src\\main\\java\\第三章_MR框架原理\\OutputFormat数据输出\\other.log")); } catch (Exception e) { e.printStackTrace(); } } @Override public void write(Text key, NullWritable value) throws IOException, InterruptedException { // 判断key的内容是否包含atguigu 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 { // 关闭IO流 IOUtils.closeStream(fosatguigu); IOUtils.closeStream(fosother); }}


Mapper阶段
  • 读取数据,由于不需要进行内部操作,直接写出
public class FilterMapper extends Mapper
{ @Override protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException { // http://www.baidu.com// http://www.google.com // 写出 context.write(value,NullWritable.get()); }}


Reducer阶段
  • reducer阶段只是将读取的数据输出,这里注意将读取的内容进行格式化,增加换行,否则最终文件以字符串追加形式,一整行展现。
public class FilterReducer extends Reducer
{ Text k = new Text(); @Override protected void reduce(Text key, Iterable
values, Context context) throws IOException, InterruptedException { // http://www.baidu.com // 写出,添加换行符 String line = key.toString(); line = line + "\r\n"; k.set(line); // 循环防止有重复 for (NullWritable value:values){ context.write(k,NullWritable.get()); } }}


Driver阶段
public class FilterDriver {       public static void main(String[] args) {           Job job = null;        Configuration conf = new Configuration();        try {               // 获取job            job = Job.getInstance(conf);            // 配置            job.setMapperClass(FilterMapper.class);            job.setReducerClass(FilterReducer.class);            job.setJarByClass(FilterDriver.class);            job.setMapOutputKeyClass(Text.class);            job.setMapOutputValueClass(NullWritable.class);            job.setOutputKeyClass(Text.class);            job.setOutputValueClass(NullWritable.class);            // 将自定义的输出格式组件设置到job中            job.setOutputFormatClass(FilterOutputFormat.class);            // 设置输入输出路径            // 虽然我们自定义了outputformat,但是因为我们的outputformat继承自fileoutputformat            // 而fileoutputformat要输出一个_SUCCESS文件,所以,在这还得指定一个输出目录            FileInputFormat.setInputPaths(job,new Path("G:\\Projects\\IdeaProject-C\\MapReduce\\src\\main\\java\\第三章_MR框架原理\\OutputFormat数据输出\\log.txt"));            FileOutputFormat.setOutputPath(job,new Path("G:\\Projects\\IdeaProject-C\\MapReduce\\src\\main\\java\\第三章_MR框架原理\\Filteroutput"));            // 提交job            boolean result = job.waitForCompletion(true);            System.exit(result ? 0 : 1);        } catch (Exception e){               e.printStackTrace();        }    }}
  • 虽然我们自定义了outputformat,但是因为我们的outputformat继承自fileoutputformat,而fileoutputformat要输出一个_SUCCESS文件,所以,在这还得指定一个输出目录,用于存储_SUCCESS文件。
    在这里插入图片描述


转载地址:http://vueq.baihongyu.com/

你可能感兴趣的文章
mysql8 配置文件配置group 问题 sql语句group不能使用报错解决 mysql8.X版本的my.cnf配置文件 my.cnf文件 能够使用的my.cnf配置文件
查看>>
MySQL8.0.29启动报错Different lower_case_table_names settings for server (‘0‘) and data dictionary (‘1‘)
查看>>
MYSQL8.0以上忘记root密码
查看>>
Mysql8.0以上重置初始密码的方法
查看>>
mysql8.0新特性-自增变量的持久化
查看>>
Mysql8.0注意url变更写法
查看>>
Mysql8.0的特性
查看>>
MySQL8修改密码报错ERROR 1819 (HY000): Your password does not satisfy the current policy requirements
查看>>
MySQL8修改密码的方法
查看>>
Mysql8在Centos上安装后忘记root密码如何重新设置
查看>>
Mysql8在Windows上离线安装时忘记root密码
查看>>
MySQL8找不到my.ini配置文件以及报sql_mode=only_full_group_by解决方案
查看>>
mysql8的安装与卸载
查看>>
MySQL8,体验不一样的安装方式!
查看>>
MySQL: Host '127.0.0.1' is not allowed to connect to this MySQL server
查看>>
Mysql: 对换(替换)两条记录的同一个字段值
查看>>
mysql:Can‘t connect to local MySQL server through socket ‘/var/run/mysqld/mysqld.sock‘解决方法
查看>>
MYSQL:基础——3N范式的表结构设计
查看>>
MYSQL:基础——触发器
查看>>
Mysql:连接报错“closing inbound before receiving peer‘s close_notify”
查看>>