用 Hadoop 进行分布式并行编程, 第 3 部分
?
?
</property><property>
<name>mapred.job.tracker</name>
<value>homer06.austin.ibm.com:9001</value>
<description>The host and port that the MapReduce job tracker runs at. If
"local", then jobs are run in-process as a single map and reduce task.</description>
</property>
<name>dfs.name.dir</name>
<value>/home/caoyuz/hadoopfs/name</value>
<description>Determines where on the local filesystem the DFS name node
should store the name table. If this is a comma-delimited list of directories
then the name table is replicated in all of the directories,
for redundancy. </description>
</property>
<property>
<name>dfs.data.dir</name>
<value>/home/caoyuz/hadoopfs/data</value>
<description>Determines where on the local filesystem an DFS data node
should store its blocks. If this is a comma-delimited list of directories,
then data will be stored in all named directories, typically on different devices.
Directories that do not exist are ignored.</description>
</property>
<property>
<name>dfs.replication</name>
<value>2</value>
<description>Default block replication. The actual number of replications
can be specified when the file is created. The default is used if replication
is not specified in create time.</description>
</property>
</configuration>
参数 fs.default.name 指定 Name Node 的 IP 地址和端口号,此处我们将其设定为 homer06 及 9000 端口,参数 mapred.job.tracker 指定 JobTracker 的 IP 地址和端口号,此处我们将其设定为 homer06 及 9001 端口。 参数 dfs.name.dir 指定 Name Node 相关数据在本地文件系统上的存放位置, 此处我们将其设定为 /home/caoyuz/hadoopfs/name ,参数 dfs.data.dir 指定 Data Node 相关数据在本地文件系统上的存放位置,此处我们将其设定为 /home/caoyuz/hadoopfs/data 。注意, Hadoop 会自动创建这两个目录,无需事先创建。
更多的参数配置,可以参考 conf/hadoop-default.xml 文件,并在 conf/hadoop-site.xml 文件中设置。
StringTokenizer itr = new StringTokenizer(line);
while (itr.hasMoreTokens()) {
word.set(itr.nextToken());
output.collect(word, one);
}
}
}
public static class Reduce extends MapReduceBase
implements Reducer<Text, IntWritable, Text, IntWritable> {
public void reduce(Text key, Iterator<IntWritable> values,
OutputCollector<Text, IntWritable> output,
Reporter reporter) throws IOException {
int sum = 0;
while (values.hasNext()) {
sum += values.next().get();
}
output.collect(key, new IntWritable(sum));
}
}
public int run(String[] args) throws Exception {
Path tempDir = new Path("wordcount-temp-" +
Integer.toString(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);
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);
}
}
public static void main(String[] args) throws Exception {
String[] paths = {"input" , "output"};
int res = ToolRunner.run(new Configuration(), new WordCount(), paths);
System.exit(res);
}
}
然后在 “select hadoop server” 弹出框中选择我们已经定义好的 Hadoop server, 点击 Finish 之后,MapReduce Tool 会自动将 WordCount project打包成一个 jar 并拷到远程 Hadoop server 上运行起来, 整个运行过程的输出在 Eclipse 的 console 中即可看到,非常方便。
回页首
六 云计算与 Hadoop
我们知道,在分布式集群环境中才能发挥 Hadoop 的并行优势,拥有的机器数量越多,越能快速有效的处理海量数据。现实问题是,虽然很多公司都有处理海量数据的需求,却又不可能专门投资去搭建大规模的集群环境,Hadoop 于他们,不免沦为”屠龙之技”,无处发挥其优势,如之奈何?在过去,这个问题还真是难以解决,今天的情况就不一样了。读者如果关注 IT 业界动态,当知现在 IT 业界正在极力鼓吹”云计算”, 并有一些公司开始投资搭建所谓的”云计算平台”,这里的”云”, 就是一堆机器组成的分布式环境外加一些基础构架软件和管理软件,其中便会有类似于 Hadoop 这样的分布式计算软件,HDFS 这样的分布式文件系统,有需求的公司和个人可以到这样的”云计算平台”上去租用存储空间,租用计算结点(计算能力)做分布式运算。
比如 Amazon 公司基于 Hadoop 推出了 Amazon S3 ( Amazon Simple Storage Service ),提供可靠,快速,可扩展的网络存储服务,以及一个商用的云计算平台 Amazon EC2 ( Amazon Elastic Compute Cloud )。用户可以将其数据存储在 Amazon S3 分布式存储平台上, 然后到 Amazon EC2 上去租用计算能力,完成对数据的计算。Amazon EC2 提供所谓的按需租用服务,目前的收费标准是每台虚拟计算机 (Amazon EC2 称之为一个 instance) 每小时0.10美元。与传统的主机租用服务完全不同,用户可以根据自己某次运算处理的规模,租用相应数量的虚拟计算机,运算完毕后就可以释放你租用的虚拟计算机,Amazon 则会根据你租用的虚拟计算机的数量以及本次计算的实际运行时间向你收费,等于说你花钱租用计算能力,但不会浪费一个子儿。IBM 公司的云计算平台"蓝云"也面向企业用户提供了类似的功能。
如果我们打算基于 Hadoop 编写分布式并行程序来处理大量的数据,完全可以到 IBM, Amazon 等提供的云计算平台上去进行计算,对于 IBM 蓝云,Amazon S3, Amazon EC2 的详细介绍超出了本文范围,有兴趣的读者可以去其官方网站了解更多的信息。
回页首
七 结束语
这是系列文章的最后一篇。第一篇文章介绍了 MapReduce 计算模型,分布式文件系统 HDFS,分布式并行计算等的基本原理, 如何安装和部署单机 Hadoop 环境, 在第二篇文章中,我们实际编写了一个 Hadoop 并行计算程序,并了解了一些重要的编程细节,了解了如何使用 IBM MapReduce Tools 在 Eclipse 环境中编译,运行和调试 Hadoop 并行计算程序。本篇文章则详细介绍了如何部署分布式 Hadoop 环境,如何利用 IBM MapReduce Tools 将程序部署到分布式环境中运行,并简略介绍了现在流行的”云计算平台” 以及计算能力按需租用服务。
希望这三篇文章能起到一个抛砖引玉的作用,让你感受到 MapReduce 分布式并行编程的乐趣并从此入门且乐在其中,为即将到来的所谓”云计算”时代提前热热身。
声明:本文仅代表作者个人之观点,不代表 IBM 公司之观点。
参考资料
学习
访问Hadoop 官方网站,了解 Hadoop 及其子项目 HBase 的信息。Hadoop wiki上, 有许多 Hadoop 的用户文档,开发文档,示例程序等。
阅读 Google Mapduce 论文: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 MapReduce Tools:http://www.alphaworks.ibm.com/tech/mapreducetools,
阅读本系列文章的第一篇:用 Hadoop 进行分布式并行编程,第 1 部分:基本概念与安装部署
阅读本系列文章的第二篇:用 Hadoop 进行分布式并行编程,第 2 部分:程序实例与分析
讨论
Hadoop 邮件列表关于作者
曹羽中,在北京航空航天大学获得计算机软件与理论专业的硕士学位,具有数年的 unix 环境下的 C 语言,Java,数据库以及电信计费软件的开发经验,他的技术兴趣还包括 OSGi 和搜索技术。他目前在IBM中国系统与科技实验室从事系统管理软件的开发工作,可以通过caoyuz@cn.ibm.com与他联系。
?
?