hadoop的wordcount實(shí)例代碼
可以通過(guò)一個(gè)簡(jiǎn)單的例子來(lái)說(shuō)明MapReduce到底是什么:
我們要統(tǒng)計(jì)一個(gè)大文件中的各個(gè)單詞出現(xiàn)的次數(shù)。由于文件太大。我們把這個(gè)文件切分成如果小文件,然后安排多個(gè)人去統(tǒng)計(jì)。這個(gè)過(guò)程就是”Map”。然后把每個(gè)人統(tǒng)計(jì)的數(shù)字合并起來(lái),這個(gè)就是“Reduce"。
上面的例子如果在MapReduce去做呢,就需要?jiǎng)?chuàng)建一個(gè)任務(wù)job,由job把文件切分成若干獨(dú)立的數(shù)據(jù)塊,并分布在不同的機(jī)器節(jié)點(diǎn)中。然后通過(guò)分散在不同節(jié)點(diǎn)中的Map任務(wù)以完全并行的方式進(jìn)行處理。MapReduce會(huì)對(duì)Map的輸出地行收集,再將結(jié)果輸出送給Reduce進(jìn)行下一步的處理。
對(duì)于一個(gè)任務(wù)的具體執(zhí)行過(guò)程,會(huì)有一個(gè)名為"JobTracker"的進(jìn)程負(fù)責(zé)協(xié)調(diào)MapReduce執(zhí)行過(guò)程中的所有任務(wù)。若干條TaskTracker進(jìn)程用來(lái)運(yùn)行單獨(dú)的Map任務(wù),并隨時(shí)將任務(wù)的執(zhí)行情況匯報(bào)給JobTracker。如果一個(gè)TaskTracker匯報(bào)任務(wù)失敗或者長(zhǎng)時(shí)間未對(duì)本身任務(wù)進(jìn)行匯報(bào),JobTracker會(huì)啟動(dòng)另外一個(gè)TaskTracker重新執(zhí)行單獨(dú)的Map任務(wù)。
下面的具體的代碼實(shí)現(xiàn):
1. 編寫(xiě)wordcount的相關(guān)job
(1)eclipse下創(chuàng)建相關(guān)maven項(xiàng)目,依賴(lài)jar包如下(也可參照hadoop源碼包下的hadoop-mapreduce-examples項(xiàng)目的pom配置)
注意:要配置一個(gè)maven插件maven-jar-plugin,并指定mainClass
<dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.11</version> </dependency> <dependency> <groupId>org.apache.hadoop</groupId> <artifactId>hadoop-mapreduce-client-core</artifactId> <version>2.5.2</version> </dependency> <dependency> <groupId>org.apache.hadoop</groupId> <artifactId>hadoop-common</artifactId> <version>2.5.2</version> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-jar-plugin</artifactId> <configuration> <archive> <manifest> <mainClass>com.xxx.demo.hadoop.wordcount.WordCount</mainClass> </manifest> </archive> </configuration> </plugin> </plugins> </build>
(2)根據(jù)MapReduce的運(yùn)行機(jī)制,一個(gè)job至少要編寫(xiě)三個(gè)類(lèi)分別用來(lái)完成Map邏輯、Reduce邏輯、作業(yè)調(diào)度這三件事。
Map的代碼可繼承org.apache.hadoop.mapreduce.Mapper類(lèi)
public static class TokenizerMapper extends Mapper<Object, Text, Text, IntWritable>{ private final static IntWritable one = new IntWritable(1); private Text word = new Text(); //由于該例子未用到key的參數(shù),所以該處key的類(lèi)型就簡(jiǎn)單指定為Object public void map(Object key, Text value, Context context ) throws IOException, InterruptedException { StringTokenizer itr = new StringTokenizer(value.toString()); while (itr.hasMoreTokens()) { word.set(itr.nextToken()); context.write(word, one); } } }
Reduce的代碼可繼承org.apache.hadoop.mapreduce.Reducer類(lèi)
public class IntSumReducer extends Reducer<Text,IntWritable,Text,IntWritable> { private IntWritable result = new IntWritable(); public void reduce(Text key, Iterable<IntWritable> values, Context context ) throws IOException, InterruptedException { int sum = 0; for (IntWritable val : values) { sum += val.get(); } result.set(sum); context.write(key, result); } }
編寫(xiě)main方法進(jìn)行作業(yè)調(diào)度
public static void main(String[] args) throws Exception { Configuration conf = new Configuration(); Job job = Job.getInstance(conf, "word count"); job.setJarByClass(WordCount.class); job.setMapperClass(TokenizerMapper.class); job.setCombinerClass(IntSumReducer.class); job.setReducerClass(IntSumReducer.class); job.setOutputKeyClass(Text.class); job.setOutputValueClass(IntWritable.class); FileInputFormat.addInputPath(job, new Path(args[0])); FileOutputFormat.setOutputPath(job, new Path(args[1])); job.waitForCompletion(true) ; //System.exit(job.waitForCompletion(true) ? 0 : 1); }
2. 上傳數(shù)據(jù)文件到hadoop集群環(huán)境
執(zhí)行mvn install把項(xiàng)目打成jar文件然后上傳到linux集群環(huán)境,使用hdfs dfs -mkdir命令在hdfs文件系統(tǒng)中創(chuàng)建相應(yīng)的命令,使用hdfs dfs -put 把需要處理的數(shù)據(jù)文件上傳到hdfs系統(tǒng)中,示例:hdfs dfs -put ${linux_path/數(shù)據(jù)文件} ${hdfs_path}
3. 執(zhí)行job
在集群環(huán)境中執(zhí)行命令: hadoop jar ${linux_path}/wordcount.jar ${hdfs_input_path} ${hdfs_output_path}
4. 查看統(tǒng)計(jì)結(jié)果
hdfs dfs -cat ${hdfs_output_path}/輸出文件名
以上的方式在未啟動(dòng)hadoop集群環(huán)境時(shí),是以Local模式運(yùn)行,此時(shí)HDFS和YARN都不起作用。下面是在偽分布式模式下執(zhí)行mapreduce job時(shí)需要做的工作,先把官網(wǎng)上列的步驟摘錄出來(lái):
配置主機(jī)名
# vi /etc/sysconfig/network
例如:
NETWORKING=yes HOSTNAME=master vi /etc/hosts
填入以下內(nèi)容
127.0.0.1 localhost
配置ssh免密碼互通
ssh-keygen -t rsa
# cat?~/.ssh/id_rsa.pub?>>?~/.ssh/authorized_keys
配置core-site.xml文件(位于${HADOOP_HOME}/etc/hadoop/
<configuration> <property> <name>fs.defaultFS</name> <value>hdfs://localhost:9000</value> </property> </configuration>
配置hdfs-site.xml文件
<configuration> <property> <name>dfs.replication</name> <value>1</value> </property> </configuration>
下面的命令可以在單機(jī)偽分布模式下運(yùn)行mapreduce的job
1.Format the filesystem:
$ bin/hdfs namenode -format
2.Start NameNode daemon and DataNode daemon:
$ sbin/start-dfs.sh
3.The hadoop daemon log output is written to the $HADOOP_LOG_DIR directory (defaults to $HADOOP_HOME/logs).4.Browse the web interface for the NameNode; by default it is available at:
NameNode - http://localhost:50070/
Make the HDFS directories required to execute MapReduce jobs:
$ bin/hdfs dfs -mkdir /user
$ bin/hdfs dfs -mkdir /user/<username>
5.Copy the input files into the distributed filesystem:
$ bin/hdfs dfs -put etc/hadoop input
6.Run some of the examples provided:
$ bin/hadoop jar share/hadoop/mapreduce/hadoop-mapreduce-examples-2.5.2.jar grep input output 'dfs[a-z.]+'
7.Examine the output files:
Copy the output files from the distributed filesystem to the local filesystem and examine them:$ bin/hdfs dfs -get output output
$ cat output/*
orView the output files on the distributed filesystem:
$ bin/hdfs dfs -cat output/*
8.When you're done, stop the daemons with:
$ sbin/stop-dfs.sh
總結(jié)
以上就是本文關(guān)于hadoop的wordcount實(shí)例代碼的全部?jī)?nèi)容,希望對(duì)大家有所幫助。感興趣的朋友可以繼續(xù)參閱本站其他相關(guān)專(zhuān)題,如有不足之處,歡迎留言指出。感謝朋友們對(duì)本站的支持!
- Hadoop之NameNode Federation圖文詳解
- Hadoop 2.X新特性回收站功能的講解
- Hadoop計(jì)數(shù)器的應(yīng)用以及數(shù)據(jù)清洗
- ubantu 16.4下Hadoop完全分布式搭建實(shí)戰(zhàn)教程
- Hadoop 2.x與3.x 22點(diǎn)比較,Hadoop 3.x比2.x的改進(jìn)
- ubuntu docker搭建Hadoop集群環(huán)境的方法
- 在CentOS中搭建Hadoop的詳細(xì)步驟
- Java/Web調(diào)用Hadoop進(jìn)行MapReduce示例代碼
- Hadoop中namenode和secondarynamenode工作機(jī)制講解
相關(guān)文章
詳解Java并發(fā)編程之volatile關(guān)鍵字
這篇文章主要為大家介紹了Java并發(fā)編程之volatile關(guān)鍵字,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來(lái)幫助2021-11-11解決SpringMvc中普通類(lèi)注入Service為null的問(wèn)題
這篇文章主要介紹了解決SpringMvc中普通類(lèi)注入Service為null的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-07-07Java常用HASH算法總結(jié)【經(jīng)典實(shí)例】
這篇文章主要介紹了Java常用HASH算法,結(jié)合實(shí)例形式總結(jié)分析了Java常用的Hash算法,包括加法hash、旋轉(zhuǎn)hash、FNV算法、RS算法hash、PJW算法、ELF算法、BKDR算法、SDBM算法、DJB算法、DEK算法、AP算法等,需要的朋友可以參考下2017-09-09Java實(shí)現(xiàn)求解一元n次多項(xiàng)式的方法示例
這篇文章主要介紹了Java實(shí)現(xiàn)求解一元n次多項(xiàng)式的方法,涉及java高斯消元法處理矩陣運(yùn)算解多項(xiàng)式的相關(guān)操作技巧,需要的朋友可以參考下2018-01-01SpringBoot AOP注解失效問(wèn)題排查與解決(調(diào)用內(nèi)部方法)
這篇文章主要介紹了SpringBoot AOP注解失效問(wèn)題排查與解決(調(diào)用內(nèi)部方法),文中通過(guò)代碼示例介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作有一定的幫助,需要的朋友可以參考下2024-04-04Feign如何解決服務(wù)之間調(diào)用傳遞token
這篇文章主要介紹了Feign如何解決服務(wù)之間調(diào)用傳遞token,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-03-03Java中的Semaphore信號(hào)量簡(jiǎn)單使用代碼實(shí)例
這篇文章主要介紹了Java中的Semaphore信號(hào)量簡(jiǎn)單使用代碼實(shí)例,Semaphore是用來(lái)保護(hù)一個(gè)或者多個(gè)共享資源的訪問(wèn),Semaphore內(nèi)部維護(hù)了一個(gè)計(jì)數(shù)器,其值為可以訪問(wèn)的共享資源的個(gè)數(shù),一個(gè)線程要訪問(wèn)共享資源,需要的朋友可以參考下2023-12-12Springboot 使用maven release插件執(zhí)行版本管理及打包操作
maven-release-plugin 可用于構(gòu)建release版本項(xiàng)目,實(shí)現(xiàn)自動(dòng)打tag、遞增版本號(hào)、分發(fā)release版本jar包至倉(cāng)庫(kù),接下來(lái)通過(guò)本文給大家介紹Springboot 使用maven release插件執(zhí)行版本管理及打包操作,需要的朋友可以參考下2022-03-03Mybatis往Mapper.xml文件中傳遞多個(gè)參數(shù)問(wèn)題
這篇文章主要介紹了Mybatis往Mapper.xml文件中傳遞多個(gè)參數(shù)問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-05-05