//近期在研究hadoop。第一个想要要開始研究的必然是wordcount程序了。看了《hadoop应用开发实战解说》结合自己的理解,对wordcount的源代码进行分析。
<pre name="code" class="java">

package org.apache.hadoop.mapred;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.StringTokenizer; import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.conf.Configured;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapred.FileInputFormat;
import org.apache.hadoop.mapred.FileOutputFormat;
import org.apache.hadoop.mapred.JobClient;
import org.apache.hadoop.mapred.JobConf;
import org.apache.hadoop.mapred.MapReduceBase;
import org.apache.hadoop.mapred.Mapper;
import org.apache.hadoop.mapred.OutputCollector;
import org.apache.hadoop.mapred.Reducer;
import org.apache.hadoop.mapred.Reporter;
import org.apache.hadoop.util.Tool;
import org.apache.hadoop.util.ToolRunner; public class WordCount extends Configured implements Tool { /*
这个类实现mapper接口的map方法,输入的是文本总的每一行。 利用StringTokenizer将字符串拆分成单词。然后将输出结果(word, 1)写入到OutputCollector中去
OutputCollector有hadoop框架提供。负责收集mapper和reducer的输出数据,实现map函数和reduce函数时。仅仅须要将输出的<key,value>对向OutputCollector一丢就可以,其余的事情框架会自己处理。
*/
public static class MapClass extends MapReduceBase
implements Mapper<LongWritable, Text, Text, IntWritable> { private final static IntWritable one = new IntWritable(1);
private Text word = new Text();
/*类中的LongWritable, Text, IntWritable是hadoop中实现的用于封装Java数据类型的类,这些类都可以被串行化从而便于在分布式系统中进行数据交换。可以将它们等同的视为long,string,int的替代品
*/
public void map(LongWritable key, Text value,
OutputCollector<Text, IntWritable> output,
Reporter reporter) throws IOException {
String line = value.toString();
StringTokenizer itr = new StringTokenizer(line);
while (itr.hasMoreTokens()) {
word.set(itr.nextToken());
output.collect(word, one);//输出结果(word,1)
}
}
} /*
此类实现的是Reducer接口中的reduce方法。函数中的參数key.value是由mapper输出的中间结果。values是一个iterator(迭代器)
*/
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;
/*
遍历这个迭代器。就行得到有同样的key的全部的value值。
此处的key是一个单词。而value则是词频
*/
while (values.hasNext()) {
sum += values.next().get();
}
//遍历后得到这个单词出现的总次数。
output.collect(key, new IntWritable(sum));
}
} static int printUsage() {
System.out.println("wordcount [-m <maps>] [-r <reduces>] <input> <output>");//输入输入路径
ToolRunner.printGenericCommandUsage(System.out);
return -1;
} /*
Wordcount 中map/reduce项目的主要驱动程序,调用此方法提交的map / reduce任务。在hadoop中一次计算任务成为一个job。可以通过以一个JobConf对象设置怎样执行这个job。此处定义了输出的key 类型是text,而value的类型是IntWritable
*/
public int run(String[] args) throws Exception {
JobConf conf = new JobConf(getConf(), WordCount.class);
conf.setJobName("wordcount"); // key是text(words)
conf.setOutputKeyClass(Text.class);
// value是IntWritable (ints)
conf.setOutputValueClass(IntWritable.class); conf.setMapperClass(MapClass.class);
conf.setCombinerClass(Reduce.class);
conf.setReducerClass(Reduce.class); List<String> other_args = new ArrayList<String>();
for(int i=0; i < args.length; ++i) {
try {
if ("-m".equals(args[i])) {
conf.setNumMapTasks(Integer.parseInt(args[++i]));
} else if ("-r".equals(args[i])) {
conf.setNumReduceTasks(Integer.parseInt(args[++i]));
} else {
other_args.add(args[i]);
}
} catch (NumberFormatException except) {
System.out.println("ERROR: Integer expected instead of " + args[i]);
return printUsage();
} catch (ArrayIndexOutOfBoundsException except) {
System.out.println("ERROR: Required parameter missing from " +
args[i-1]);
return printUsage();
}
}
// Make sure there are exactly 2 parameters left.
if (other_args.size() != 2) {
System.out.println("ERROR: Wrong number of parameters: " +
other_args.size() + " instead of 2.");
return printUsage();
}
FileInputFormat.setInputPaths(conf, other_args.get(0));
FileOutputFormat.setOutputPath(conf, new Path(other_args.get(1))); JobClient.runJob(conf);
return 0;
} public static void main(String[] args) throws Exception {
/* ToolRunner的run方法開始,run方法有三个參数。 第一个是Configuration类的实例,第二个是wordcount的实例,args则是从控制台接收到的命令行数组
*/
int res = ToolRunner.run(new Configuration(), new WordCount(), args);
System.exit(res);
} }


最新文章

  1. Rafy 领域实体框架设计 - 重构 ORM 中的 Sql 生成
  2. Linux多线程系列-2-条件变量的使用(线程安全队列的实现)
  3. 【leetcode】Binary Tree Zigzag Level Order Traversal (middle)
  4. 我使用中的Linux命令和快捷键(For Ubuntu)
  5. 发起post请求
  6. eclipse创建android项目失败的问题 [ android support library ]
  7. 出现,视图必须派生自 WebViewPage 或 WebViewPage错误解决方法
  8. Golang在视频直播平台的高性能实践
  9. C++ Dialog Box Command IDs
  10. JavaScript原生对象总纲
  11. js history对象 手机物理返回键
  12. 【转】一些常用的Vi命令,可帮助脱离鼠标
  13. MySQL优化配置之query_cache_size
  14. Hbase各版本环境要求
  15. Unity3D用户手册
  16. postgresql逻辑结构--视图(五)
  17. Redis 的线程模型
  18. Struts2笔记1:--Struts2原理、优点、编程流程、6大配置文件以及核心配置文件struts.xml
  19. js弹出遮层
  20. 四、curator recipes之共享重入互斥锁

热门文章

  1. DOM系统学习-进阶
  2. Python 获取图片文件大小并转换为base64编码
  3. 转: Android微信智能心跳方案
  4. 【Qt编程】基于Qt的词典开发系列&amp;lt;八&amp;gt;--用户登录及API调用的实现
  5. PHP关闭notice级别报错信息
  6. DB2解锁
  7. CentOS6.5下docker的安装及遇到的问题和简单使用(已实践)
  8. C语言文件操作函数大全(超详细)
  9. Unlink of file &#39;xx&#39; failed. Should I try again? (y/n) 解决办法
  10. nginx缓存设置