读书人

用 Hadoop 开展分布式并行编程, 第 2

发布时间: 2012-08-26 16:48:05 作者: rapoo

用 Hadoop 进行分布式并行编程, 第 2 部分

?

用 Hadoop 开展分布式并行编程, 第 2 部分用 Hadoop 开展分布式并行编程, 第 2 部分用 Hadoop 开展分布式并行编程, 第 2 部分用 Hadoop 开展分布式并行编程, 第 2 部分平均分 (共 6 个评分 )

?

StringTokenizer itr = new StringTokenizer(line);
while (itr.hasMoreTokens()) {
word.set(itr.nextToken());
output.collect(word, one);
}
}
}new Random().nextInt(Integer.MAX_VALUE))); //定义一个临时目录

JobConf conf = new JobConf(getConf(), WordCount.class);
try {
conf.setJobName("wordcount");

conf.setOutputKeyClass(Text.class);
conf.setOutputValueClass(IntWritable.class);

conf.setMapperClass(MapClass.class);
conf.setCombinerClass(Reduce.class);
conf.setReducerClass(Reduce.class);

conf.setInputPath(new Path(args[0]));
conf.setOutputPath(tempDir); //先将词频统计任务的输出结果写到临时目
//录中, 下一个排序任务以临时目录为输入目录。

conf.setOutputFormat(SequenceFileOutputFormat.class);

JobClient.runJob(conf);

JobConf sortJob = new JobConf(getConf(), WordCount.class);
sortJob.setJobName("sort");

sortJob.setInputPath(tempDir);
sortJob.setInputFormat(SequenceFileInputFormat.class);

sortJob.setMapperClass(InverseMapper.class);

sortJob.setNumReduceTasks(1); //将 Reducer 的个数限定为1, 最终输出的结果
           //文件就是一个。
sortJob.setOutputPath(new Path(args[1]));
sortJob.setOutputKeyClass(IntWritable.class);
sortJob.setOutputValueClass(Text.class);

sortJob.setOutputKeyComparatorClass(IntWritableDecreasingComparator.class);

JobClient.runJob(sortJob);
} finally {
FileSystem.get(conf).delete(tempDir); //删除临时目录
}
return 0;
}

private static class IntWritableDecreasingComparator extends IntWritable.Comparator {
public int compare(WritableComparable a, WritableComparable b) {
return -super.compare(a, b);
}

public int compare(byte[] b1, int s1, int l1, byte[] b2, int s2, int l2) {
return -super.compare(b1, s1, l1, b2, s2, l2);
}

点击 Eclipse 主菜单上 File->New->Project, 在弹出的对话框中选择 MapReduce Project, 输入 project name 如 wordcount, 然后点击 Finish 即可。,如图 2 所示:



用 Hadoop 开展分布式并行编程, 第 2 部分

此后,你就可以象一个普通的 Eclipse Java project 那样,添加入 Java 类,比如你可以定义一个 WordCount 类,然后将本文代码清单1,2,3中的代码写到此类中,添加入必要的 import 语句 ( Eclipse 快捷键 ctrl+shift+o 可以帮你),即可形成一个完整的 wordcount 程序。

在我们这个简单的 wordcount 程序中,我们把全部的内容都放在一个 WordCount 类中。实际上 IBM MapReduce tools 还提供了几个实用的向导 ( wizard ) 工具,帮你创建单独的 Mapper 类,Reducer 类,MapReduce Driver 类(就是代码清单3中那部分内容),在编写比较复杂的 MapReduce 程序时,将这些类独立出来是非常有必要的,也有利于在不同的计算任务中重用你编写的各种 Mapper 类和 Reducer 类。

如图三所示,设定程序的运行参数:输入目录和输出目录之后,你就可以在 Eclipse 中运行 wordcount 程序了,当然,你也可以设定断点,调试程序。



用 Hadoop 开展分布式并行编程, 第 2 部分

描述名字大小下载方法改进的 wordcount 程序wordcount.zip8KBHTTPIBM MapReduce Toolsmapreduce_plugin.zip324KBHTTP

关于下载方法的信息


学习

访问Hadoop 官方网站,了解 Hadoop 及其子项目 HBase 的信息。Hadoop wiki上, 有许多 Hadoop 的用户文档,开发文档,示例程序等。阅读 Google Mapreduce 论文:MapReduce: Simplified Data Processing on Large Clusters, 深入了解 Mapreduce 计算模型。学习 Hadoop 分布式文件系统 HDFS:The Hadoop Distributed File System:Architecture and Design学习 Google 文件系统 GFS:The Google File System, Hadoop HDFS 实现了与 GFS 类似的功能。到 IBM alphaWorks 网站了解并且下载 IBM MapReduce Tools:http://www.alphaworks.ibm.com/tech/mapreducetools,

讨论

加入Hadoop 开发者邮件列表,了解 Hadoop 项目开发的最新进展。

曹羽中,在北京航空航天大学获得计算机软件与理论专业的硕士学位,具有数年的 unix 环境下的 C 语言,Java,数据库以及电信计费软件的开发经验,他的技术兴趣还包括 OSGi 和搜索技术。他目前在IBM中国系统与科技实验室从事系统管理软件的开发工作,可以通过caoyuz@cn.ibm.com与他联系。

?

读书人网 >编程

热点推荐