大數(shù)據(jù)分析Hadoop Streaming編程實(shí)戰(zhàn)之C++、Php、Python
2018-03-05 15:50:44
1861瀏覽
Streaming框架允許任何程序語(yǔ)言實(shí)現(xiàn)的程序在HadoopMapReduce中使用,方便已有程序向Hadoop平臺(tái)移植。因此可以說(shuō)對(duì)于hadoop的擴(kuò)展性意義重大。接下來(lái)我們分別使用C++、Php、Python語(yǔ)言實(shí)現(xiàn)HadoopWordCount。
實(shí)戰(zhàn)一:C++語(yǔ)言實(shí)現(xiàn)Wordcount
代碼實(shí)現(xiàn):
1)C++語(yǔ)言實(shí)現(xiàn)WordCount中的Mapper,文件命名為mapper.cpp,以下是詳細(xì)代碼
#include
#include
#include
usingnamespacestd;
intmain(){
stringkey;
stringvalue="1";
while(cin>>key){
cout<}
return0;
}
2)C++語(yǔ)言實(shí)現(xiàn)WordCount中的Reducer,文件命名為reducer.cpp,以下是詳細(xì)代碼
#include
#include
#include
#include
usingnamespacestd;
intmain(){
stringkey;
stringvalue;
mapword2count;
map::iteratorit;
while(cin>>key){
cin>>value;
it=word2count.find(key);
if(it!=word2count.end()){
(it->second)++;
}
else{
word2count.insert(make_pair(key,1));
}
}
for(it=word2count.begin();it!=word2count.end();++it){
cout}
return0;
}
測(cè)試運(yùn)行C++實(shí)現(xiàn)Wordcount的具體步驟
1)在線安裝C++
在Linux環(huán)境下,如果沒(méi)有安裝C++,需要我們?cè)诰€安裝C++
yum-yinstallgcc-c++
2)對(duì)c++文件編譯,生成可執(zhí)行文件
我們通過(guò)以下命令將C++程序編譯成可執(zhí)行文件,然后才能夠運(yùn)行
g++-omappermapper.cpp
g++-oreducerreducer.cpp
3)本地測(cè)試
集群運(yùn)行C++版本的WordCount之前,首先要在Linux本地測(cè)試運(yùn)行,調(diào)試成功,確保程序在集群中正常運(yùn)行,測(cè)試運(yùn)行命令如下:
catdjt.txt|./mapper|sort|./reducer
4)集群運(yùn)行
切換到hadoop安裝目錄下,提交C++版本的WordCount作業(yè),進(jìn)行單詞統(tǒng)計(jì)。
hadoopjar/usr/java/hadoop/share/hadoop/tools/lib/hadoop-streaming-2.2.0.jar
-Dmapred.reduce.tasks=2
-mapper"./mapper"
-reducer"./reducer"
-filemapper
-filereducer
-input/dajiangtai/djt.txt
-output/dajiangtai/out
如果最終出現(xiàn)想要的結(jié)果,說(shuō)明C++語(yǔ)言成功實(shí)現(xiàn)Wordcount
實(shí)戰(zhàn)二:Php語(yǔ)言實(shí)現(xiàn)Wordcount
代碼實(shí)現(xiàn):
1)Php語(yǔ)言實(shí)現(xiàn)WordCount中的Mapper,文件命名為wc_mapper.php,以下是詳細(xì)代碼
#!/usr/bin/php
error_reporting(E_ALL^E_NOTICE);
$word2count=array();
while(($line=fgets(STDIN))!==false){
$line=trim($line);
$words=preg_split('/\W/',$line,0,PREG_SPLIT_NO_EMPTY);
foreach($wordsas$word){
echo$word,chr(9),"1",PHP_EOL;
}
}
?>
2)Php語(yǔ)言實(shí)現(xiàn)WordCount中的Reducer,文件命名為wc_reducer.php,以下是詳細(xì)代碼
#!/usr/bin/php
error_reporting(E_ALL^E_NOTICE);
$word2count=array();
while(($line=fgets(STDIN))!==false){
$line=trim($line);
list($word,$count)=explode(chr(9),$line);
$count=intval($count);
$word2count[$word]+=$count;
}
foreach($word2countas$word=>$count){
echo$word,chr(9),$count,PHP_EOL;
}
?>
測(cè)試運(yùn)行Php實(shí)現(xiàn)Wordcount的具體步驟
1)在線安裝Php
在Linux環(huán)境下,如果沒(méi)有安裝Php,需要我們?cè)诰€安裝Php環(huán)境
yum-yinstallphp
2)本地測(cè)試
集群運(yùn)行Php版本的WordCount之前,首先要在Linux本地測(cè)試運(yùn)行,調(diào)試成功,確保程序在集群中正常運(yùn)行,測(cè)試運(yùn)行命令如下:
catdjt.txt|phpwc_mapper.php|sort|phpwc_reducer.php
3)集群運(yùn)行
切換到hadoop安裝目錄下,提交Php版本的WordCount作業(yè),進(jìn)行單詞統(tǒng)計(jì)。
hadoopjar/usr/java/hadoop/share/hadoop/tools/lib/hadoop-streaming-2.2.0.jar
-Dmapred.reduce.tasks=2
-mapper"phpwc_mapper.php"
-reducer"phpwc_reducer.php"
-filewc_mapper.php
-filewc_reducer.php
-input/dajiangtai/djt.txt
-output/dajiangtai/out
如果最終出現(xiàn)想要的結(jié)果,說(shuō)明Php語(yǔ)言成功實(shí)現(xiàn)Wordcount
實(shí)戰(zhàn)三:Python語(yǔ)言實(shí)現(xiàn)Wordcount
代碼實(shí)現(xiàn):
1)Python語(yǔ)言實(shí)現(xiàn)WordCount中的Mapper,文件命名為Mapper.py,以下是詳細(xì)代碼
#!/usr/java/hadoop/envpython
importsys
word2count={}
forlineinsys.stdin:
line=line.strip()
words=filter(lambdaword:word,line.split())
forwordinwords:
print'%s\t%s'%(word,1)
2)Python語(yǔ)言實(shí)現(xiàn)WordCount中的Reducer,文件命名為Reducer.py,以下是詳細(xì)代碼
#!/usr/java/hadoop/envpython
fromoperatorimportitemgetter
importsys
word2count={}
forlineinsys.stdin:
line=line.strip()
word,count=line.split()
try:
count=int(count)
word2count[word]=word2count.get(word,0)+count
exceptValueError:
pass
sorted_word2count=sorted(word2count.items(),key=itemgetter(0))
forword,countinsorted_word2count:
print'%s\t%s'%(word,count)
測(cè)試運(yùn)行Python實(shí)現(xiàn)Wordcount的具體步驟
1)在線安裝Python
在Linux環(huán)境下,如果沒(méi)有安裝Python,需要我們?cè)诰€安裝Python環(huán)境
yum-yinstallpython27
2)本地測(cè)試
集群運(yùn)行Python版本的WordCount之前,首先要在Linux本地測(cè)試運(yùn)行,調(diào)試成功,確保程序在集群中正常運(yùn)行,測(cè)試運(yùn)行命令如下:
catdjt.txt|pythonMapper.py|sort|pythonReducer.py
3)集群運(yùn)行
切換到hadoop安裝目錄下,提交Python版本的WordCount作業(yè),進(jìn)行單詞統(tǒng)計(jì)。
hadoopjar/usr/java/hadoop/share/hadoop/tools/lib/hadoop-streaming-2.2.0.jar
-Dmapred.reduce.tasks=2
-mapper"pythonMapper.py"
-reducer"pythonReducer.py"
-fileMapper.py
-fileReducer.py
-input/dajiangtai/djt.txt
-output/dajiangtai/out
如果最終出現(xiàn)想要的結(jié)果,說(shuō)明Python語(yǔ)言成功實(shí)現(xiàn)Wordcount。最后想要了解更多關(guān)于大數(shù)據(jù)發(fā)展前景趨勢(shì),請(qǐng)關(guān)注扣丁學(xué)堂官網(wǎng)、微信等平臺(tái),扣丁學(xué)堂IT職業(yè)在線學(xué)習(xí)教育平臺(tái)為您提供權(quán)威的大數(shù)據(jù)培訓(xùn)視頻教程系統(tǒng),通過(guò)千鋒旗下金牌講師在線錄制的大數(shù)據(jù)視頻教程系統(tǒng),讓你快速掌握大數(shù)據(jù)從入門(mén)到精通大數(shù)據(jù)開(kāi)發(fā)實(shí)戰(zhàn)技能??鄱W(xué)堂大數(shù)據(jù)學(xué)習(xí)群:209080834。
【關(guān)注微信公眾號(hào)獲取更多學(xué)習(xí)資料】
查看更多關(guān)于“大數(shù)據(jù)培訓(xùn)資訊”的相關(guān)文章>>
標(biāo)簽:
大數(shù)據(jù)分析
大數(shù)據(jù)培訓(xùn)
大數(shù)據(jù)視頻教程
Hadoop視頻教程
大數(shù)據(jù)開(kāi)發(fā)工程師
大數(shù)據(jù)在線視頻