Hadoop中WordCount如何实现

小编给大家分享一下Hadoop中WordCount如何实现,相信大部分人都还不怎么了解,因此分享这篇文章给大家参考一下,希望大家阅读完这篇文章后大有收获,下面让我们一起去了解一下吧!

创新互联建站自2013年创立以来,是专业互联网技术服务公司,拥有项目成都网站设计、网站制作网站策划,项目实施与项目整合能力。我们以让每一个梦想脱颖而出为使命,1280元温宿做网站,已为上家服务,为温宿各地企业和个人服务,联系电话:028-86922220

WordCount 是 Hadoop 应用最经典的例子。

使用 hadoop-2.6.0 版本,需要引入的包目录位于 hadoop-2.6.0/share/hadoop/common/lib

源码

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  {
		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  {
		
		public void reduce(Text key, Iterable 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 方法,对输入的行进行切分。

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

将一行文本变为<单词,出现次数>这样的键值对。

对于每个键,都会调用一次 reduce 方法,对键出现次数进行求和。

运行测试

用 eclipse 导出 WordCount 的 Runable jar 包,放到目录 hadoop-2.6.0/bin

在目录 hadoop-2.6.0/bin 下新建 input 文件夹,并新建文件 file1, file2。

file1 内容为 one titus two titus three titus

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

以上是“Hadoop中WordCount如何实现”这篇文章的所有内容,感谢各位的阅读!相信大家都有了一定的了解,希望分享的内容对大家有所帮助,如果还想学习更多知识,欢迎关注创新互联行业资讯频道!


网页名称:Hadoop中WordCount如何实现
网页URL:http://pwwzsj.com/article/pcddse.html