php hbase thrift,PHP使用Thrift操作Hbase

72123510aa97?utm_campaign=maleskine&utm_content=note&utm_medium=seo_notes&utm_source=recommendation

系统架构图

HBase 启动 Thrift服务

hbase启动thrift服务

// 进入安装的hbase bin目录下

// 执行

hbase-daemon.sh start thrift2

需要注意的是,这里启动的是thrift2服务,如果需要启动thrift服务只需要将thrift2改为thrift就可以了,具体thrift和thrift2之间的区别可以参考以下文章hbase的thrift接口

生成 php所需hbase相关php类

首先到appache官网上下载thrift windows 可执行程序 thrift-0.9.0.exe 和thrift-0.9.0.tar.gz 两个文件 下载地址

从linux环境中的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目录下,执行

// 生成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目录下

此时目录结构应该是这样的

72123510aa97?utm_campaign=maleskine&utm_content=note&utm_medium=seo_notes&utm_source=recommendation

hbase目录

72123510aa97?utm_campaign=maleskine&utm_content=note&utm_medium=seo_notes&utm_source=recommendation

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相关操作

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值