温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

Hadoop中WordCount如何实现

发布时间:2021-12-09 15:11:59 来源:亿速云 阅读:109 作者:小新 栏目:云计算

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

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 <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 方法,对输入的行进行切分。

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如何实现”这篇文章的所有内容,感谢各位的阅读!相信大家都有了一定的了解,希望分享的内容对大家有所帮助,如果还想学习更多知识,欢迎关注亿速云行业资讯频道!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI