hadoop thrift:php通過thrift擷取hadoop資源
來源:互聯網
上載者:User
php可以通過thrift串連hbase,同樣php可以通過thrift讀取hadoop資源(hdfs資源)。
準備:
php需要thrift的libary
packages:hadoop-0.20.2\src\contrib\thriftfs\gen-php
源碼:
<?php
$globals['thrift_root'] = rootpath . '/lib/thrift';
require_once($globals['thrift_root'].'/thrift.php');
require_once($globals['thrift_root'].'/transport/tsocket.php');
require_once($globals['thrift_root'].'/transport/tbufferedtransport.php');
require_once($globals['thrift_root'].'/protocol/tbinaryprotocol.php');
require_once($globals["thrift_root"] . "/packages/hadoopfs/thrifthadoopfilesystem.php");
$hadoop_socket = new tsocket("localhost", 59256);
$hadoop_socket -> setsendtimeout(10000); // ten seconds
$hadoop_socket -> setrecvtimeout(20000); // twenty seconds
$hadoop_transport = new tbufferedtransport($hadoop_socket);
$hadoop_protocol = new tbinaryprotocol($hadoop_transport);
$hadoopclient = new thrifthadoopfilesystemclient($hadoop_protocol);
$hadoop_transport -> open();
try {
// create directory
$dirpathname = new hadoopfs_pathname(array("pathname" => "/user/root/hadoop"));
if($hadoopclient -> exists($dirpathname) == true) {
echo $dirpathname -> pathname . " exists.\n";
} else {
$result = $hadoopclient -> mkdirs($dirpathname);
}
// put file
$filepathname = new hadoopfs_pathname(array("pathname" => $dirpathname -> pathname . "/hello.txt"));
$localfile = fopen("hello.txt", "rb");
$hdfsfile = $hadoopclient -> create($filepathname);
while(true) {
$data = fread($localfile, 1024);
if(strlen($data) == 0)
break;
$hadoopclient -> write($hdfsfile, $data);
}
$hadoopclient -> close($hdfsfile);
fclose($localfile);
// get file
echo "read file:\n";
print_r($filepathname);
$data = "";
$hdfsfile = $hadoopclient -> open($filepathname);
print_r($hdfsfile);
while(true) {
$data = $hadoopclient -> read($hdfsfile, 0, 1024);
if(strlen($data) == 0)
break;
print $data;
}
$hadoopclient -> close($hdfsfile);
echo "liststatus:\n";
$result = $hadoopclient -> liststatus($dirpathname);
print_r($result);
foreach($result as $key => $value) {
if($value -> isdir == "1")
print "dir\t";
else
print "file\t";
print $value -> block_replication . "\t" . $value -> length . "\t" . $value -> modification_time . "\t" . $value -> permission . "\t" . $value -> owner . "\t" . $value -> group . "\t" . $value -> path . "\n";
}
$hadoop_transport -> close();
} catch(exception $e) {
print_r($e);
}
?>
啟動hadoop的thrift
hadoop-0.20.2\src\contrib\thriftfs\scripts\start_thrift_server.sh 59256
problem one:
在系統目錄建立檔案,而不是在hadoop目錄中建立檔案
原因:
thrift啟動時載入預設的設定檔
解決方案:
修改start_thrift_server.sh檔案
top=/usr/local/hadoop-0.20.2
classpath=$classpath:$top/conf
problem two:
java.lang.nullpointerexception
at org.apache.hadoop.thriftfs.hadoopthriftserver$hadoopthrifthandler.write(hadoopthriftserver.java:282)
at org.apache.hadoop.thriftfs.api.thrifthadoopfilesystem$processor$write.process(unknown source)
at org.apache.hadoop.thriftfs.api.thrifthadoopfilesystem$processor.process(unknown source)
at com.facebook.thrift.server.tthreadpoolserver$workerprocess.run(unknown source)
at java.util.concurrent.threadpoolexecutor$worker.runtask(threadpoolexecutor.java:886)
at java.util.concurrent.threadpoolexecutor$worker.run(threadpoolexecutor.java:908)
at java.lang.thread.run(thread.java:662)
原因:
java返回的map hash id為long類型,而php(32位)無法儲存long類型的資料,導致轉換成float資料後丟失精度。
private long nextid = new random().nextlong();
java返回資料:4207488029786584864 本文連結http://www.cxybl.com/html/wlbc/Php/20120607/28513.html