php hbase thrift,PHP使用Thrift操作Hbase
系統架構圖
HBase 啟動 Thrift服務
hbase啟動thrift服務
// 進入安裝的hbase bin目錄下
// 執(zhí)行
hbase-daemon.sh start thrift2
需要注意的是,這里啟動的是thrift2服務,如果需要啟動thrift服務只需要將thrift2改為thrift就可以了,具體thrift和thrift2之間的區(qū)別可以參考以下文章hbase的thrift接口
生成 php所需hbase相關php類
首先到appache官網上下載thrift windows 可執(zhí)行程序 thrift-0.9.0.exe 和thrift-0.9.0.tar.gz 兩個文件 下載地址
從linux環(huán)境中的Hbase安裝目錄里獲取hbase.thrift文件,這個文件用來生成相關php類
// thrift
/opt/soft/hbase/hbase-thrift/src/main/resources/org/apache/hadoop/hbase/thrift/Hbase.thrift
// thrift2
/opt/soft/hbase/hbase-thrift/src/main/resources/org/apache/hadoop/hbase/thrift2/hbase.thrift
這里/opt/soft/hbase為hbase安裝目錄
將上面的hbase.thrift文件移到windows下,放到剛剛下載好的thrift-0.9.0.exe目錄下,執(zhí)行
// 生成hbase必要的php相關類
thrift-0.9.0.exe -gen php hbase.thrift
可以看到目錄下多出了gen-php目錄,gen-php目錄下包含THBaseService.php和Types.php兩個php文件
注意:這里采用的是thrift-0.9.0版本,生成的是上述兩個版本在 thrift-1.0.0以上的版本,生成的文件和引入方式有所不同,這里暫只介紹thrift-0.9.0版本所生成的相關php文件引入方式
在php項目中新建一個hbase目錄并拷貝如下文件
1.THBaseService.php 和 Types.php 拷貝到/hbase目錄下
2.解壓thrift-0.9.0.tar.gz 并將 thrift-0.9.0\lib\php\lib\Thrift 下所有文件拷貝到 /hbase/Thrift目錄下
此時目錄結構應該是這樣的
hbase目錄
Thrift目錄
這樣php所需要的hbase相關php類都在/hbase這個目錄下了
PHP導入Hbase相關類
在php文件中編寫如下代碼,導入Hbase相關依賴
$GLOBALS['HBASE_ROOT'] = '/hbase';
require_once $GLOBALS['HBASE_ROOT'].'/Thrift/ClassLoader/ThriftClassLoader.php';
use Thrift\ClassLoader\ThriftClassLoader;
use Thrift\Protocol\TBinaryProtocol;
use Thrift\Transport\TSocket;
use Thrift\Transport\TBufferedTransport;
$loader = new ThriftClassLoader();
$loader->registerNamespace('Thrift', dirname(dirname(__DIR__)) . '/hbase/');
$loader->register();
require_once $GLOBALS['HBASE_ROOT'].'/THBaseService.php';
require_once $GLOBALS['HBASE_ROOT'].'/Types.php';
測試連接是否成功
$host = '127.0.0.1';
$port = 8020;
$socket = new TSocket($host, $port);
$transport = new TBufferedTransport($socket);
$protocol = new TBinaryProtocol($transport);
$client = new THBaseServiceClient($protocol);
$transport->open();
return $client;
再來個查詢
$scanId = $client->openScanner( $tableName , $scan);
$numRows = 100;
$resultList = [];
while (true) {
$arr = $client->getScannerRows($scanId, $numRows);
$resultList = array_merge($resultList, $arr);
if (count($arr) < $numRows) {
break;
}
}
$client->closeScanner($scanId);
return $resultList;
Types.php 存放的是hbase定義的相關類、THBaseService定義的是client相關操作
總結
以上是生活随笔為你收集整理的php hbase thrift,PHP使用Thrift操作Hbase的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 骁龙8+凉了 小米12S Ultra叶脉
- 下一篇: 悟空php微信复制的东西在哪找,微信收藏