欧美一区二区三区老妇人-欧美做爰猛烈大尺度电-99久久夜色精品国产亚洲a-亚洲福利视频一区二区

用PHP編寫Hadoop的MapReduce程序-創(chuàng)新互聯(lián)

來(lái)源:http://blog.csdn.net/hguisu/article/details/7263746用PHP編寫Hadoop的Map
Reduce程序

Hadoop流

雖然Hadoop是用java寫的,但是Hadoop提供了Hadoop流,Hadoop流提供一個(gè)API, 允許用戶使用任何語(yǔ)言編寫map函數(shù)和reduce函數(shù).
Hadoop流動(dòng)關(guān)鍵是,它使用UNIX標(biāo)準(zhǔn)流作為程序與Hadoop之間的接口。因此,任何程序只要可以從標(biāo)準(zhǔn)輸入流中讀取數(shù)據(jù),并且可以把數(shù)據(jù)寫入標(biāo)準(zhǔn)輸出流中,那么就可以通過(guò)Hadoop流使用任何語(yǔ)言編寫MapReduce程序的map函數(shù)和reduce函數(shù)。
例如: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根目錄下是沒(méi)有hadoop-streaming.jar的,因?yàn)閟treaming是一個(gè)contrib,所以要去contrib下面找,以hadoop-0.20.2為例,它在這里:
-input:指明輸入hdfs文件的路徑
-output:指明輸出hdfs文件的路徑
-mapper:指明map函數(shù)
-reducer:指明reduce函數(shù)

創(chuàng)新互聯(lián)公司主營(yíng)隴縣網(wǎng)站建設(shè)的網(wǎng)絡(luò)公司,主營(yíng)網(wǎng)站建設(shè)方案,成都app開(kāi)發(fā),隴縣h5微信小程序開(kāi)發(fā)搭建,隴縣網(wǎng)站營(yíng)銷推廣歡迎隴縣等地區(qū)企業(yè)咨詢

mapper函數(shù)

mapper.php文件,寫入如下代碼:

  1. #!/usr/local/php/bin/php
  2. <?php
  3. $word2count = array();
  4. // input comes from STDIN (standard input)
  5. // You can this code :$stdin = fopen(“php://stdin”, “r”);
  6. while (($line = fgets(STDIN)) !== false) {
  7.     // remove leading and trailing whitespace and lowercase
  8.     $line = strtolower(trim($line));
  9.     // split the line into words while removing any empty string
  10.     $words = preg_split('/W/', $line, 0, PREG_SPLIT_NO_EMPTY);
  11.     // increase counters
  12.     foreach ($words as $word) {
  13.         $word2count[$word] += 1;
  14.     }
  15. }
  16. // write the results to STDOUT (standard output)
  17. // what we output here will be the input for the
  18. // Reduce step, i.e. the input for reducer.py
  19. foreach ($word2count as $word => $count) {
  20.     // tab-delimited
  21.     echo $word, chr(9), $count, PHP_EOL;
  22. }
  23. ?>

這段代碼的大致意思是:把輸入的每行文本中的單詞找出來(lái),并以”

            hello   1
           world 1″

這樣的形式輸出出來(lái)。

和之前寫的PHP基本沒(méi)有什么不同,對(duì)吧,可能稍微讓你感到陌生有兩個(gè)地方:

PHP作為可執(zhí)行程序

第一行的

  1. #!/usr/local/php/bin/php
告訴linux,要用#!/usr/local/php/bin/php這個(gè)程序作為以下代碼的解釋器。寫過(guò)linux shell的人應(yīng)該很熟悉這種寫法了,每個(gè)shell腳本的第一行都是這樣: #!/bin/bash, #!/usr/bin/python

有了這一行,保存好這個(gè)文件以后,就可以像這樣直接把mapper.php當(dāng)作cat, grep一樣的命令執(zhí)行了:./mapper.php

使用stdin接收輸入

PHP支持多種參數(shù)傳入的方法,大家最熟悉的應(yīng)該是從$_GET, $_POST超全局變量里面取通過(guò)Web傳遞的參數(shù),次之是從$_SERVER['argv']里取通過(guò)命令行傳入的參數(shù),這里,采用的是標(biāo)準(zhǔn)輸入stdin

它的使用效果是:

在linux控制臺(tái)輸入 ./mapper.php

mapper.php運(yùn)行,控制臺(tái)進(jìn)入等候用戶鍵盤輸入狀態(tài)

用戶通過(guò)鍵盤輸入文本

用戶按下Ctrl + D終止輸入,mapper.php開(kāi)始執(zhí)行真正的業(yè)務(wù)邏輯,并將執(zhí)行結(jié)果輸出

那么stdout在哪呢?print本身已經(jīng)就是stdout啦,跟我們以前寫web程序和CLI腳本沒(méi)有任何不同。

reducer函數(shù)

創(chuàng)建reducer.php文件,寫入如下代碼:

  1. #!/usr/local/php/bin/php
  2. <?php
  3. $word2count = array();
  4. // input comes from STDIN
  5. while (($line = fgets(STDIN)) !== false) {
  6.     // remove leading and trailing whitespace
  7.     $line = trim($line);
  8.     // parse the input we got from mapper.php
  9.     list($word, $count) = explode(chr(9), $line);
  10.     // convert count (currently a string) to int
  11.     $count = intval($count);
  12.     // sum counts
  13.     if ($count > 0) $word2count[$word] += $count;
  14. }
  15. // sort the words lexigraphically
  16. //
  17. // this set is NOT required, we just do it so that our
  18. // final output will look more like the official Hadoop
  19. // word count examples
  20. ksort($word2count);
  21. // write the results to STDOUT (standard output)
  22. foreach ($word2count as $word => $count) {
  23.     echo $word, chr(9), $count, PHP_EOL;
  24. }
  25. ?>

這段代碼的大意是統(tǒng)計(jì)每個(gè)單詞出現(xiàn)了多少次數(shù),并以”

hello  2

world  1″

這樣的形式輸出

用Hadoop來(lái)運(yùn)行

把文件放入 Hadoop 的 DFS 中:

bin/hadoop dfs -put test.log test
執(zhí)行 php 程序處理這些文本(以Streaming方式執(zhí)行PHP mapreduce程序:):

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 out

注意:

1) input和output目錄是在hdfs上的路徑

2) mapper和reducer是在本地機(jī)器的路徑,一定要寫絕對(duì)路徑,不要寫相對(duì)路徑,以免到時(shí)候hadoop報(bào)錯(cuò)說(shuō)找不到mapreduce程序

3 ) mapper.php 和 reducer.php 必須復(fù)制到所有 DataNode 服務(wù)器上的相同路徑下, 所有的服務(wù)器都已經(jīng)安裝php.且安裝路徑一樣.

查看結(jié)果

bin/hadoop d fs -cat /tmp/out/part-00000

分享標(biāo)題:用PHP編寫Hadoop的MapReduce程序-創(chuàng)新互聯(lián)
瀏覽地址:http://chinadenli.net/article48/cophep.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站設(shè)計(jì)公司電子商務(wù)企業(yè)網(wǎng)站制作外貿(mào)建站網(wǎng)站內(nèi)鏈自適應(yīng)網(wǎng)站

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)

成都定制網(wǎng)站網(wǎng)頁(yè)設(shè)計(jì)