Changes between Initial Version and Version 1 of III120825/Lab9


Ignore:
Timestamp:
Aug 23, 2012, 11:58:30 PM (12 years ago)
Author:
jazz
Comment:

--

Legend:

Unmodified
Added
Removed
Modified
  • III120825/Lab9

    v1 v1  
     1[[PageOutline]]
     2
     3◢ <[wiki:III120825/Lab8 實作八]> | <[wiki:III120825 回課程大綱]> ▲ | <[wiki:III120825/Lab10 實作十]> ◣
     4
     5= 實作九 Lab 9 =
     6
     7{{{
     8#!html
     9<div style="text-align: center;"><big style="font-weight: bold;"><big>Hadoop Streaming 搭配不同程式語言練習<br/>Hadoop Streaming in different Language</big></big></div>
     10}}}
     11
     12{{{
     13#!text
     14以下練習,請連線至 hadoop.nchc.org.tw 操作。底下的 hXXXX 等於您的用戶名稱。
     15}}}
     16
     17== 搭配現存二進位執行檔 ==
     18== Existing Binary ==
     19
     20{{{
     21~$ hadoop fs -put /etc/hadoop/conf lab9_input
     22~$ hadoop jar hadoop-streaming.jar -input lab9_input -output lab9_out1 -mapper /bin/cat -reducer /usr/bin/wc
     23~$ hadoop fs -cat lab9_out1/part-00000
     24}}}
     25
     26== 搭配 Bash Shell Script ==
     27
     28{{{
     29~$ echo "sed -e \"s/ /\n/g\" | grep ." > streamingMapper.sh
     30~$ echo "uniq -c | awk '{print \$2 \"\t\" \$1}'" > streamingReducer.sh
     31~$ chmod a+x streamingMapper.sh
     32~$ chmod a+x streamingReducer.sh
     33~$ hadoop jar hadoop-streaming.jar -input lab9_input -output lab9_out2 -mapper streamingMapper.sh -reducer streamingReducer.sh -file streamingMapper.sh -file streamingReducer.sh
     34~$ hadoop fs -cat lab9_out2/part-00000
     35}}}
     36
     37== 搭配 PHP Script ==
     38
     39 * 編輯 mapper 的 php 程式
     40{{{
     41~$ cat > mapper.php << EOF
     42#!/usr/bin/php
     43<?php
     44
     45\$word2count = array();
     46
     47// 標準輸入為 STDIN (standard input)
     48while ((\$line = fgets(STDIN)) !== false) {
     49   // 移除小寫與空白
     50   \$line = strtolower(trim(\$line));
     51   // 將行拆解成各個字於 words 陣列中
     52   \$words = preg_split('/\W/', \$line, 0, PREG_SPLIT_NO_EMPTY);
     53   // 將字 +1
     54   foreach (\$words as \$word) {
     55       \$word2count[\$word] += 1;
     56   }
     57}
     58
     59// 將結果寫到 STDOUT (standard output)
     60foreach (\$word2count as \$word => \$count) {
     61   // 印出 [字 , "tab符號" ,  "數字" , "結束字元"]
     62   echo \$word, chr(9), \$count, PHP_EOL;
     63}
     64?>
     65EOF
     66}}}
     67 * 編輯 reduce 的 php 程式
     68{{{
     69~$ cat > reducer.php << EOF
     70#!/usr/bin/php
     71<?php
     72
     73\$word2count = array();
     74
     75// 輸入為 STDIN
     76while ((\$line = fgets(STDIN)) !== false) {
     77    // 移除多餘的空白
     78    \$line = trim(\$line);
     79    // 每一行的格式為 (單字 "tab" 數字) ,紀錄到(\$word, \$count)
     80    list(\$word, \$count) = explode(chr(9), \$line);
     81    // 轉換格式string -> int
     82    \$count = intval(\$count);
     83    // 加總
     84    if (\$count > 0) \$word2count[\$word] += \$count;
     85}
     86
     87// 此行不必要,但可讓output排列更完整
     88ksort(\$word2count);
     89
     90// 將結果寫到 STDOUT (standard output)
     91foreach (\$word2count as \$word => \$count) {
     92    echo \$word, chr(9), \$count, PHP_EOL;
     93}
     94?>
     95EOF
     96}}}
     97 * 修改執行權限
     98{{{
     99~$ chmod a+x *.php
     100}}}
     101 * 測試是否能運作
     102{{{
     103~$ echo "i love hadoop, hadoop love u" | ./mapper.php | ./reducer.php
     104}}}
     105 * 開始執行
     106{{{
     107~$ hadoop jar hadoop-streaming.jar -mapper mapper.php -reducer reducer.php -input lab9_input -output lab9_out3 -file mapper.php -file reducer.php
     108}}}
     109 * 檢查結果
     110{{{
     111~$ hadoop fs -cat lab9_out3/part-00000
     112}}}