{{{ #!html
Hadoop Streaming
}}} [[PageOutline]] Hadoop streaming是Hadoop的一個工具, 它幫助用戶創建和運行一類特殊的map/reduce作業, 這些特殊的map/reduce作業是由一些可執行文件或腳本文件充當mapper或者reducer * 用法: {{{ $ bin/hadoop jar contrib/streaming/hadoop-0.20.2-streaming.jar \ -input $INPUT -output $OUTPUT -mapper $MAPPER -reducer $REDUCER }}} * 格式分析: || bin/hadoop || 呼叫使用hadoop程式 || || || jar contrib/streaming/hadoop-0.20.2-streaming.jar || 使用streaming這個功能 || ps:預設此jar檔放在 contrib/streaming/ 內 || || -input $INPUT || 設定hdfs上的輸入資料夾 || ps:需先上傳資料到hdfs 上 || || -output $OUTPUT || 設定hdfs上的輸出資料夾|| ps:在hdfs 上的 output資料夾不可重複|| || -mapper $MAPPER || 設定mapper程式 || ps:要給完整路徑 || || -reducer $REDUCER || 設定reducer程式 || ps:要給完整路徑 || ------------ = Stream Example 1 : 用 Shell = 此範例以 cat 當mapper , wc 作 reducer * 運算方法如下 {{{ $ bin/hadoop jar contrib/streaming/hadoop-0.20.2-streaming.jar \ -input lab4_input -output stream-out1 -mapper /bin/cat -reducer /usr/bin/wc }}} * 輸出的結果為: {{{ $ bin/hadoop fs -cat stream-out1/part-00000 }}} || 行 || 字數 || 字元數 || || 2 || 15 || 80 || ------------ = Stream Example 2 : 用 PHP = [http://www.hadoop.tw/2008/09/php-hadoop.html 參考自 Hadoop Taiwan User Group ] * 安裝php的執行方法 {{{ $ cd /opt/hadoop/ $ sudo apt-get install php5-cli }}} * 編輯 mapper 的 php 程式 {{{ $ gedit mapper.php }}} 內容為 : {{{ #!php #!/usr/bin/php $count) { // 印出 [字 , "tab符號" , "數字" , "結束字元"] echo $word, chr(9), $count, PHP_EOL; } ?> }}} * 編輯 reduce 的php程式 {{{ $ gedit reducer.php }}} 內容為: {{{ #!php #!/usr/bin/php int $count = intval($count); // 加總 if ($count > 0) $word2count[$word] += $count; } // 此行不必要,但可讓output排列更完整 ksort($word2count); // 將結果寫到 STDOUT (standard output) foreach ($word2count as $word => $count) { echo $word, chr(9), $count, PHP_EOL; } ?> }}} * 修改執行權限 {{{ $ chmod 755 *.php }}} * 測試是否能運作 {{{ $ echo "i love hadoop, hadoop love u" | ./mapper.php | ./reducer.php }}} * 開始執行 {{{ $ bin/hadoop jar contrib/streaming/hadoop-0.20.2-streaming.jar \ -mapper /opt/hadoop/mapper.php -reducer /opt/hadoop/reducer.php -input lab4_input -output stream_out2 }}} * 檢查結果 {{{ $ bin/hadoop dfs -cat stream_out2/part-00000 }}} = Python 實做 = * [http://www.cs.brandeis.edu/~cs147a/lab/hadoop-example/ Hadoop Example Program] from brandeis University