教你用Python 編寫 Hadoop MapReduce 程序


摘要:Hadoop Streaming 使用 MapReduce 框架,該框架可用於編寫應用程序來處理海量數據。

本文分享自華為雲社區《Hadoop Streaming:用 Python 編寫 Hadoop MapReduce 程序》,作者:Donglian Lin。

隨着數字媒體、物聯網等發展的出現,每天產生的數字數據量呈指數級增長。這種情況給創建下一代工具和技術來存儲和操作這些數據帶來了挑戰。這就是 Hadoop Streaming 的用武之地!下面給出的圖表描繪了從2013年起全球每年產生的數據增長情況。IDC估計,到 2025年,每年產生的數據量將達到180 Zettabytes!

IBM 表示,每天有近 2.5 千萬字節的數據被創建,其中 90% 的世界數據是在過去兩年中創建的!存儲如此龐大的數據量是一項具有挑戰性的任務。Hadoop 可以比傳統的企業數據倉庫更有效地處理大量結構化和非結構化數據。它跨分布式計算機集群存儲這些龐大的數據集。Hadoop Streaming 使用 MapReduce 框架,該框架可用於編寫應用程序來處理海量數據。

由於 MapReduce 框架基於 Java,您可能想知道如果開發人員沒有 Java 經驗,他/她如何工作。好吧,開發人員可以使用他們喜歡的語言編寫 mapper/Reducer 應用程序,而無需掌握太多 Java 知識,使用Hadoop Streaming而不是切換到 Pig 和 Hive 等新工具或技術。

什么是 Hadoop 流?

Hadoop Streaming 是 Hadoop 發行版附帶的實用程序。它可用於執行大數據分析程序。Hadoop 流可以使用 Python、Java、PHP、Scala、Perl、UNIX 等語言執行。該實用程序允許我們使用任何可執行文件或腳本作為映射器和/或化簡器來創建和運行 Map/Reduce 作業。例如:

$HADOOP_HOME/bin/hadoop jar $HADOOP_HOME/hadoop-streaming.jar
-input myInputDirs
-輸出我的輸出目錄
-文件夾/垃圾箱/-減速器/bin/wc

參數說明:

Python MapReduce 代碼:

mapper.py
#!/usr/bin/python
import sys
#Word Count Example
# input comes from standard input STDIN
for line in sys.stdin:
line = line.strip() #remove leading and trailing whitespaces
words = line.split() #split the line into words and returns as a list
for word in words:
#write the results to standard output STDOUT
print'%s    %s' % (word,1) #Emit the word

reducer.py

#!/usr/bin/python
import sys
from operator import itemgetter
# using a dictionary to map words to their counts
current_word = None
current_count = 0
word = None
# input comes from STDIN
for line in sys.stdin:
line = line.strip()
word,count = line.split('   ',1)
try:
count = int(count)
except ValueError:
continue
if current_word == word:
current_count += count
else:
if current_word:
print '%s   %s' % (current_word, current_count)
current_count = count
current_word = word
if current_word == word:
print '%s   %s' % (current_word,current_count)

跑:

  • 創建一個包含以下內容的文件並將其命名為 word.txt。

貓鼠獅鹿虎獅象獅鹿

  • 將 mapper.py 和 reducer.py 腳本復制到上述文件所在的同一文件夾中。

  • 打開終端並找到文件所在的目錄。 命令:ls:列出目錄中的所有文件cd:更改目錄/文件夾

  • 查看文件的內容。

命令:cat file_name

> mapper.py 的內容

命令:cat mapper.py

>reducer.py 的內容

命令:cat reducer.py

我們可以在本地文件(例如:word.txt)上運行 mapper 和 reducer。為了在 Hadoop 分布式文件系統 (HDFS) 上運行 Map 和 Reduce,我們需要Hadoop Streaming jar。所以在我們在 HDFS 上運行腳本之前,讓我們在本地運行它們以確保它們工作正常。

>運行映射器

命令:cat word.txt | python mapper.py

>運行reducer.py

命令: cat word.txt | python mapper.py | sort -k1,1 | python reducer.py

我們可以看到映射器和減速器按預期工作,因此我們不會面臨任何進一步的問題。

在 Hadoop 上運行Python 代碼

在我們在 Hadoop 上運行 MapReduce 任務之前,將本地數據(word.txt)復制到 HDFS

> 示例:hdfs dfs -put source_directory hadoop_destination_directory

命令:hdfs dfs -put /home/edureka/MapReduce/word.txt /user/edureka

復制jar文件的路徑

基於jar版本的Hadoop Streaming jar路徑為:

/usr/lib/hadoop-2.2.X/share/hadoop/tools/lib/hadoop-streaming-2.2.X.jar

因此,在您的終端上找到 Hadoop Streaming jar 並復制路徑。

命令:

ls /usr/lib/hadoop-2.2.0/share/hadoop/tools/lib/hadoop-streaming-2.2.0.jar

運行 MapReduce 作業

命令:

hadoop jar /usr/lib/hadoop-2.2.0/share/hadoop/tools/lib/hadoop-streaming-2.2.0.jar -file /home/edureka/mapper.py -mapper mapper.py -file /home/ edureka/reducer.py -reducer reducer.py -input /user/edureka/word -output /user/edureka/Wordcount

Hadoop 為統計和信息提供了一個基本的 Web 界面。當 Hadoop 集群運行時,在瀏覽器中打開 http://localhost:50070。這是 Hadoop Web 界面的屏幕截圖。

現在瀏覽文件系統並找到生成的 wordcount 文件以查看輸出。下面是截圖。

我們可以使用這個命令在終端上看到輸出

命令:hadoop fs -cat /user/edureka/Wordcount/part-00000

您現在已經學會了如何使用 Hadoop Streaming 執行用 Python 編寫的 MapReduce 程序!

 

點擊關注,第一時間了解華為雲新鮮技術~


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM