网创优客建站品牌官网
为成都网站建设公司企业提供高品质网站建设
热线:028-86922220
成都专业网站建设公司

定制建站费用3500元

符合中小企业对网站设计、功能常规化式的企业展示型网站建设

成都品牌网站建设

品牌网站建设费用6000元

本套餐主要针对企业品牌型网站、中高端设计、前端互动体验...

成都商城网站建设

商城网站建设费用8000元

商城网站建设因基本功能的需求不同费用上面也有很大的差别...

成都微信网站建设

手机微信网站建站3000元

手机微信网站开发、微信官网、微信商城网站...

建站知识

当前位置:首页 > 建站知识

用PHP编写Hadoop的MapReduce程序-创新互联

来源:http://blog.csdn.net/hguisu/article/details/7263746用PHP编写Hadoop的Map
Reduce程序

Hadoop流

虽然Hadoop是用java写的,但是Hadoop提供了Hadoop流,Hadoop流提供一个API, 允许用户使用任何语言编写map函数和reduce函数.
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函数

创新互联公司主营陇县网站建设的网络公司,主营网站建设方案,成都app开发,陇县h5微信小程序开发搭建,陇县网站营销推广欢迎陇县等地区企业咨询

mapper函数

mapper.php文件,写入如下代码:

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

这段代码的大致意思是:把输入的每行文本中的单词找出来,并以”

            hello   1
           world 1″

这样的形式输出出来。

和之前写的PHP基本没有什么不同,对吧,可能稍微让你感到陌生有两个地方:

PHP作为可执行程序

第一行的

  1. #!/usr/local/php/bin/php
告诉linux,要用#!/usr/local/php/bin/php这个程序作为以下代码的解释器。写过linux shell的人应该很熟悉这种写法了,每个shell脚本的第一行都是这样: #!/bin/bash, #!/usr/bin/python

有了这一行,保存好这个文件以后,就可以像这样直接把mapper.php当作cat, grep一样的命令执行了:./mapper.php

使用stdin接收输入

PHP支持多种参数传入的方法,大家最熟悉的应该是从$_GET, $_POST超全局变量里面取通过Web传递的参数,次之是从$_SERVER['argv']里取通过命令行传入的参数,这里,采用的是标准输入stdin

它的使用效果是:

在linux控制台输入 ./mapper.php

mapper.php运行,控制台进入等候用户键盘输入状态

用户通过键盘输入文本

用户按下Ctrl + D终止输入,mapper.php开始执行真正的业务逻辑,并将执行结果输出

那么stdout在哪呢?print本身已经就是stdout啦,跟我们以前写web程序和CLI脚本没有任何不同。

reducer函数

创建reducer.php文件,写入如下代码:

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

这段代码的大意是统计每个单词出现了多少次数,并以”

hello  2

world  1″

这样的形式输出

用Hadoop来运行

把文件放入 Hadoop 的 DFS 中:

bin/hadoop dfs -put test.log test
执行 php 程序处理这些文本(以Streaming方式执行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是在本地机器的路径,一定要写绝对路径,不要写相对路径,以免到时候hadoop报错说找不到mapreduce程序

3 ) mapper.php 和 reducer.php 必须复制到所有 DataNode 服务器上的相同路径下, 所有的服务器都已经安装php.且安装路径一样.

查看结果

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


本文题目:用PHP编写Hadoop的MapReduce程序-创新互联
网站路径:http://bjjierui.cn/article/cophep.html

其他资讯