Hadoop MapReduce 修改输出文件名 MultipleOutputs

本文介绍了如何在Hadoop MapReduce中使用MultipleOutputs工具修改默认的输出文件名,将默认的'part-r-xxx'或'000178_0'更改为自定义格式,如'自定义名字-r-xxx'。主要步骤包括声明multipleOutputs、在setup方法中初始化,以及在reduce方法中调用write方法进行写入。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

需求:修改mapreduce的输出文件名称 为自己想要的名字

工具:MultipleOutputs

默认文件名:part-r-xxx 或者000178_0

修改后为: 自定义名字-r-xxx   后边的r-xxx还没有去掉

主要流程:

1、声明 multipleOutputs

2、在setup方法中初始化

3、在reduce方法中调用  public voidwrite(KEYOUT key, VALUEOUT value, String baseOutputPath)

4、在cleanup放中close
5、取消原来文件的输出, LazyOutputFormat.setOutputFormatClass(job, TextOutputFormat.class);
 
 

package com.writer;

import org.apache.hadoop.io.NullWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Reducer;
import org.apache.hadoop.mapreduce.lib.output.MultipleOutputs;

import java.io.IOException;

/**
 * @author anjinlong
 * @create 2017-07-06 10:42
 * @description description
 **/
public class HistoryReduce extends Reducer<Text, Text, NullWritable, Text> {

  private MultipleOutputs<NullWritable, Text> multipleOutputs;

  protected void setup(Context context) throws IOException, InterruptedException {
    multipleOutputs = new MultipleOutputs<NullWritable, Text>(context);
  }

  public void reduce(Text key, Iterable<Text> values, Context context)
          throws IOException, InterruptedException {

    String fileName = key.toString().substring(key.toString().length() - 2);

    for (Text val : values) {
      multipleOutputs.write(NullWritable.get(), val, fileName.toString());
    }
  }

  protected void cleanup(Context context) throws IOException, InterruptedException {
    multipleOutputs.close();
  }

}


//取消类似part-r-00000的空文件
    LazyOutputFormat.setOutputFormatClass(job, TextOutputFormat.class);




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值