Hadoop中WordCount如何實(shí)現(xiàn)

小編給大家分享一下Hadoop中WordCount如何實(shí)現(xiàn),相信大部分人都還不怎么了解,因此分享這篇文章給大家參考一下,希望大家閱讀完這篇文章后大有收獲,下面讓我們一起去了解一下吧!

創(chuàng)新互聯(lián)建站自2013年創(chuàng)立以來(lái),是專(zhuān)業(yè)互聯(lián)網(wǎng)技術(shù)服務(wù)公司,擁有項(xiàng)目成都網(wǎng)站設(shè)計(jì)、網(wǎng)站制作網(wǎng)站策劃,項(xiàng)目實(shí)施與項(xiàng)目整合能力。我們以讓每一個(gè)夢(mèng)想脫穎而出為使命,1280元溫宿做網(wǎng)站,已為上家服務(wù),為溫宿各地企業(yè)和個(gè)人服務(wù),聯(lián)系電話:028-86922220

WordCount 是 Hadoop 應(yīng)用最經(jīng)典的例子。

使用 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 的輸入類(lèi)型為文本,鍵用 Object 代替,值為文本 (Text)。

Mapper 的輸出類(lèi)型為文本,鍵為 Text,值為 IntWritable,相當(dāng)于java中Integer整型變量。將分割后的字符串形成鍵值對(duì) <單詞,1>。

對(duì)于每一行輸入文本,都會(huì)調(diào)用一次 map 方法,對(duì)輸入的行進(jìn)行切分。

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

將一行文本變?yōu)?lt;單詞,出現(xiàn)次數(shù)>這樣的鍵值對(duì)。

對(duì)于每個(gè)鍵,都會(huì)調(diào)用一次 reduce 方法,對(duì)鍵出現(xiàn)次數(shù)進(jìn)行求和。

運(yùn)行測(cè)試

用 eclipse 導(dǎo)出 WordCount 的 Runable jar 包,放到目錄 hadoop-2.6.0/bin。

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

file1 內(nèi)容為 one titus two titus three titus

file2 內(nèi)容為 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

運(yùn)行 ./hadoop jar wordcount.jar input output

會(huì)生成 output 目錄和結(jié)果。

huangyi	2
one	2
three	1
titus	3
two	2

以上是“Hadoop中WordCount如何實(shí)現(xiàn)”這篇文章的所有內(nèi)容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內(nèi)容對(duì)大家有所幫助,如果還想學(xué)習(xí)更多知識(shí),歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道!

分享標(biāo)題:Hadoop中WordCount如何實(shí)現(xiàn)
文章分享:http://bm7419.com/article34/pcddse.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站導(dǎo)航、外貿(mào)網(wǎng)站建設(shè)微信公眾號(hào)、小程序開(kāi)發(fā)域名注冊(cè)、響應(yīng)式網(wǎng)站

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶(hù)投稿、用戶(hù)轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)

成都app開(kāi)發(fā)公司