一,介绍

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]

【hadoop】MultipleOutputFormat和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. MultipleOutputFormat和MultipleOutputs

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

  4. MultipleOutputs(三)

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

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

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

  6. Hadoop集群上使用JNI,调用资源文件

    hadoop是基于java的数据计算平台,引入第三方库,例如C语言实现的开发包将会大大增强数据分析的效率和能力. 通常在是用一些工具的时候都要用到一些配置文件.资源文件等.接下来,借一个例子来说明ha ...

  7. MapReduce入门

    说明 MapReduce是一种分布式计算模型,解决海量数据的计算问题,主要有Map和Reduce组成 用户使用时需要实现map()和reduce()两个函数,两个函数的形参都是key/value键值对 ...

  8. MapReduce中的自定义多目录/文件名输出HDFS

    转载自 http://my.oschina.net/leejun2005/blog/94706 最近考虑到这样一个需求: 需要把原始的日志文件用hadoop做清洗后,按业务线输出到不同的目录下去,以供 ...

  9. MapReduce中的自定义多目录/文件名输出转

    最近考虑到这样一个需求: 需要把原始的日志文件用hadoop做清洗后,按业务线输出到不同的目录下去,以供不同的部门业务线使用. 这个需求需要用到MultipleOutputFormat和Multipl ...

最新文章

  1. python的难点在哪里_自己写的Python答案,不知道错在哪儿希望能被告知问题在哪儿和答案...
  2. Openstack组件部署 — Nova overview
  3. 什么情况下可以不写PHP的结束标签“?”
  4. 怎么找出电脑里隐藏的流氓软件_9成人都不知道的秘密!那些隐藏在你电脑里的“大象”!...
  5. 转:在eclipse中搭建maven工程(第二种方法)
  6. html 文本框 初始化,Flutter 文本框初始化时显示默认值
  7. java 读取数据库输出_java 读取数据库数据转化输出XML输出在jsp页面
  8. Ruby On Rails 安装手记
  9. cocoachina上很酷的帖子
  10. Linux下编译redis及配置
  11. 利用utl_smtp从oracle数据库发送带blob附件的电子邮件
  12. Unity3D学习 ④ Unity导入商店资源,实现基本的奔跑、攻击动作切换与交互
  13. 入门 HTML JavaScript Jquery学习回顾 有小案例
  14. 【整理】轴体结构和润轴
  15. 参加珠海苹果售后维修体验
  16. t.zijieimg.com/v.douyin.com短网址在线缩短工具
  17. Verilog功能模块——时钟分频
  18. 笔记③:牛客校招冲刺集训营---C++工程师(5.9 C++新特性)
  19. S5PV210-uboot解析(三)-start_armboot解析
  20. 联想笔记本重装win11系统后恢复fn+q热键

热门文章

  1. c语言线性表链路存储结构运用,哈尔滨工业大学2020年考研854计算机基础考试大纲...
  2. 我入门 Python 后总结的基础教程
  3. 超级简单的视频调色教程分享
  4. element输入框限制数字输入
  5. X Error of failed request: BadWindow (invalid Window parameter)
  6. Spring Data JPA-单向一对一关联映射
  7. 程序员升职记 全关卡攻略通俗思路 Human Resource Machine
  8. 【JZOJ】【匈牙利算法】【二分】 导弹
  9. 本人地推行业摸爬滚打5年
  10. 嵌入式软件工程师就业方向有哪些呢?