Changes between Initial Version and Version 1 of III120526/Lab9


Ignore:
Timestamp:
May 27, 2012, 3:23:49 PM (12 years ago)
Author:
jazz
Comment:

--

Legend:

Unmodified
Added
Removed
Modified
  • III120526/Lab9

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