Hbase通过thrift这个跨语言的RPC框架提供多语言的调用。
Hbase有两套thrift接口(thrift1和thrift2),但是它们并不兼容。根据官方文档,thrift1很可能被抛弃,本文以thrift2整合为例。
1、访问官网http://thrift.apache.org/download,下载
thrift-0.11.0.exe (生成接口rpc工具,thrift-0.11.0.exe改名thrift.exe,保存在D:\project\thrift\thrift.exe)
thrift-0.11.0.tar.gz(thrift相关库,保存在D:\project\thrift\thrift-0.11.0)
2、访问hbase官网(http://archive.apache.org/dist/hbase/),下载hbase-1.2.6-src.tar.gz
解压保存在D:\project\thrift\hbase-1.2.6
3、生成php接口代码
解压hbase-1.2.6-src.tar.gz,hbase-1.2.6\hbase-thrift\src\main\resources\org\apache\hadoop\hbase文件夹同时存在thrift和thrift2接口描述文件,本文只使用thrift2
在D:\project\thrift目录中输入cmd命令,生成对应php的sdk文件。
thrift -gen php hbase-1.2.6\hbase-thrift\src\main\resources\org\apache\hadoop\hbase\thrift2\hbase.thrift
生成的D:\project\thrift\gen-php目录包含文件:
THBaseService.php
Types.php
4、要通过thrifc调用hbase,需要先启动hbase的接口服务
$HBASE_HOME/bin/hbase-daemon.sh start thrift2 //启动
$HBASE_HOME/bin/hbase-daemon.sh stop thrift2 //停止
5、与yii2整合
在vendor文件夹中新建hbase目录
vendor\hbase\gen-php //复制D:\project\thrift\gen-php
vendor\hbase\php //复制D:\project\thrift\thrift-0.11.0\lib\php
由于thrift2的php不使用Composer,类库命名方式也不完全符合PSR-4标准, 所以本文使用include_path方式来定位并导入类文件。
common\models\HArticle.php
<?php
namespace common\models;
require_once dirname(dirname(__DIR__)).'/vendor/hbase/php/lib/Thrift/ClassLoader/ThriftClassLoader.php';
use Thrift\ClassLoader\ThriftClassLoader;
$loader = new ThriftClassLoader();
$loader->registerNamespace('Thrift', dirname(dirname(__DIR__)) . '/vendor/hbase/php/lib');
$loader->register();
require_once dirname(dirname(__DIR__)) . '/vendor/hbase/gen-php/Types.php';
require_once dirname(dirname(__DIR__)) . '/vendor/hbase/gen-php/THBaseService.php';
use Thrift\Protocol\TBinaryProtocol;
use Thrift\Transport\TSocket;
use Thrift\Transport\TBufferedTransport;
use THBaseServiceClient;
use TGet;
class HArticle
{
private $host = '192.168.1.108';
private $port = 9090;
public function get($rowKey){
$socket = new TSocket($this->host, $this->port);
$transport = new TBufferedTransport($socket);
$protocol = new TBinaryProtocol($transport);
$client = new THBaseServiceClient($protocol);
$transport->open();
$tableName = "article_2018";
$get = new TGet();
$get->row = $rowKey;
$arr = $client->get($tableName, $get);
$data = array();
$results = $arr->columnValues;
foreach($results as $result)
{
$qualifier = (string)$result->qualifier;
$value = $result->value;
$data[$qualifier] = $value;
}
$transport->close();
return $data;
}
}
frontend\controllers\TestController.php
<?php
namespace frontend\controllers;
use yii\web\Controller;
use common\models\HArticle;
class TestController extends Controller
{
public function actionIndex()
{
$hArticle = new HArticle();
$data = $hbaseNews->get('20180908_1f1be3cd26a36e351175015f450fa3f6');
var_dump($data);
exit();
}
}
来源:oschina
链接:https://my.oschina.net/u/117179/blog/2049652