1、默认情况下生成的文件名是part-r-00000格式,想要自定义生成输出文件名可以使用org.apache.hadoop.mapreduce.lib.output.MultipleOutputs类用来写出

2、MultipleOutputs类需要在Reduce的setup()方法初始化,最好在cleanup()中关闭

3、这个时候还会生产成part-r-000000这种文件,发现是里面是空的,需要 LazyOutputFormat.setOutputFormatClass(job, TextOutputFormat.class);

4、MultipleOutputs类的write()方法有几个重载的函数

write(KEYOUT key, VALUEOUT value, String baseOutputPath)
如果baseOutputPath带/,那么输出路径就是baseOutputPath + -r-00000,比如baseOutpuPath="/PUFA/" + key.toString(),输出文件路径就是/PUFA/0000000001-r-000000
不带/,输出路径就是FileOutputFormat.setOutputPath(job, outPutPath)下面,比如baseOutputPath=key.toString(),ouputPath=/trx,输出文件的路径就是/trx/0000000001-r-000000

write方法如果是带namedOutput参数的,需要在运行主类上面指定namedOutput,
MultipleOutputs.addNamedOutput(job, "PFBANK", TextOutputFormat.class, JournalTrxDataSet.class, NullWritable.class);
MultipleOutputs.addNamedOutput(job, "ZSBANK", TextOutputFormat.class, JournalTrxDataSet.class, NullWritable.class);
write(String namedOutput, K key, V value, String baseOutputPath) 
write(String namedOutput, K key, V value) 
这两种况和上面方法差不多,就是通过namedOutput对一组reduce处理的结果输出到不同的文件夹中,如果没有baseOutputPath,会输出到FileOutputFormat.setOutputPath()目录
  if (Integer.parseInt(key.toString()) >= 500000){
mos.write("PFBANK", journalTrxDataSet, NullWritable.get(), "/PUFA/" + key.toString());
}else if(Integer.parseInt(key.toString()) < 500000){
mos.write("ZSBANK", journalTrxDataSet, NullWritable.get(), "/ZHAOSHANG/" + key.toString());
}

注意:有的时候会出现_SUCCESS文件和reduce输出的文件不在同一个目录,这是因为FileOutputFormat.setOutputPaht()和MultipleOutputs类的write()方法设置的baseOutputPath不一样所致,_SUCCESS文件始终在FileOutputFormat.setOutputPaht()设定的路径上

有的时候会报一些莫名其妙的错的话,可能是LazyOutputFormat.setOutputFormatClass()和MultipleOutputs.addNamedOutput()的formatclass参数有关

最后附上完整代码

当时我们的需求是,需要分析统计银行各个终端的交易情况,当时我们数据量也不太多,领导说尽可能简单点做,当时统计纬度有两种,一种是按照机器,一种是按照分行,所以直接使用了同一个mapreduce来完成,当然,下面代码只是统计机器的,分行的维度还没写上。当时mr程序是独立的一个模块,在数据采集完成后,会直接使用sh或cmd命令调用这个jar包,文件名当成启动参数传递过来。文件需要自定义

package com.xhy.xgg.mapreduce;

import com.xhy.xgg.common.HDFSCommand;
import com.xhy.xgg.common.enums.StatisticDemensionEnums;
import com.xhy.xgg.entity.JournalTrxDataSet;
import lombok.extern.slf4j.Slf4j;
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.io.WritableComparable;
import org.apache.hadoop.io.WritableComparator;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.Mapper;
import org.apache.hadoop.mapreduce.Partitioner;
import org.apache.hadoop.mapreduce.Reducer;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
import org.apache.hadoop.mapreduce.lib.output.LazyOutputFormat;
import org.apache.hadoop.mapreduce.lib.output.MultipleOutputs;
import org.apache.hadoop.mapreduce.lib.output.TextOutputFormat;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.stereotype.Service; import javax.annotation.PostConstruct;
import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date; @Slf4j
@Service
public class MapReduceTest { @Value("${data-set.output-path}")
private String dataCollectionOutputPath;
@Value("${data-collection.output-path}")
private String dataCollectionIutputPath; @Value("${data-collection.hdfsURI}")
private String hdfsURI; @Value("${mr-run-mode.value}")
private String mrRunMode; @PostConstruct
public void executeMRJob(StatisticDemensionEnums statisticDemensionEnums)
throws IOException, ClassNotFoundException, InterruptedException { log.info(">> executeMRJob start execute");
Configuration conf = new Configuration();
conf.set("dfs.blocksize", "67108864");
conf.set("dfs.replication", "1");
conf.set("mapreduce.framework.name", mrRunMode);
conf.set("fs.defaultFS", hdfsURI);
Job job;
job = Job.getInstance(conf, "JournalDataProcessService");
job.setJarByClass(com.xhy.xgg.mapreduce.TerminalJournalDataService.class);
job.getConfiguration().set("statisticDemensionEnums", String.valueOf(statisticDemensionEnums.getIndex()));
job.setMapperClass(com.xhy.xgg.mapreduce.MapReduceTest.JournalDataMapper.class);
job.setMapOutputKeyClass(Text.class); job.setMapOutputValueClass(JournalTrxDataSet.class);
job.setReducerClass(com.xhy.xgg.mapreduce.MapReduceTest.JournalDataReducer.class);
job.setOutputKeyClass(JournalTrxDataSet.class);
job.setOutputValueClass(NullWritable.class);
job.setPartitionerClass(com.xhy.xgg.mapreduce.MapReduceTest.JournalDataPartitioner.class);
// job.setSortComparatorClass(JournalTrxDataComparator.class);
job.setNumReduceTasks(3);
Path inputPath = new Path(dataCollectionIutputPath + File.separator + "esbTrx" + File.separator
+ hdfsFileName); log.info("-- executeMRJob inputPath = {}", inputPath.toString()); FileInputFormat.addInputPath(job, inputPath); String outPutPathData = ""; outPutPathData = dataCollectionOutputPath + "_" + new SimpleDateFormat("yyyyMMdd").format(new Date());
try {
if (HDFSCommand.exists(conf, hdfsURI, outPutPathData)) {
HDFSCommand.delete(conf, hdfsURI, outPutPathData);
}
} catch (Exception e) {
// TODO Auto-generated catch block
log.error("<< executeMRJob exception, message {}", e.getMessage());
}
Path outPutPath = new Path(outPutPathData);
FileOutputFormat.setOutputPath(job, outPutPath);
MultipleOutputs.addNamedOutput(job, "PFBANK", TextOutputFormat.class, JournalTrxDataSet.class, NullWritable.class);
MultipleOutputs.addNamedOutput(job, "ZSBANK", TextOutputFormat.class, JournalTrxDataSet.class, NullWritable.class); LazyOutputFormat.setOutputFormatClass(job, TextOutputFormat.class); System.exit(job.waitForCompletion(true) ? 0 : 1);
} public void start() throws Exception {
executeMRJob(StatisticDemensionEnums.TERMINAL);
} private String hdfsFileName = "journal_test.txt"; private static class JournalDataReducer extends Reducer<Text, JournalTrxDataSet, JournalTrxDataSet, NullWritable> { private String statisticDemensionEnums = null;
JournalTrxDataSet journalTrxDataSet = new JournalTrxDataSet(); private MultipleOutputs mos; @Override
protected void setup(Context context) throws IOException, InterruptedException {
super.setup(context);
mos = new MultipleOutputs<>(context);
statisticDemensionEnums = context.getConfiguration().get("statisticDemensionEnums"); } @Override
protected void reduce(Text key, Iterable<JournalTrxDataSet> values,
Reducer<Text, JournalTrxDataSet, JournalTrxDataSet, NullWritable>.Context context)
throws IOException, InterruptedException { String branchId = "";
String branchName = "";
String trxType = "";
double trxAmt = 0.0;
int trxCount = 0; for (JournalTrxDataSet rw : values) {
branchId = rw.getBranchId();
branchName = rw.getBranchName();
trxType = rw.getTrxType();
trxAmt += rw.getAmount();
trxCount += rw.getCount(); }
journalTrxDataSet.Set(key.toString(), branchId, branchName, trxType, trxAmt, trxCount);
if (Integer.parseInt(key.toString()) >= 500000) {
mos.write("PFBANK", journalTrxDataSet, NullWritable.get(), "/PUFA/" + key.toString());
} else if (Integer.parseInt(key.toString()) < 500000) {
mos.write("ZSBANK", journalTrxDataSet, NullWritable.get(), "/ZHAOSHANG/" + key.toString());
}
//mos.write(journalTrxDataSet, NullWritable.get(), "/PUFA/"+ key.toString());
//mos.write(journalTrxDataSet, NullWritable.get(), key.toString());
} @Override
protected void cleanup(Context context) throws IOException, InterruptedException {
super.cleanup(context);
mos.close();
} } private static class JournalDataPartitioner extends Partitioner<Text, JournalTrxDataSet> {
@Override
public int getPartition(Text key, JournalTrxDataSet value, int arg2) { if ("706010101".equals(value.getBranchId())) {
return 0;
} else if ("706010106".equals(value.getBranchId())) {
return 1;
}
return 2;
} } private static class JournalDataMapper extends Mapper<Object, Text, Text, JournalTrxDataSet> { private String statisticDemensionEnums = null; JournalTrxDataSet journalTrxDataSet = new JournalTrxDataSet(); @Override
protected void setup(Context context) throws IOException, InterruptedException {
super.setup(context);
// 上面传递过来需要map reduce的纬度,一个按照终端来统计,一个按照分行来统计
statisticDemensionEnums = context.getConfiguration().get("statisticDemensionEnums"); } @Override
protected void map(Object key, Text value, Mapper<Object, Text, Text, JournalTrxDataSet>.Context context)
throws IOException, InterruptedException { String strContent = value.toString();
String result[] = strContent.split("\\|");
String terminalId = result[0]; // terminal id
String branchId = result[1]; // branch id
String branchName = result[2]; // branch id
double amount = Double.parseDouble(result[4]); // transaction amount
String trxType = result[5];
journalTrxDataSet.Set(terminalId, branchId, branchName, trxType, amount, 1);
context.write(new Text(terminalId), journalTrxDataSet);
} } private static class JournalTrxDataComparator extends WritableComparator { protected JournalTrxDataComparator() {
super(JournalTrxDataSet.class, true);
} @Override
public int compare(WritableComparable w1, WritableComparable w2) { JournalTrxDataSet j1 = (JournalTrxDataSet) w1;
JournalTrxDataSet j2 = (JournalTrxDataSet) w2;
int resultCompare = 0;
/*
* if (j1.getAmount() == j2.getAmount()) { resultCompare = 0; } else if
* (j1.getAmount() < j2.getAmount()) { resultCompare = -1; } else {
* resultCompare = 1; }
*/
return resultCompare;// return -1,0,1
} public static void main(String[] args) { SpringApplication.run(com.xhy.xgg.mapreduce.TerminalJournalDataService.class, args);
} }
}

MapReduce输出文件名更改的更多相关文章

  1. NSLog 输出文件名、方法名、行号

    项目中经常会需要根据日志输出来寻找源代码,通过以下方法可以让它自动输出文件名.方法.行号,非常方便. 找到项目的pch文件,添加以下内容即可: ...为三个英文句号(复制粘贴后可能会变化). /** ...

  2. 关于wxFileSystemWatcher输出文件名的解决方法

    本文针对的wxWidgets版本: 2.9.4, 2.9.5,其他版本未作测试. 如果要使用 wxFileSystemWatcher 并且让其产生的wxFileSystemWatcherEvent 事 ...

  3. log4cxx用环境变量设置输出文件名

    log4cxx用环境变量设置输出文件名(金庆的专栏 2016.12)利用环境变量,可以用同一个log4j.xml来配置多个相似进程,输出日志到不同文件.例如多个BaseApp进程使用同一个BaseAp ...

  4. 统计 MapReduce 输出路径修改。

    先在上一篇MR 的104 行加入代码.jobConf.setOutputFormat(MyMultipleFilesTextOutputFormat.class); 用意是自定义 job 的输出格式: ...

  5. 如何去掉MapReduce输出的默认分隔符

    我们在用MapReduce做数据处理的时候,经常会遇到将只需要输出键或者值的情况,如context.write(new Text(record), new Text("")),这样 ...

  6. java文件名更改一直是false,看看是否是文件打开没有关

    // 更改文件名称 public static void chenckFileName(String oldFile, String newFileName) { File file = new Fi ...

  7. hadoop拾遗(五)---- mapreduce 输出到多个文件 / 文件夹

    今天要把HBase中的部分数据转移到HDFS上,想根据时间戳来自动输出到以时间戳来命名的每个文件夹下.虽然以前也做过相似工作,但有些细节还是忘记了,所以这次写个随笔记录一下. package com. ...

  8. hadoop MapReduce —— 输出每个单词所对应的文件

    下面是四个文件及其内容. 代码实现: Mapper: package cn.tedu.invert; import java.io.IOException; import org.apache.had ...

  9. MapReduce:输出是一个文本文件,每一行第一个数字式行标,第二个数字是输入文件中每一行除行标外数字的平均值,且整数不保留小数,小数保留两位小数点

    有时候你会遇到这样的问题:你有一个表格,给出了每个人在十二月,一月和二月的收入. 表格如下: 姓名 一月 二月 三月 楚乔     200   314   3500 宇文玥     2000  332 ...

随机推荐

  1. c# throw和throw ex

    c# throw和throw ex 我们在日常开发当中,经常会用到exception异常,并且我们会在exception中的catch块中throw exception,例如: static void ...

  2. iOS开发基础-图片切换(1)

    一.程序功能分析 1)点击左右箭头切换图片.序号.描述: 2)如果是首张图片,左边箭头失效: 3)如果是最后一张图片,右边箭头失效. 二.程序实现 定义确定图片位置.大小的常量: //ViewCont ...

  3. 在SQL Server中如何进行UPDATE TOP .....ORDER BY?

    前言 今天在导入数据到系统后需要根据时间排序对刚导入的TOP N条进行数据更新,之前没遇到过UPDATE TOP...ORDER BY,以此作为备忘录. SQL SERVER之UPDATE TOP.. ...

  4. SpringCloud(7)服务链路追踪Spring Cloud Sleuth

    1.简介 Spring Cloud Sleuth 主要功能就是在分布式系统中提供追踪解决方案,并且兼容支持了 zipkin,你只需要在pom文件中引入相应的依赖即可.本文主要讲述服务追踪组件zipki ...

  5. 【学习总结】Git学习-参考廖雪峰老师教程-总

    公元2018-10-21 实验室台式机 win7 64位 参考教程: 廖雪峰Git教程 其他资料:Git-book 北大一只总结的笔记,最终整理的时候可以参考:Git笔记 评论区看到的另一个人,总结在 ...

  6. mysql varchar integer

    MySQL 中将 varchar 字段转换成数字进行排序 - MySQL - 大象笔记 https://www.sunzhongwei.com/order-by-varchar-field-which ...

  7. git命令行 整理(一位大神给我的私藏)

    Evernote Export Git 是一个很强大的分布式版本控制系统.它不但适用于管理大型开源软件的源代码,管理私人的文档和源代码也有很多优势. Git常用操作命令: 1) 远程仓库相关命令 检出 ...

  8. 利用Python中SocketServer 实现客户端与服务器间非阻塞通信

    利用SocketServer模块来实现网络客户端与服务器并发连接非阻塞通信 版权声明 本文转自:http://blog.csdn.net/cnmilan/article/details/9664823 ...

  9. Linux--虚拟环境

    一 . 虚拟环境的安装 如果我们在进行多个django项目的话,只用一个物理环境的话,那么会影响效率,这时候我们局可以应用虚拟环境了 virtualenv #指定清华源下载pip的包 pip3 ins ...

  10. Activiti6-数据库配置-dbconfig(学习笔记)

    常用数据连接池种类: 不一样的地方在于filters过滤器,设置了统计.和记录 avtiviti支持的数据库有: <?xml version="1.0" encoding=& ...