hadoop的分布式計(jì)算框架MapReduce

MapReduce概述

源自于Google的MapReduce論文,論文發(fā)表于2004年12月。

Hadoop MapReduce是Google MapReduce的克隆版。

MapReduce優(yōu)點(diǎn):海量數(shù)據(jù)離線處理&易開(kāi)發(fā)&易運(yùn)行。

MapReduce缺點(diǎn):實(shí)時(shí)流式計(jì)算。

wordcount:統(tǒng)計(jì)文件中每個(gè)單詞出現(xiàn)的次數(shù)

需求:求wordcount

1)文件內(nèi)容大小:shell

2)文件內(nèi)容很大:比如TB、GB,那么如何解決大數(shù)據(jù)量的統(tǒng)計(jì)分析吶?

工作中很多場(chǎng)景的開(kāi)發(fā)都是wc的基礎(chǔ)上進(jìn)行改造的。

以上需求,都需要借用分布式計(jì)算框架來(lái)解決了:MapReduce


MapReduce編程模型

? ? ?1、準(zhǔn)備map處理的輸入數(shù)據(jù)

? ? ?2、Mapper處理

? ? 3、 Shuffle

? ? 4、 Reduce處理

? ? 5、 結(jié)果輸出

MapReduce架構(gòu)


Split

InputFormat

OutputFormat

Combiner

Partitioner



MapReduce編程

使用java 來(lái)開(kāi)發(fā)wordcount案例

使用IDEA+MAVEN開(kāi)發(fā)wordcount案例:

1)開(kāi)發(fā)

2)編譯

3)jar包上傳到服務(wù)器

4)運(yùn)行?

hadoop jar /home/hadoop/lib/你開(kāi)發(fā)的jar名字.jar? 主程序class名稱 參數(shù)1? 參數(shù)2

例如: hadoop jar /usr/hadoop/hadoop-2.6.0-cdh5.7.0/lib/bigdata-1.1.1.jar com.sc.mapreduce.WordCountApp hdfs://10.6.24.143:8020/xxx/test.txt hdfs://10.6.24.143:8020/xxx/output/wc

常見(jiàn)錯(cuò)誤

相同的代碼和腳本,再次執(zhí)行會(huì)包錯(cuò):

WARN security.UserGroupInformation: PriviledgedActionException as:root (auth:SIMPLE) cause:org.apache.hadoop.mapred.FileAlreadyExistsException: Output directory hdfs://10.6.24.143:8020/xxx/output/wc already exists

Exception in thread "main" org.apache.hadoop.mapred.FileAlreadyExistsException: Output directory hdfs://10.6.24.143:8020/xxx/output/wc already exists

原因:在MapReduce中,輸出文件是不能事先存在的,要想避免這個(gè)問(wèn)題,有兩種方式:

第一種方式:就需要我們手工通過(guò)

shell的方式將輸出文件刪除。

hadoop fs -rm -r /xxx/output/wc

第二種方式:通過(guò)java api的方式(推薦)

//刪除存在的輸出目錄

Path outputPath=new Path(args[1]);

FileSystem fileSystem = FileSystem.get(configuration);

if (fileSystem.exists(outputPath)){

fileSystem.delete(outputPath,true);//遞歸刪除

}

MapReduce編程之Combiner


Combiner 就是在map階段提前做reduce的操作,減少傳輸?shù)絩educe的key的數(shù)量,即是減少傳輸數(shù)據(jù)量,縮短傳輸時(shí)間。

雖然combiner有優(yōu)點(diǎn),但并非所有場(chǎng)景都適合:

求和,計(jì)數(shù)場(chǎng)景:適合

求平均數(shù)場(chǎng)景:不適合

MapReduce編程之Partitioner

1)Partitioner決定MapTask輸出的數(shù)據(jù)交由哪個(gè)ReduceTask處理。

2)默認(rèn)實(shí)現(xiàn):分發(fā)的key的hash值對(duì)Reduce Task個(gè)數(shù)取模

案例如下:


按照指定規(guī)則類(lèi)型來(lái)分類(lèi)

我們執(zhí)行程序看到有幾個(gè)reduce就會(huì)生成幾個(gè)輸出文件


JobHistory

作用:

記錄已經(jīng)運(yùn)行完的MapReduce信息到指定的HDFS目錄下。

默認(rèn)是不開(kāi)啟的。

如何啟動(dòng)吶?

第一步完成

yarn-site.xml的配置

<property>

<name>yarn.log-aggregation-enable</name>

<value>true</value>

</property>

mapred-site.xml的配置

<property>

<name>mapreduce.jobhistory.address</name>

<value>10.6.24.143:10020</value>

</property>

<property>

<name>mapreduce.jobhistory.webapp.address</name>

<value>10.6.24.143:19888</value>

</property>

<--執(zhí)行完畢作業(yè)的日志存放目錄-->

<property>

<name>mapreduce.jobhistory.done-dir</name>

<value>/history/done</value>

</property>

<--執(zhí)行中的作業(yè)日志存放目錄-->

<property>

<name>mapreduce.jobhistory.intermediate-done-dir</name>

<value>/history/done_intermediate</value>

</property>


第二步重啟yarn服務(wù)

./stop-yarn.sh

./start-yarn.sh

第三步啟動(dòng)jobHistory服務(wù)

./mr-jobhistory-daemon.sh start historyserver

如果向停止jobHistory服務(wù)命令

./mr-jobhistory-daemon.sh stop historyserver

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容