Mapreduce中自定义OutputFormat

Mapreduce中自定义OutputFormat

OutputFormat接口实现类

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

  1. 文本输出TextOutputFormat

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

  1. SequenceFileOutputFormat

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

  1. 自定义OutputFormat

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

自定义OutputFormat步骤:

(1) 自定义一个类继承FileOutputFormat
(2) 改写RecordWriter,具体改写输出数据的方法write()

自定义OutputFormat

  1. 需求分析
    过滤输入的数据,将包含atguigu的网站输出到一个文件,不包含atguigu的网站输出到另一个文件

  2. 输入数据
    在这里插入图片描述

  3. 编写Mapper类

package com.atguigu.mr.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 FileMapper extends Mapper<LongWritable, Text, Text, NullWritable> {
   
    @Override
    protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
   
        context.write(value,NullWritable.get());
    }
}
  1. 编写Reducer类
package com.atguigu.mr.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 FileReducer extends Reducer<Text, NullWritable,Text,NullWritable> {
   
    Text k = new Text()
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值