首頁  >  文章  >  後端開發  >  php能用hadoop嗎

php能用hadoop嗎

(*-*)浩
(*-*)浩原創
2019-10-14 15:07:164242瀏覽

Hadoop是一個由Apache基金會所開發的分散式系統基礎架構,使用者可以在不了解分散式底層細節的情況下,開發分散式程式。充分利用叢集的威力進行高速運算和儲存。

php能用hadoop嗎

雖然Hadoop是用java寫的,但Hadoop提供了Hadoop串流,Hadoop串流提供一個API,,允許使用者使用任何語言編寫map函數和reduce函數。   (建議學習:PHP影片教學

Hadoop流動關鍵是,它使用UNIX標準串流作為程式與Hadoop之間的介面。因此,任何程式只要可以從標準輸入流中讀取數據,並且可以把數據寫入標準輸出流中,那麼就可以透過Hadoop流使用任何語言來編寫MapReduce程式的map函數和reduce函數。

例如:

bin/hadoop jar contrib/streaming/hadoop-streaming-0.20.203.0.jar -mapper /usr/local/hadoop/mapper.php -reducer /usr/local/hadoop/reducer.php -input test/* -output out4

Hadoop串流引入的套件:hadoop-streaming-0.20.203.0.jar,Hadoop根目錄下是沒有hadoop-streaming.jar的,因為streaming是一個contrib,所以要去contrib下面找,以hadoop-0.20.2為例,它在這裡:

-input:指明輸入hdfs檔案的路徑

-output:指明輸出hdfs檔案的路徑

-mapper:指明map函數

-reducer:指明reduce函數

#mapper函數

##mapper.php文件,寫入如下程式碼:

#!/usr/local/php/bin/php
<?php
$word2count = array();
// input comes from STDIN (standard input)
// You can this code :$stdin = fopen(“php://stdin”, “r”);
while (($line = fgets(STDIN)) !== false) {
    // remove leading and trailing whitespace and lowercase
    $line = strtolower(trim($line));
    // split the line into words while removing any empty string
    $words = preg_split(&#39;/\W/&#39;, $line, 0, PREG_SPLIT_NO_EMPTY);
    // increase counters
    foreach ($words as $word) {
        $word2count[$word] += 1;
    }
}
// write the results to STDOUT (standard output)
// what we output here will be the input for the
// Reduce step, i.e. the input for reducer.py
foreach ($word2count as $word => $count) {
    // tab-delimited
    echo $word, chr(9), $count, PHP_EOL;
}
?>

以上是php能用hadoop嗎的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn