这段时间需要学习Hadoop了,以前一直听说Hadoop,但是从来没有研究过,这几天粗略看完了《Hadoop实战》这本书,对Hadoop编程有了大致的了解。接下来就是多看多写了。以Hadoop自带的例子WordCount程序开始,来记录我的Hadoop学习过程。

Hadoop自带例子WordCount.java

/**
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/ package org.apache.hadoop.examples; import java.io.IOException;
import java.util.StringTokenizer; import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IntWritable;
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.output.FileOutputFormat;
import org.apache.hadoop.util.GenericOptionsParser; public class WordCount { public static class TokenizerMapper
extends Mapper<Object, Text, Text, IntWritable>{ private final static IntWritable one = new IntWritable(1);
private Text word = new Text(); public void map(Object key, Text value, Context context
) throws IOException, InterruptedException {
StringTokenizer itr = new StringTokenizer(value.toString());
while (itr.hasMoreTokens()) {
word.set(itr.nextToken());
context.write(word, one);
}
}
} public static class IntSumReducer
extends Reducer<Text,IntWritable,Text,IntWritable> {
private IntWritable result = new IntWritable(); public void reduce(Text key, Iterable<IntWritable> values,
Context context
) throws IOException, InterruptedException {
int sum = 0;
for (IntWritable val : values) {
sum += val.get();
}
result.set(sum);
context.write(key, result);
}
} public static void main(String[] args) throws Exception {
Configuration conf = new Configuration();
String[] otherArgs = new GenericOptionsParser(conf, args).getRemainingArgs();
if (otherArgs.length != 2) {
System.err.println("Usage: wordcount <in> <out>");
System.exit(2);
}
Job job = new Job(conf, "word count");
job.setJarByClass(WordCount.class);
job.setMapperClass(TokenizerMapper.class);
job.setReducerClass(IntSumReducer.class);
job.setOutputKeyClass(Text.class);
job.setOutputValueClass(IntWritable.class);
FileInputFormat.addInputPath(job, new Path(otherArgs[0]));
FileOutputFormat.setOutputPath(job, new Path(otherArgs[1]));
System.exit(job.waitForCompletion(true) ? 0 : 1);
}
}
这个程序的功能是对文件中各个单词的数目进行统计。

在Wordount.java中有两个静态内部类TokenizerMapper,IntSumReducer,关于静态内部类,可以参考另一篇文章
Java中的静态内部类。这两个类分别对应与MapReduce中的map和reduce。至于为什么要用静态的内部类,个人理解是这样的:一般一个简单作业(Job)包含了一个map过程和一个reduce过程,Job,Map,Reduce写在一个文件中便于文件的组织。但是,Hadoop内部需要使用反射的方式来实例化客户端的Map和Reduce,所以使用了静态内部类的方式,参考了StackOverflow上的一个帖子:
Do Mappers and Reducers in Hadoop have to be static classes?,如果不许要将Job,Map和Reduce组织在一起,完全可以将这三个类写在三个类文件中。


在程序的main函数中首先实例化一个Configuration,用于加载Hadoop的配置信息,然后就解析给程序传递的参数,这里我们传递了两个字符串参数,经过解析之后保存在有两个元素的数组otherArgs中,其中otherArgs[0]为要进行统计的文件的路径,otherArgs[1]为经过MapReduce计算之后的结果所保存的位置。

Job job = new Job(conf, "word count");

语句实例化一个Job对象,然后就为Job对像指定运行时所需的类

job.setJarByClass(WordCount.class);

表示告诉Hadoop集群,作业从哪个类开始运行,

job.setMapperClass(TokenizerMapper.class);

表示执行哪个类的map方法,我们这里指定的是方法

 public void map(Object key, Text value, Context context
) throws IOException, InterruptedException {
StringTokenizer itr = new StringTokenizer(value.toString());
while (itr.hasMoreTokens()) {
word.set(itr.nextToken());
context.write(word, one);
}
}

这个方法对要进行map的每行数据,使用StringTokenizer类进行分割,分割出来的值在保存到context中进行,从而在reduce中进行单词数量统计。

 job.setReducerClass(IntSumReducer.class);

这行语句设置用于进行Reduce的类,告诉Hadoop集群执行哪个reduce函数:

 public void reduce(Text key, Iterable<IntWritable> values,
Context context
) throws IOException, InterruptedException {
int sum = 0;
for (IntWritable val : values) {
sum += val.get();
}
result.set(sum);
context.write(key, result);
}

在这个函数执行之前,Hadoop已经为我们将各个单词的个数大概的归并在一起了,函数的前两个参数是Text 类型和Iterable类型,参数名分别为key和alues,其中在这里key表示在map方法中分割得到的单词,values表示在map阶段统计的单词的数量(由于reduce阶段接收到多个数据结点发送过来的统计结果,所以对应于一个key,可能有多个value,所以将这些value都保存在一迭代器中,然后对迭代器进行遍历,这个过程以后再讨论。),遍历values迭代器,对每个key的数量进行汇总,然后再记录在context中。

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

表示MapReduce执行结束之后,将结果保存在HDFS中时,保存的数据类型。这里将结果的key以Text类型保存,value以IntWritable类型保存。

 FileInputFormat.addInputPath(job, new Path(otherArgs[0]));
FileOutputFormat.setOutputPath(job, new Path(otherArgs[1]));

分别表示输入和输出的路径。

这个程序相对于Hadoop的例子,我去掉了
job.setCombinerClass(IntSumReducer.class);

这行语句,在Hadoop中,Combiner主要用于提升Hadoop的处理效率,为了集中于理解MapReduce,我去掉了这行代码,待以后讨论提升Hadoop性能时,再学习Combiner。







最新文章

  1. 深入理解JSON对象
  2. sql 函数 汉字转拼音
  3. chrome浏览器调用 ajax 提示net::ERR_INCOMPLETE_CHUNKED_ENCODING问题解决方案,以及 Response.Close 和 Response.End 的一些问题。
  4. 如何让aspnet服务加载静态资源html(我的动态网页静态化) 转
  5. 类型“System.Windows.Markup.IUriContext”在未被引用的程序集中定义 解决办法
  6. 隐语义模型LFM(latent factor model)
  7. java里的static和final
  8. 在Mac上用自己编译出的DNX运行.NET程序
  9. asp.net后台编写 loading效果
  10. JAVA语法细节(1)
  11. 对xml操作
  12. IOS--UISwitch的使用方法
  13. 无废话WCF入门教程四[WCF的配置文件]
  14. javascript动画效果之多物体透明度
  15. 分布式唯一id:snowflake算法思考
  16. [HAOI2008]下落的圆盘
  17. Appium移动自动化测试(五)--app控件获取之uiautomatorviewer
  18. C#两个实体之间相同属性的映射
  19. 020_nginx禁止ip默认参数是$remote_addr无法禁止真实ip的问题
  20. 了解java虚拟机—串行回收器(6)

热门文章

  1. php object转数组示例
  2. awk 例子
  3. JQuery:通过noConflict()方法同时使用jQuery 和其他框架
  4. iOS:app直播---采集篇
  5. jQuery的dom操作(二)转
  6. WPF控件模板
  7. 2、JavaScript常用互动方法
  8. Oracle修改时间报:ORA-01830: 日期格式图片在转换整个输入字符串之前结束的解决办法
  9. Java基础之集合框架——使用堆栈Stack&lt;&gt;对象模拟发牌(TryDeal)
  10. linux:计算机概论