hadoop的wordcount實(shí)例代碼
可以通過一個簡單的例子來說明MapReduce到底是什么:
我們要統(tǒng)計(jì)一個大文件中的各個單詞出現(xiàn)的次數(shù)。由于文件太大。我們把這個文件切分成如果小文件,然后安排多個人去統(tǒng)計(jì)。這個過程就是”Map”。然后把每個人統(tǒng)計(jì)的數(shù)字合并起來,這個就是“Reduce"。
上面的例子如果在MapReduce去做呢,就需要創(chuàng)建一個任務(wù)job,由job把文件切分成若干獨(dú)立的數(shù)據(jù)塊,并分布在不同的機(jī)器節(jié)點(diǎn)中。然后通過分散在不同節(jié)點(diǎn)中的Map任務(wù)以完全并行的方式進(jìn)行處理。MapReduce會對Map的輸出地行收集,再將結(jié)果輸出送給Reduce進(jìn)行下一步的處理。
對于一個任務(wù)的具體執(zhí)行過程,會有一個名為"JobTracker"的進(jìn)程負(fù)責(zé)協(xié)調(diào)MapReduce執(zhí)行過程中的所有任務(wù)。若干條TaskTracker進(jìn)程用來運(yùn)行單獨(dú)的Map任務(wù),并隨時將任務(wù)的執(zhí)行情況匯報(bào)給JobTracker。如果一個TaskTracker匯報(bào)任務(wù)失敗或者長時間未對本身任務(wù)進(jìn)行匯報(bào),JobTracker會啟動另外一個TaskTracker重新執(zhí)行單獨(dú)的Map任務(wù)。
下面的具體的代碼實(shí)現(xiàn):
1. 編寫wordcount的相關(guān)job
(1)eclipse下創(chuàng)建相關(guān)maven項(xiàng)目,依賴jar包如下(也可參照hadoop源碼包下的hadoop-mapreduce-examples項(xiàng)目的pom配置)
注意:要配置一個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ī)制,一個job至少要編寫三個類分別用來完成Map邏輯、Reduce邏輯、作業(yè)調(diào)度這三件事。
Map的代碼可繼承org.apache.hadoop.mapreduce.Mapper類
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的類型就簡單指定為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類
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);
}
}
編寫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}/輸出文件名
以上的方式在未啟動hadoop集群環(huán)境時,是以Local模式運(yùn)行,此時HDFS和YARN都不起作用。下面是在偽分布式模式下執(zhí)行mapreduce job時需要做的工作,先把官網(wǎng)上列的步驟摘錄出來:
配置主機(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í)例代碼的全部內(nèi)容,希望對大家有所幫助。感興趣的朋友可以繼續(xù)參閱本站其他相關(guān)專題,如有不足之處,歡迎留言指出。感謝朋友們對本站的支持!
- 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)鍵字,具有一定的參考價值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來幫助2021-11-11
解決SpringMvc中普通類注入Service為null的問題
這篇文章主要介紹了解決SpringMvc中普通類注入Service為null的問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-07-07
Java常用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-09
Java實(shí)現(xiàn)求解一元n次多項(xiàng)式的方法示例
這篇文章主要介紹了Java實(shí)現(xiàn)求解一元n次多項(xiàng)式的方法,涉及java高斯消元法處理矩陣運(yùn)算解多項(xiàng)式的相關(guān)操作技巧,需要的朋友可以參考下2018-01-01
SpringBoot AOP注解失效問題排查與解決(調(diào)用內(nèi)部方法)
這篇文章主要介紹了SpringBoot AOP注解失效問題排查與解決(調(diào)用內(nèi)部方法),文中通過代碼示例介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作有一定的幫助,需要的朋友可以參考下2024-04-04
Feign如何解決服務(wù)之間調(diào)用傳遞token
這篇文章主要介紹了Feign如何解決服務(wù)之間調(diào)用傳遞token,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-03-03
Java中的Semaphore信號量簡單使用代碼實(shí)例
這篇文章主要介紹了Java中的Semaphore信號量簡單使用代碼實(shí)例,Semaphore是用來保護(hù)一個或者多個共享資源的訪問,Semaphore內(nèi)部維護(hù)了一個計(jì)數(shù)器,其值為可以訪問的共享資源的個數(shù),一個線程要訪問共享資源,需要的朋友可以參考下2023-12-12
Springboot 使用maven release插件執(zhí)行版本管理及打包操作
maven-release-plugin 可用于構(gòu)建release版本項(xiàng)目,實(shí)現(xiàn)自動打tag、遞增版本號、分發(fā)release版本jar包至倉庫,接下來通過本文給大家介紹Springboot 使用maven release插件執(zhí)行版本管理及打包操作,需要的朋友可以參考下2022-03-03
Mybatis往Mapper.xml文件中傳遞多個參數(shù)問題
這篇文章主要介紹了Mybatis往Mapper.xml文件中傳遞多個參數(shù)問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-05-05

