项目github地址:bitcarmanlee easy-algorithm-interview-and-practice
欢迎大家star,留言,一起学习进步

1.MultiPleOutputs简介

MapReduce job中,可以使用FileInputFormat和FileOutputFormat来对输入路径和输出路径来进行设置。在输出的时候,MR内部会对输出的文件进行重新命名,例如常见的形式为part-r-00000。
但是很多情况下,我们希望将输出的文件分开,即所谓的多路输出。我们希望将输出的内容重新组织,输出到不同的目录或者文件夹中,方便我们后续进一步的处理。否则,我们还需要在job完成以后,再来编写相应的脚本进行相关的文本处理,这样就耗时耗力非常不方便。
想要在MR中实现多路输出,MultiPleOutputs就能很好地帮助我们完成这个目标。

2.能正常运行的代码

首先准备测试数据。

1512,iphone5s,4inchs,A7,64,M7,lowerpower
1512,iphone5,4inchs,A6,IOS7
1512,iphone4s,3.5inchs,A5
50019780,Ipad,9.7,retina
50019780,yoga,lenovel,18hours
50019780,nexus,7,google
50019780,Ipad mini2,retina,7.9
1101,macbook air,OS X mavericks
1101,macbook pro,OS X lion
1101,thinkpad yoga,lenovel,windows 8

我们想将相同id的数据输入到同一个文件中,将不同id的数据分开。
二话不说,直接上源码

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.conf.Configured;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.NullWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.Mapper;
import org.apache.hadoop.mapreduce.Reducer;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.input.TextInputFormat;
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.apache.hadoop.util.Tool;
import org.apache.hadoop.util.ToolRunner;import java.io.IOException;/*** @author WangLei* @since 17-2-4*/
public class MultiOutput extends Configured implements Tool{public static class MultiMapper extends Mapper<LongWritable,Text,Text,Text> {@Overrideprotected void map(LongWritable key,Text value,Context context) throws IOException,InterruptedException {String line = value.toString().trim();if(line.length() > 0) {String[] lines = line.split(",");context.write(new Text(lines[0]),value);}}}public static class MultiReducer extends Reducer<Text,Text,NullWritable,Text> {//设置多文件输出private MultipleOutputs<NullWritable,Text> mos;@Overrideprotected void setup(Context context) throws IOException,InterruptedException {mos = new MultipleOutputs<NullWritable,Text>(context);}@Overrideprotected void cleanup(Context context) throws IOException,InterruptedException {//注意要调用close方法,否则会没有输出mos.close();}@Overrideprotected void reduce(Text key,Iterable<Text> values,Context context) throws IOException,InterruptedException {for(Text value:values) {//指定写出不同文件的数据mos.write("BaseOnKey",NullWritable.get(),value,key.toString() + "/" + key.toString());//指定写出全部数据mos.write("All",NullWritable.get(),value);}}}public int run(String[] args) throws Exception{Configuration conf = new Configuration();Job job = new Job(conf);job.setJarByClass(MultiOutput.class);job.setMapperClass(MultiMapper.class);job.setMapOutputKeyClass(Text.class);job.setMapOutputValueClass(Text.class);job.setReducerClass(MultiReducer.class);job.setOutputKeyClass(NullWritable.class);job.setOutputValueClass(Text.class);//BaseOnKey的输出MultipleOutputs.addNamedOutput(job,"BaseOnKey",TextOutputFormat.class,NullWritable.class,Text.class);//没有区分的所有输出MultipleOutputs.addNamedOutput(job,"All",TextOutputFormat.class,NullWritable.class,Text.class);job.setInputFormatClass(TextInputFormat.class);//取消part-r-00000新式文件输出LazyOutputFormat.setOutputFormatClass(job,TextOutputFormat.class);FileInputFormat.addInputPath(job,new Path(args[0]));Path outputPath = new Path(args[1]);FileSystem fs = FileSystem.get(job.getConfiguration());if(fs.exists(outputPath)) fs.delete(outputPath,true);FileOutputFormat.setOutputPath(job,outputPath);return job.waitForCompletion(true) ? 0 : 1;}public static void main(String[] args) throws Exception{System.exit(ToolRunner.run(new MultiOutput(),args));}
}

3.几个注意事项

应该注意的地方其实在代码的注释中已经写得相当详细了。这里再拎出来啰嗦一下。
1.例子中reduce阶段输出是,先设置多文件输出mos。
2.在cleanup方法中,需要调用mos的close方法,否则最后的输出为空。
3.LazyOutputFormat.setOutputFormatClass(job,TextOutputFormat.class); 这一行是为了取消part-r-00000形式的文件输出。否则最终的输出目录中还会有空的part-r-00000输出。

mapreduce多路输出实例相关推荐

  1. MapReduce工作笔记——Streaming多路输出

    文章目录 多路输出 实现 MapReduce工作笔记 系列目录:MapReduce工作笔记--目录 多路输出 加入如下命令: -outputformat org.apache.hadoop.mapre ...

  2. MapReduce Java API实例-排序

    场景 MapReduce Java API实例-统计单词出现频率: https://blog.csdn.net/BADAO_LIUMANG_QIZHI/article/details/11941016 ...

  3. MapReduce Java API实例-统计平均成绩

    场景 MapReduce Java API实例-统计单词出现频率: https://blog.csdn.net/BADAO_LIUMANG_QIZHI/article/details/11941016 ...

  4. MapReduce Java API实例-统计出现过的单词

    场景 MapReduce Java API实例-统计单词出现频率: https://blog.csdn.net/BADAO_LIUMANG_QIZHI/article/details/11941016 ...

  5. Hadoop Streaming 实战: 多路输出

    streaming把reduce的输出作为一个任务的最终输出,输出文件形如:       part-00000.part-00001--       文件个数为reduce任务个数 但是,有的时候,我 ...

  6. keras提取模型中的某一层_keras获得某一层或者某层权重的输出实例

    一个例子: print("Loading vgg19 weights...") vgg_model = VGG19(include_top=False, weights='imag ...

  7. PHP 读取数据库内容并以二维数组按指定列输出实例

    最新PHP 读取数据库内容并以二维数组按指定列输出实例 以下是三零网为大家整理的最新PHP 读取数据库内容并以二维数组按指定列输出实例的文章,希望大家能够喜欢! <?php $host = &q ...

  8. ITK:多路输出不同的类型

    ITK:多路输出不同的类型 内容提要 C++实现代码 内容提要 编写一个具有多个不同类型输出的过滤器. C++实现代码 #include "itkImage.h" #include ...

  9. ITK:多路输出相同类型的

    ITK:多路输出相同类型的 内容提要 C++实现代码 内容提要 编写一个具有多个相同类型输出的过滤器. C++实现代码 #include "itkImage.h" #include ...

  10. c语言将一个已知头结点的单链表逆序_C语言实现单链表逆序与逆序输出实例

    单链表的逆序输出分为两种情况,一种是只逆序输出,实际上不逆序:另一种是把链表逆序.本文就分别实例讲述一下两种方法.具体如下: 1.逆序输出 实例代码如下: #include #include #inc ...

最新文章

  1. 一次挂死(hang)的处理过程及经验
  2. 最大权值闭合子图的证明详解
  3. 统计学出身但编程一般,该选择数据分析还是挖掘算法?
  4. linux替换每个英文字开头为大写,shell脚本,文件里面的英文大小写替换方法。...
  5. WCF热带鱼书学习手记 - Service Contract Overload
  6. [剑指offer]面试题47:不用加减乘除做加法
  7. Linux scp 指令
  8. 远控免杀专题11-Avoidz免杀
  9. 选择适合你的开源 OLAP 引擎
  10. 详解Python的max、min和sum函数用法
  11. Happy Programming Contest
  12. 程序员的自我修养 - 符号修饰 函数签名 以及一个引申的问题: extern c
  13. 8-思科防火墙:Cisco ASA uRPF运用
  14. python制作GIF图
  15. 联想电脑管家图文介绍:联想电脑管家怎么下载?
  16. 重庆邮电大学计算机2019湖北分数线,2019重庆邮电大学录取分数线
  17. 女性向游戏难复制下一个“恋与”,从日本游戏能取到什么经?
  18. App渠道打包的最佳攻略,一次解决打包难题
  19. 肯德基app电脑端自动下单程序
  20. sql显示服务器连接不上,sql服务器连接不上

热门文章

  1. 如何将倾城时光录制成MP3格式
  2. mysql 在update中实现子查询的方式
  3. 【码云周刊第 8 期】面试之前,或许该高效率地学点干货了!
  4. 简单介绍,基于ldirectord的高可用lvs-dr调度器
  5. 想成为优秀的技术人员你必须做到的几件事情【转载】
  6. [声明]honkql大量密码被海空神佛团伙盗走
  7. C语言数据结构之图的邻接矩阵的应用实例
  8. Android中如何使用Intent在Activity之间传递对象[使用Serializable或者Parcelable]
  9. Eclipse Package Explorer视图无法打开
  10. 侦听键盘,将data写入文件data.out(成功版本)