Hadoop筆記之 WordCount

WordCount 是 Hadoop 應用最經典的例子。java

筆者使用 hadoop-2.6.0 版本,須要引入的包目錄位於 hadoop-2.6.0/share/hadoop/common/libapache

源碼

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.mapreduce.lib.input.TextInputFormat;
import org.apache.hadoop.mapreduce.lib.output.TextOutputFormat;


public class WordCount {
	
	public static class WordCountMap 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 {
			String line = value.toString();
			StringTokenizer tokenizer = new StringTokenizer(line);
		
			while (tokenizer.hasMoreTokens()){
				word.set(tokenizer.nextToken());
				context.write(word, one);
			}
		}
		}
	
	public static class WordCountReduce extends Reducer <Text, IntWritable, Text, IntWritable> {
		
		public void reduce(Text key, Iterable<IntWritable> values, Context context)
				throws IOException, InterruptedException {
			int sum = 0;
			for (IntWritable val : values) {
				sum += val.get();
			}
			context.write(key, new IntWritable(sum));
	    }
	}
	
	public static void main(String[] args) throws Exception {
		Configuration conf = new Configuration();  
		Job job = new Job(conf);  
		job.setJarByClass(WordCount.class);  
		job.setJobName("wordcount");  

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

		job.setMapperClass(WordCountMap.class);  
		job.setReducerClass(WordCountReduce.class);  

		job.setInputFormatClass(TextInputFormat.class);  
		job.setOutputFormatClass(TextOutputFormat.class);  

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

		job.waitForCompletion(true);  
		
	}
}

Mapper 的輸入類型爲文本,鍵用 Object 代替,值爲文本 (Text)。編程

Mapper 的輸出類型爲文本,鍵爲 Text,值爲 IntWritable,至關於java中Integer整型變量。將分割後的字符串造成鍵值對 <單詞,1>。併發

對於每一行輸入文本,都會調用一次 map 方法,對輸入的行進行切分。app

while (tokenizer.hasMoreTokens()){
    word.set(tokenizer.nextToken());
    context.write(word, one);
}

將一行文本變爲<單詞,出現次數>這樣的鍵值對。eclipse

對於每一個鍵,都會調用一次 reduce 方法,對鍵出現次數進行求和。oop

運行測試

用 eclipse 導出 WordCount 的 Runable jar 包,放到目錄 hadoop-2.6.0/bin測試

在目錄 hadoop-2.6.0/bin 下新建 input 文件夾,並新建文件 file1, file2。.net

file1 內容爲 one titus two titus three tituscode

file2 內容爲 one huangyi two huangyi

.
├── container-executor
├── hadoop
├── hadoop.cmd
├── hdfs
├── hdfs.cmd
├── input
│   ├── file1.txt
│   └── file2.txt
├── mapred
├── mapred.cmd
├── rcc
├── test-container-executor
├── wordcount.jar
├── yarn
└── yarn.cmd

運行 ./hadoop jar wordcount.jar input output

會生成 output 目錄和結果。

huangyi	2
one	2
three	1
titus	3
two	2

參考

http://blog.csdn.net/jediael_lu/article/details/38637277

http://blog.csdn.net/jediael_lu/article/details/38705371

七週七併發編程模型. Page 199~203

相關文章
相關標籤/搜索