- 浏览: 246106 次
-
文章分类
最新评论
引自http://www.cnblogs.com/liangzh/archive/2012/05/22/2512264.html
一,介绍
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]
发表评论
-
大数据方面的文章
2013-07-29 17:01 869http://bbs.e-works.net.cn/forum ... -
Apache Pig中文教程(进阶)
2013-05-13 17:18 1768引自http://www.codelast.com/?p=42 ... -
hadoop视频
2013-05-10 09:35 805http://pan.baidu.com/share/li ... -
Apache Pig的一些基础概念及用法总结(1
2013-05-08 16:01 1108引自http://www.codelast.com/?p=3 ... -
hadoop动态增加删除节点
2013-04-28 09:54 1192在master的conf/hdfs-site.xml中加入 ... -
hadoop 比较好的博客
2013-04-27 17:35 735http://dongxicheng.org 好的书 h ... -
Hadoop错误一的解决猜测
2013-04-26 10:29 845如果出现:java.lang.NullPointerExc ... -
Reduce作业运行时错误:Too many fetch-failures
2013-04-24 21:19 5796root@ubuntu:/usr/local/hadoop# ... -
hadoop各种输入方法(InputFormat)汇总
2013-01-04 17:02 1425引自http://www.blogjava.net/shenh ... -
Hadoop运行报错: java.lang.ClassNotFoundException解决方法
2012-12-27 16:44 12813在创建自定义的Mapper时候,编译正确,但上传到集群执 ... -
hadoop-1.1.0 rpm + centos 6.3 64 + JDK7 搭建全分布式集群的方法
2012-12-22 20:45 1258引自 http://blog.csdn.net/ireland ... -
HADOOP中DATANODE无法启动
2012-12-22 20:43 963摘要:该文档解决了多次格式化文件系统后,datanode ... -
Hadoop HDFS 编程
2012-12-18 17:38 879引自http://blog.csdn.net/lmc ... -
HDFS之SequenceFile和MapFile
2012-12-17 11:37 957引自http://blog.csdn.net/javam ... -
Hadoop -【IO专题-序列化机制】
2012-12-17 10:32 1096引自http://blog.sina.com.cn/s/ ... -
hadoop问题Type mismatch in value from map解决方法
2012-12-13 10:49 876hadoop问题Type mismatch in ... -
hadoop hbase svn site
2012-12-13 10:49 1002hadoop hbase svn site ... -
hadoop项目svn地址
2012-12-11 18:11 1061http://svn.apache.org/repos/asf ... -
在Eclipse中导入hadoop
2012-12-11 18:03 12430. 准备 (1) 需要有gcc、autoconf、 ... -
Hadoop实例WordCount程序一步一步运行
2012-12-11 16:32 1012虽说现在用Eclipse下开发Hadoop程序很方便了,但是命 ...
相关推荐
Hadoop MapReduce 中使用 MultipleOutputFormat 实现多文件输出 Hadoop 默认的输出格式是 TextOutputFormat,输出文件名不可定制。从 Hadoop 0.19.X 开始,Hadoop 提供了一个 org.apache.hadoop.mapred.lib....
通过自定义`MultipleOutputFormat`和`RecordWriter`,我们可以根据实际需求构建适应未来Hadoop版本的解决方案。这对于大数据和云计算的学习者来说,是一个宝贵的实践经验,能够深入理解MapReduce的工作机制和如何...
Hadoop MapReduce多输出的功能主要由MultipleOutputFormat类及其相关类实现,使得开发者可以在Map和Reduce阶段分别控制输出的数据集,将数据分散存储到不同的输出文件中。 在默认情况下,Hadoop MapReduce的输出...
但是,从 Hadoop 0.20.x 版本开始,MultipleOutputFormat 所在包的所有类被标记为“已过时”,这意味着如果继续使用 MultipleOutputFormat,在将来版本的 Hadoop 中可能无法使用。 因此,我们需要自己实现一个简单...
顺丰单号发给还是关键是感慨时光飞逝看看
基于.net+SQLserver校园通,适合学生毕设参考,项目可完美运行
PkgModelProposal_rev
环中国自驾公路路线是许多自驾游爱好者的终极梦想,这条路线包括三条主要的国道:G219、G331和G228。 自驾路线的SHP和KML格式文件 环中国自驾路线经过的省份有新疆、西藏、云南、广西、辽宁、吉林、黑龙江、内蒙古、河北、天津、山东、江苏、上海、浙江、福建、广东。
zambell_110216_3cd_adhoc-v2
2024免费微信小程序毕业设计成品,包括源码+数据库+往届论文资料,附带启动教程和安装包。 启动教程:https://www.bilibili.com/video/BV1BfB2YYEnS 讲解视频:https://www.bilibili.com/video/BV1BVKMeZEYr 技术栈:Uniapp+Vue.js+SpringBoot+MySQL。 开发工具:Idea+VSCode+微信开发者工具。
基于Comsol模拟的环盘结构近场耦合效应的增强研究,基于COMSOL的环盘结构近场耦合增强技术研究,comsol环盘近场耦合增强。 ,comsol;环盘;近场耦合;增强,Comsol环盘结构近场耦合效应显著增强
1、文件内容:ws-jaxme-manual-0.5.2-10.el7.rpm以及相关依赖 2、文件形式:tar.gz压缩包 3、安装指令: #Step1、解压 tar -zxvf /mnt/data/output/ws-jaxme-manual-0.5.2-10.el7.tar.gz #Step2、进入解压后的目录,执行安装 sudo rpm -ivh *.rpm 4、更多资源/技术支持:公众号禅静编程坊
资源内项目源码是均来自个人的课程设计、毕业设计或者具体项目,代码都测试ok,都是运行成功后才上传资源,答辩评审绝对信服的,拿来就能用。放心下载使用!源码、说明、论文、数据集一站式服务,拿来就能用的绝对好资源!!! 项目备注 1、该资源内项目代码都经过测试运行成功,功能ok的情况下才上传的,请放心下载使用! 2、本项目适合计算机相关专业(如计科、人工智能、通信工程、自动化、电子信息等)的在校学生、老师或者企业员工下载学习,也适合小白学习进阶,当然也可作为毕设项目、课程设计、大作业、项目初期立项演示等。 3、如果基础还行,也可在此代码基础上进行修改,以实现其他功能,也可用于毕设、课设、作业等。 下载后请首先打开README.md文件(如有),仅供学习参考, 切勿用于商业用途。 4、如有侵权请私信博主,感谢支持
基于Django的个性化餐饮管理系统_1ml1r29h.zip
exceptionLogs.zip
普通人如何抓住DeepSeek红利
开关磁阻电机SRM的PID参数优化研究:基于粒子群算法的联合仿真探索,开关磁阻电机SRM的PID参数优化研究:基于粒子群算法的Matlab与Simulink联合仿真方法,开关磁阻电机SRM的PID参数优化 PID控制开关磁阻电机,粒子群算法优化PID参数(模型里面是matlab和simulink联合仿真) ,开关磁阻电机SRM; PID参数优化; 粒子群算法; MATLAB; Simulink联合仿真,基于粒子群算法的SRM电机PID参数优化
deepseek最新资讯、配置方法、使用技巧,持续更新中
2024免费微信小程序毕业设计成品,包括源码+数据库+往届论文资料,附带启动教程和安装包。 启动教程:https://www.bilibili.com/video/BV1BfB2YYEnS 讲解视频:https://www.bilibili.com/video/BV1BVKMeZEYr 技术栈:Uniapp+Vue.js+SpringBoot+MySQL。 开发工具:Idea+VSCode+微信开发者工具。
金属壁镜面反射BIC特性解析:频率实虚部、Q因子与反射谱计算研究,金属壁镜面反射BIC的频域特性分析与反射谱计算:涵盖实部虚部与Q因子探讨,金属壁镜面反射BIC,包含频率实部虚部,Q因子,反射谱计算 ,金属壁; 镜面反射; BIC; 频率实部虚部; Q因子; 反射谱计算;,金属壁镜面反射BIC:实虚频率分析,Q因子与反射谱计算技术