Changes between Initial Version and Version 1 of Streaming_018


Ignore:
Timestamp:
Apr 26, 2010, 6:51:55 PM (14 years ago)
Author:
waue
Comment:

--

Legend:

Unmodified
Added
Removed
Modified
  • Streaming_018

    v1 v1  
     1{{{
     2#!html
     3<div style="text-align: center;"><big
     4 style="font-weight: bold;"><big><big>Hadoop Streaming</big></big></big></div>
     5}}}
     6
     7[[PageOutline]]
     8
     9Hadoop streaming是Hadoop的一個工具, 它幫助用戶創建和運行一類特殊的map/reduce作業, 這些特殊的map/reduce作業是由一些可執行文件或腳本文件充當mapper或者reducer
     10
     11 * 用法:
     12
     13{{{
     14$ bin/hadoop jar contrib/streaming/hadoop-0.18.3-streaming.jar \
     15-input $INPUT -output $OUTPUT -mapper $MAPPER -reducer $REDUCER
     16}}}
     17
     18 * 格式分析:
     19
     20 || bin/hadoop  || 呼叫使用hadoop程式 ||  ||
     21 || jar contrib/streaming/hadoop-0.18.3-streaming.jar  || 使用streaming這個功能 || ps:預設此jar檔放在 contrib/streaming/ 內 ||
     22 || -input $INPUT  || 設定hdfs上的輸入資料夾 || ps:需先上傳資料到hdfs 上 ||
     23 || -output $OUTPUT  ||  設定hdfs上的輸出資料夾||  ps:在hdfs 上的 output資料夾不可重複||
     24 || -mapper $MAPPER  || 設定mapper程式 || ps:要給完整路徑  ||
     25 || -reducer $REDUCER || 設定reducer程式 || ps:要給完整路徑 ||
     26
     27------------
     28
     29 = Stream Example 1 : 用 Shell =
     30
     31此範例以 cat 當mapper , wc 作 reducer
     32
     33 * 運算方法如下
     34
     35{{{
     36$ bin/hadoop jar contrib/streaming/hadoop-0.18.3-streaming.jar \
     37-input lab4_input -output stream-out1 -mapper /bin/cat -reducer /usr/bin/wc
     38}}}
     39
     40 * 輸出的結果為:
     41
     42{{{
     43$ bin/hadoop fs -cat stream-out1/part-00000
     44}}}
     45
     46||     行    ||   字數    ||     字元數     ||
     47|| 2 || 15 || 80 ||
     48
     49------------
     50
     51 = Stream Example 2 : 用 PHP =
     52
     53[http://www.hadoop.tw/2008/09/php-hadoop.html 參考自 Hadoop Taiwan User Group ]
     54
     55 * 安裝php的執行方法
     56{{{
     57$ cd /opt/hadoop/
     58$ sudo apt-get install php5-cli
     59}}}
     60
     61 * 編輯 mapper 的 php 程式
     62{{{
     63$ gedit mapper.php
     64}}}
     65
     66內容為 :
     67
     68{{{
     69#!php
     70#!/usr/bin/php
     71<?php
     72
     73$word2count = array();
     74
     75// 標準輸入為 STDIN (standard input)
     76while (($line = fgets(STDIN)) !== false) {
     77   // 移除小寫與空白
     78   $line = strtolower(trim($line));
     79   // 將行拆解成各個字於words 陣列中
     80   $words = preg_split('/\W/', $line, 0, PREG_SPLIT_NO_EMPTY);
     81   // 將字+1
     82   foreach ($words as $word) {
     83       $word2count[$word] += 1;
     84   }
     85}
     86
     87// 將結果寫到 STDOUT (standard output)
     88foreach ($word2count as $word => $count) {
     89   // 印出 [字 , "tab符號" ,  "數字" , "結束字元"]
     90   echo $word, chr(9), $count, PHP_EOL;
     91}
     92?>
     93}}}
     94
     95 * 編輯 reduce 的php程式
     96{{{
     97$ gedit reducer.php
     98}}}
     99
     100內容為:
     101
     102{{{
     103#!php
     104#!/usr/bin/php
     105<?php
     106
     107$word2count = array();
     108
     109// 輸入為 STDIN
     110while (($line = fgets(STDIN)) !== false) {
     111    // 移除多餘的空白
     112    $line = trim($line);
     113    // 每一行的格式為 (單字 "tab" 數字) ,紀錄到($word, $count)
     114    list($word, $count) = explode(chr(9), $line);
     115    // 轉換格式string -> int
     116    $count = intval($count);
     117    // 加總
     118    if ($count > 0) $word2count[$word] += $count;
     119}
     120
     121// 此行不必要,但可讓output排列更完整
     122ksort($word2count);
     123
     124// 將結果寫到 STDOUT (standard output)
     125foreach ($word2count as $word => $count) {
     126    echo $word, chr(9), $count, PHP_EOL;
     127}
     128
     129?>
     130}}}
     131
     132 * 修改執行權限
     133
     134{{{
     135$ chmod 755 *.php
     136}}}
     137
     138 * 測試是否能運作
     139
     140{{{
     141$ echo "i love hadoop, hadoop love u" | ./mapper.php | ./reducer.php
     142}}}
     143
     144 * 開始執行
     145
     146{{{
     147$ bin/hadoop jar contrib/streaming/hadoop-0.18.3-streaming.jar  \
     148-mapper /opt/hadoop/mapper.php -reducer /opt/hadoop/reducer.php -input lab4_input -output stream_out2
     149}}}
     150
     151 * 檢查結果
     152
     153{{{
     154$ bin/hadoop dfs -cat stream_out2/part-00000
     155}}}
     156
     157 = Python 實做 =
     158 * [http://www.cs.brandeis.edu/~cs147a/lab/hadoop-example/ Hadoop Example Program] from brandeis University