php通过shell调用Hadoop的方法

Posted cxchanpin

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了php通过shell调用Hadoop的方法相关的知识,希望对你有一定的参考价值。

1.php代码(index.php)

<!DOCTYPE html>
<html>
 
<!-- <style> body{background-color:red} </style> -->
<!-- <style type="text/css"> -->
<!--  body { background:url(渐变绿.jpeg); -->
<!--       width:100%; -->
<!--       height:100%; } </style> -->
<body>
<?php
header(‘Content-type: text/html; charset=UTF8‘);
#phpinfo();
$jiaoben = $_GET[‘jiaoben‘];
echo "运行Hadoop的log信息:";
$ret= system("sh /home/work/$jiaoben", $status);
if($status != 0) { 
echo ‘ failed, status=‘ . "$status\n"; 
} else {
	echo $ret;
        echo "<br><br>";
}

?>
</body>
</html>


2.shell代码(hadoop.sh)

#!/bin/bash
echo "wordCount test start"
rm -rf ~/file_20141024
mkdir ~/file_20141024
cd ~/file_20141024
echo "Hello World" > file1.txt
echo "Hello China Beijing" > file2.txt
cd /home/work/hadoop/bin 
./hadoop dfs -rmr input_20141024
./hadoop dfs -rmr output_20141024
./hadoop fs -mkdir input_20141024
#hadoop fs -chomod -R 777 /input_20141024
./hadoop fs -put ~/file_20141024/file*.txt input_20141024
./hadoop jar /home/work/hadoop/hadoop-examples-1.1.2.jar wordcount input_20141024 output_20141024
./hadoop fs -ls output_20141024
./hadoop fs -cat output_20141024/part-r-00000
echo "wordCount test end"


3. 訪问http://ip:8888/index.php?

jiaoben=hadoop.sh

查询结果:

/user/work/output_20141024/part-r-00000 Beijing 1 China 1 Hello 2 World 1





以上是关于php通过shell调用Hadoop的方法的主要内容,如果未能解决你的问题,请参考以下文章

php调用shell的方法技巧

PHP链式操作通过call和callstatic魔术方法的实现,以及phpstorm通过注释去追踪function

PHP链式操作通过call和callstatic魔术方法的实现,以及phpstorm通过注释去追踪function

php 一个自定义的try..catch包装器代码片段,用于执行模型函数,使其成为一个单行函数调用

shell高级用法--函数

如何从需要 SUDO 的 php 调用 shell 脚本?