用PHP编写Hadoop的MapReduce程序

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函数

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('/\W/', $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;  
}  
?>

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

             hello    1
             world  1″

这样的形式输出出来。

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

PHP作为可执行程序

第一行的

#!/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文件,写入如下代码:

#!/usr/local/php/bin/php  
<?php  
$word2count = array();  
// input comes from STDIN  
while (($line = fgets(STDIN)) !== false) {  
    // remove leading and trailing whitespace  
    $line = trim($line);  
    // parse the input we got from mapper.php  
    list($word, $count) = explode(chr(9), $line);  
    // convert count (currently a string) to int  
    $count = intval($count);  
    // sum counts  
    if ($count > 0) $word2count[$word] += $count;  
}  
// sort the words lexigraphically  
//  
// this set is NOT required, we just do it so that our  
// final output will look more like the official Hadoop  
// word count examples  
ksort($word2count);  
// write the results to STDOUT (standard output)  
foreach ($word2count as $word => $count) {  
    echo $word, chr(9), $count, PHP_EOL;  
}  
?>

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

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

转自:http://blog.csdn.net/hguisu/article/details/7263746

原创文章,作者:s19930811,如若转载,请注明出处:http://www.178linux.com/3084

(0)
s19930811s19930811
上一篇 2015-04-13
下一篇 2015-04-13

相关推荐

  • Linux Zabbix监控

                           Linux Zabbix监控 zabbix监控系统:    zabbix是基于WEB界面的提供分布式系统监视以及网络监视功能的企业级的开源解决方案。zab…

    Linux干货 2016-11-19
  • Ansible初识

    Ansible概念 ansible是自动化运维工具,基于Python开发,集合了众多运维工具(puppet、cfengine、chef、func、fabric)的优点,实现了批量系统配置、批量程序部署、批量运行命令等功能。ansible是基于模块工作的,本身没有批量部署的能力。真正具有批量部署的是ansible所运行的模块,ansible只是提供一种框架。说…

    2017-07-08
  • 联通电信双链路内网VRRP+BFD&&PPTP+MYSQL+FreeRadius实现IDC堡垒机连接IDC机房

    一、具体需求与实现 1、多wan:两条宽带接入链路,使用VRRP+BFD技术,实现链路冗余; 2、IDC机房远程管理和登录限制:使用PPTP+freeRadius+mysql实现VPN,限制指定网段或IP进行拨号认证登录IDC机房,并编写脚本检查非法用户登录IDC机房服务器情况; 3、内网VLAN划分:利于安全管理、IP分流和带宽限制; 4、内网监控:使用N…

    Linux干货 2016-08-22
  • linux网络配置

    主要内容: ip地址以及子网划分 路由基本概念 网络配置工具:ifconfig,ip,netstat使用 网卡配置文件及修改 IP地址: 它们可唯一标识 IP 网络中的每台设备 v  每台主机(计算机、网络设备、外围设备)必须具有唯 一的地址 v IP地址由两部分组成:   &n…

    Linux干货 2016-09-07
  • Lvm基本应用,扩展及缩减实现

    什么是LVM LVM是Logical Volume Manager的简写。其是Linux环境下对磁盘分区进行管理的一种机制。LVM由Heinz Mauelshagen在Linux 2.4内核上实现。其主要作用是在不损坏磁盘数据的情况下对磁盘空间进行增加,缩减。LVM的做法是将几块物理硬盘通过软件的方式组合成一块空间相对大的Volume Group,简称VG,…

    Linux干货 2016-09-08
  • 创建CA、申请证书和吊销证书详解

    创建CA和申请证书、吊销证书 搭建工具:openssl 服务端:centos7 客户端:centos6 配置实验环境: 需要两台虚拟机为服务端、客户端提供运行环境,装载openssl工具,添加必要文件;通过查看openssl的配置文件/etc/pki/tls/openssl.cnf(图一),对比服务端的/etc/pki/CA文件内容(图二),如果第一次搭建服…

    2017-04-11