一,介绍

1,旧API中有 org.apache.hadoop.mapred.lib.MultipleOutputFormat和org.apache.hadoop.mapred.lib.MultipleOutputs

MultipleOutputFormat allowing to write the output data to different output files.

MultipleOutputs creates multiple OutputCollectors. Each OutputCollector can have its own OutputFormat and types for the key/value pair. Your MapReduce program will decide what to output to each OutputCollector.

2,新API中  org.apache.hadoop.mapreduce.lib.output.MultipleOutputs

整合了上面旧API两个的功能,没有了MultipleOutputFormat。

  The MultipleOutputs class simplifies writing output data to multiple outputs

  Case one: writing to additional outputs other than the job default output. Each additional output, or named output, may be configured with its own             OutputFormat, with its own key class and with its own value class.

  Case two: to write data to different files provided by user

下面这段话来自Hadoop:The.Definitive.Guide(3rd,Early.Release)P251

  “In the old MapReduce API there are two classes for producing multiple outputs: MultipleOutputFormat and MultipleOutputs. In a nutshell, MultipleOutputs is more fully featured, but MultipleOutputFormat has more control over the output directory structure and file naming. MultipleOutputs in the new API combines the best features of the two multiple output classes in the old API.”

二,应用

1,输出到多个文件或多个文件夹:

  驱动中不需要额外改变,只需要在MapClass或Reduce类中加入如下代码

  private MultipleOutputs<Text,IntWritable> mos;public void setup(Context context) throws IOException,InterruptedException {mos = new MultipleOutputs(context);}public void cleanup(Context context) throws IOException,InterruptedException {mos.close();}

然后就可以用 mos.write(Key key,Value value,String baseOutputPath)代替context.write(key, value);
  在MapClass或Reduce中使用,输出时也会有默认的文件part-m-00*或part-r-00*,不过这些文件是无内容的,大小为0. 而且只有part-m-00*会传给Reduce。

2,以多种格式输出:

public class TestwithMultipleOutputs extends Configured implements Tool {public static class MapClass extends Mapper<LongWritable,Text,Text,IntWritable> {private MultipleOutputs<Text,IntWritable> mos;protected void setup(Context context) throws IOException,InterruptedException {mos = new MultipleOutputs<Text,IntWritable>(context);}public void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException{String line = value.toString();String[] tokens = line.split("-");mos.write("MOSInt",new Text(tokens[0]), new IntWritable(Integer.parseInt(tokens[1])));  //(第一处)mos.write("MOSText", new Text(tokens[0]),tokens[2]);     //(第二处)mos.write("MOSText", new Text(tokens[0]),line,tokens[0]+"/");  //(第三处)同时也可写到指定的文件或文件夹中}protected void cleanup(Context context) throws IOException,InterruptedException {mos.close();}}public int run(String[] args) throws Exception {Configuration conf = getConf();Job job = new Job(conf,"word count with MultipleOutputs");job.setJarByClass(TestwithMultipleOutputs.class);Path in = new Path(args[0]);Path out = new Path(args[1]);FileInputFormat.setInputPaths(job, in);FileOutputFormat.setOutputPath(job, out);job.setMapperClass(MapClass.class);job.setNumReduceTasks(0);  MultipleOutputs.addNamedOutput(job,"MOSInt",TextOutputFormat.class,Text.class,IntWritable.class);MultipleOutputs.addNamedOutput(job,"MOSText",TextOutputFormat.class,Text.class,Text.class);System.exit(job.waitForCompletion(true)?0:1);return 0;}public static void main(String[] args) throws Exception {int res = ToolRunner.run(new Configuration(), new TestwithMultipleOutputs(), args);System.exit(res);}}

测试的数据:

abc-1232-hdf
abc-123-rtd
ioj-234-grjth
ntg-653-sdgfvd
kju-876-btyun
bhm-530-bhyt
hfter-45642-bhgf
bgrfg-8956-fmgh
jnhdf-8734-adfbgf
ntg-68763-nfhsdf
ntg-98634-dehuy
hfter-84567-drhuk

结果截图:(结果输出到/test/testMOSout)

遇到的一个问题:

  如果没有mos.close(), 程序运行中会出现异常:

  12/05/21 20:12:47 WARN hdfs.DFSClient: DataStreamer Exception:

  org.apache.hadoop.ipc.RemoteException:org.apache.hadoop.hdfs.server.namenode.LeaseExpiredException: No lease on
  /test/mosreduce/_temporary/_attempt_local_0001_r_000000_0/h-r-00000 File does not exist. [Lease. Holder: DFSClient_-352105532, pendingcreates: 5]

MultipleOutputs(三)相关推荐

  1. mapreduce多目录输出(MultipleOutputFormat和MultipleOutputs)

    一,介绍 1,旧API中有 org.apache.hadoop.mapred.lib.MultipleOutputFormat和org.apache.hadoop.mapred.lib.Multipl ...

  2. (转)MultipleOutputFormat和MultipleOutputs

    MultipleOutputFormat和MultipleOutputs http://www.cnblogs.com/liangzh/archive/2012/05/22/2512264.html ...

  3. 【hadoop】MultipleOutputFormat和MultipleOutputs

    一,介绍 1,旧API中有 org.apache.hadoop.mapred.lib.MultipleOutputFormat和org.apache.hadoop.mapred.lib.Multipl ...

  4. MultipleOutputFormat和MultipleOutputs

    文章出处:http://www.cnblogs.com/liangzh/archive/2012/05/22/2512264.html MultipleOutputFormat和MultipleOut ...

  5. Hadoop详解(三)——MapReduce原理和执行过程,远程Debug,Writable序列化接口,MapReduce程序编写

    MapReduce概述 MapReduce是一种分布式计算模型,由Google提出,主要用于搜索领域,解决海量数据的计算问题. MR由两个阶段组成:Map和Reduce,用户只需要实现map()和Re ...

  6. 伍六七带你学算法 进阶篇-三数之和

    三数之和 难度-中等 题目:给你一个包含 n 个整数的数组 nums,判断 nums 中是否存在三个元素 a,b,c ,使得 a + b + c = 0 ?请你找出所有满足条件且不重复的三元组. 注意 ...

  7. 远程控制virtual box虚拟机系统的三种方式

    远程控制virtual box虚拟机系统的三种方式[阅读时间:5分钟] 1.使用现有的远程控制软件 2.使用SSH连接 3.使用远程桌面控制虚拟机系统 对于把虚拟机安装在寝室笔记本的人来说,能够远程控 ...

  8. 2022-2028年中国三轴陀螺仪行业市场深度分析及投资前景分析报告

    [报告类型]产业研究 [出版时间]即时更新(交付时间约3个工作日) [发布机构]智研瞻产业研究院 [报告格式]PDF版 本报告介绍了三轴陀螺仪行业相关概述.中国三轴陀螺仪行业运行环境.分析了中国三轴陀 ...

  9. TCP三次握手和四次挥手的解释

    基础知识 在TCP层,有个FLAGS字段,这个字段有以下几个标识:SYN, FIN, ACK, PSH, RST, URG. 其中,对于我们日常的分析有用的就是前面的五个字段. 它们的含义是: SYN ...

最新文章

  1. java bean spring_Java+Spring+Bean+注入方式
  2. 计算机组网技术与配置 pdf,教案计算机组网技术.pdf
  3. ATen(A TENsor library for C++11)剖析(1)
  4. 重磅:.NET 6 发布首个预览版
  5. 是人是谁_其实,我们每个人心中都有一把尺子,谁好谁歹谁心里都明白……
  6. PCA对特征点描述子降维
  7. 群同态基本定理证明_有限群的线性表示 | 表示与群代数
  8. python除法保留两位小数_除法巧算(Ⅱ),任何整数除7~9,11的快速心算技巧,爸妈收藏...
  9. ios官方菜单项目重点剖析附项目源码
  10. mnist数据集下载及使用
  11. 删除亚马逊Kindle电子书的DRM,将AZW转为PDF格式文档
  12. 《计算机网络:自顶向下方法》阅读笔记
  13. 如何处理春节效应——若干券商研究团队的经验
  14. JavaScript 学习中
  15. MFC 控件大小随窗体改变而改变大小-OnSize
  16. 六面体单元的体积计算方法
  17. 海思 Hi3516 使用 gpac 库把 H265 和 AAC 封装成 MP4
  18. ESRGAN - Enhanced Super-Resolution Generative Adversarial Networks论文翻译——中英文对照
  19. 史上最简单的Elasticsearch教程:SpringBoot集成Elasticsearch 实时流量监测平台
  20. Python 商品价格区间设置与排序

热门文章

  1. what is VC维
  2. 数据库系统概论之数据模型
  3. AUC的计算方法及相关总结
  4. 在win2008安装GNS3出现的蓝屏问题
  5. 有什么好用的图片编辑软件?这个编辑软件很好用
  6. 链化未来共识协议详解(下)
  7. 记一次升级了编译器后VSCode中产生的问题
  8. 《蓝桥杯CT107D单片机竞赛板》:蜂鸣器模块
  9. 【Spire.PDF】Spire.PDF导出报告之一获取与破解
  10. STM32HAL库定时器中断关闭的方法