37 | | |
38 | | == 搭配 PHP Script == |
39 | | |
40 | | * 編輯 mapper 的 php 程式 |
41 | | {{{ |
42 | | ~$ cat > mapper.php << EOF |
43 | | #!/usr/bin/php |
44 | | <?php |
45 | | |
46 | | \$word2count = array(); |
47 | | |
48 | | // 標準輸入為 STDIN (standard input) |
49 | | while ((\$line = fgets(STDIN)) !== false) { |
50 | | // 移除小寫與空白 |
51 | | \$line = strtolower(trim(\$line)); |
52 | | // 將行拆解成各個字於 words 陣列中 |
53 | | \$words = preg_split('/\W/', \$line, 0, PREG_SPLIT_NO_EMPTY); |
54 | | // 將字 +1 |
55 | | foreach (\$words as \$word) { |
56 | | \$word2count[\$word] += 1; |
57 | | } |
58 | | } |
59 | | |
60 | | // 將結果寫到 STDOUT (standard output) |
61 | | foreach (\$word2count as \$word => \$count) { |
62 | | // 印出 [字 , "tab符號" , "數字" , "結束字元"] |
63 | | echo \$word, chr(9), \$count, PHP_EOL; |
64 | | } |
65 | | ?> |
66 | | EOF |
67 | | }}} |
68 | | * 編輯 reduce 的 php 程式 |
69 | | {{{ |
70 | | ~$ cat > reducer.php << EOF |
71 | | #!/usr/bin/php |
72 | | <?php |
73 | | |
74 | | \$word2count = array(); |
75 | | |
76 | | // 輸入為 STDIN |
77 | | while ((\$line = fgets(STDIN)) !== false) { |
78 | | // 移除多餘的空白 |
79 | | \$line = trim(\$line); |
80 | | // 每一行的格式為 (單字 "tab" 數字) ,紀錄到(\$word, \$count) |
81 | | list(\$word, \$count) = explode(chr(9), \$line); |
82 | | // 轉換格式string -> int |
83 | | \$count = intval(\$count); |
84 | | // 加總 |
85 | | if (\$count > 0) \$word2count[\$word] += \$count; |
86 | | } |
87 | | |
88 | | // 此行不必要,但可讓output排列更完整 |
89 | | ksort(\$word2count); |
90 | | |
91 | | // 將結果寫到 STDOUT (standard output) |
92 | | foreach (\$word2count as \$word => \$count) { |
93 | | echo \$word, chr(9), \$count, PHP_EOL; |
94 | | } |
95 | | ?> |
96 | | EOF |
97 | | }}} |
98 | | * 修改執行權限 |
99 | | {{{ |
100 | | ~$ chmod a+x *.php |
101 | | }}} |
102 | | * 測試是否能運作 |
103 | | {{{ |
104 | | ~$ echo "i love hadoop, hadoop love u" | ./mapper.php | ./reducer.php |
105 | | }}} |
106 | | * 開始執行 |
107 | | {{{ |
108 | | ~$ hadoop jar hadoop-streaming.jar -mapper mapper.php -reducer reducer.php -input lab9_input -output lab9_out3 -file mapper.php -file reducer.php |
109 | | }}} |
110 | | * 檢查結果 |
111 | | {{{ |
112 | | ~$ hadoop fs -cat lab9_out3/part-00000 |
113 | | }}} |