php hbase thrift,PHP使用Thrift操作Hbase
系統(tǒng)架構(gòu)圖
HBase 啟動(dòng) Thrift服務(wù)
hbase啟動(dòng)thrift服務(wù)
// 進(jìn)入安裝的hbase bin目錄下
// 執(zhí)行
hbase-daemon.sh start thrift2
需要注意的是,這里啟動(dòng)的是thrift2服務(wù),如果需要啟動(dòng)thrift服務(wù)只需要將thrift2改為thrift就可以了,具體thrift和thrift2之間的區(qū)別可以參考以下文章hbase的thrift接口
生成 php所需hbase相關(guān)php類(lèi)
首先到appache官網(wǎng)上下載thrift windows 可執(zhí)行程序 thrift-0.9.0.exe 和thrift-0.9.0.tar.gz 兩個(gè)文件 下載地址
從linux環(huán)境中的Hbase安裝目錄里獲取hbase.thrift文件,這個(gè)文件用來(lái)生成相關(guān)php類(lèi)
// 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相關(guān)類(lèi)
thrift-0.9.0.exe -gen php hbase.thrift
可以看到目錄下多出了gen-php目錄,gen-php目錄下包含THBaseService.php和Types.php兩個(gè)php文件
注意:這里采用的是thrift-0.9.0版本,生成的是上述兩個(gè)版本在 thrift-1.0.0以上的版本,生成的文件和引入方式有所不同,這里暫只介紹thrift-0.9.0版本所生成的相關(guān)php文件引入方式
在php項(xiàng)目中新建一個(gè)hbase目錄并拷貝如下文件
1.THBaseService.php 和 Types.php 拷貝到/hbase目錄下
2.解壓thrift-0.9.0.tar.gz 并將 thrift-0.9.0\lib\php\lib\Thrift 下所有文件拷貝到 /hbase/Thrift目錄下
此時(shí)目錄結(jié)構(gòu)應(yīng)該是這樣的
hbase目錄
Thrift目錄
這樣php所需要的hbase相關(guān)php類(lèi)都在/hbase這個(gè)目錄下了
PHP導(dǎo)入Hbase相關(guān)類(lèi)
在php文件中編寫(xiě)如下代碼,導(dǎo)入Hbase相關(guān)依賴
$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';
測(cè)試連接是否成功
$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;
再來(lái)個(gè)查詢
$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定義的相關(guān)類(lèi)、THBaseService定義的是client相關(guān)操作
總結(jié)
以上是生活随笔為你收集整理的php hbase thrift,PHP使用Thrift操作Hbase的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 骁龙8+凉了 小米12S Ultra叶脉
- 下一篇: android+图标+i_explore