Hadoop实战 关于WordCount的小问题记录

 


Hadoop初学者运行wordcount的步骤

https://www.cnblogs.com/xia520pi/archive/2012/05/16/2504205.html

按照这个链接流程进行操作到2.2运行例子部分时要注意:每个人的存储位置不尽相同

找examples例子时我们需要找到这个例子的位置:

首先需要找到你的hadoop文件夹,然后依照下面路径:

/hadoop/share/hadoop/mapreduce

会看到如下图找到:hadoop-mapreduce-examples-2.5.2.jar

Hadoop实战 关于WordCount的小问题记录

然后再接着操作就可以了。

关于wordcount源码的理解和分析有助于我们更好的理解MapReduce框架的运行模式

资料参考:https://www.cnblogs.com/numen-fan/p/6628379.html

 

WordCount源码解析:

                              WordCount.java


1.	package org.myorg;
2.	
3.	import java.io.IOException;
4.	import java.util.*;
5.	
6.	import org.apache.hadoop.fs.Path;
7.	import org.apache.hadoop.conf.*;
8.	import org.apache.hadoop.io.*;
9.	import org.apache.hadoop.mapred.*;
10.	import org.apache.hadoop.util.*;
11.	
12.	public class WordCount {
13.	
	   /*Mapper(14-26行)中的Map方法(18-25行)通过指定的TextInputFormat(49行conf.setInputFormat(TextInputFormat.class))
一次处理一行,然后通过StringTokenizer以空格为分隔符将一行且分为若干tokens,之后输出相应的键值对<<word>,1>*/	
14.	   public static class Map extends MapReduceBase implements Mapper<LongWritable, Text, Text, IntWritable> {
15.	     private final static IntWritable one = new IntWritable(1);
16.	     private Text word = new Text();
17.		
18.	     public void map(LongWritable key, Text value, OutputCollector<Text, IntWritable> output, Reporter reporter) throws IOException {
19.	       String line = value.toString();
20.	       StringTokenizer tokenizer = new StringTokenizer(line);
21.	       while (tokenizer.hasMoreTokens()) {
22.	         word.set(tokenizer.nextToken());
23.	         output.collect(word, one);
24.	       }
25.	     }
26.	   }
27.	

	   /*Ruducer(28-36行)中的Reduce方法(29-35行)仅是将每个kry出现的额次数求和*/
28.	   public static class Reduce extends MapReduceBase implements Reducer<Text, IntWritable, Text, IntWritable> {
29.	     public void reduce(Text key, Iterator<IntWritable> values, OutputCollector<Text, IntWritable> output, Reporter reporter) throws IOException {
30.	       int sum = 0;
31.	       while (values.hasNext()) {
32.	         sum += values.next().get();
33.	       }
34.	       output.collect(key, new IntWritable(sum));
35.	     }
36.	   }
37.	
38.	   public static void main(String[] args) throws Exception {
39.	     JobConf conf = new JobConf(WordCount.class);
40.	     conf.setJobName("wordcount");
41.	
42.	     conf.setOutputKeyClass(Text.class);
43.	     conf.setOutputValueClass(IntWritable.class);
44.	
45.	     conf.setMapperClass(Map.class);

	     /*指定Combiner对运行结果按照key值排序,然后把输出传递给本地的combiner*/
46.	     conf.setCombinerClass(Reduce.class);
47.	     conf.setReducerClass(Reduce.class);
48.	
	     /*指定TextInputFormat一次处理一行*/
49.	     conf.setInputFormat(TextInputFormat.class);
50.	     conf.setOutputFormat(TextOutputFormat.class);
51.	
52.	     FileInputFormat.setInputPaths(conf, new Path(args[0]));
53.	     FileOutputFormat.setOutputPath(conf, new Path(args[1]));
54.	
	     /*run方法指定了作业的几个方面,例如通过命令行传递过来的输入/输出路径,key/value的类型,
输入/输出的格式等等,JobConf中的配置信息。调用55行来提交作业并监控他的执行*/
55.	     JobClient.runJob(conf);
57.	   }
58.	}
59.	

关于组成一个指定作业的map数目的确定,以及如何以更精细的方式去控制这些map,我们将在教程的后续部分学习到更更多的内容