使用thrift的php版本操作hbase数据库

本文介绍了如何配置HBase和Thrift环境,以及使用PHP生成API接口来操作HBase数据库。详细步骤包括HBase的环境配置,Thrift的安装与启动,以及具体的PHP操作案例。
摘要由CSDN通过智能技术生成

目录

hbase环境配置

thrift环境配置

api接口生成

操作案例


最近要进行用户行为分析,数据存储决定采用hbase。并且为了便于和当前系统契合,采用php进行存取。

hbase环境配置

 

1. 进入hbase官网,进入下载页面,下载对应版本的可执行文件源文件

2. 直接将可执行文件进行解压缩,并且进行配置。

conf/hbase-env.sh中的 export JAVA_HOME=/***/jdk 设定为自己系统的jdk路径

conf/hbase-site.xml 中配置数据存储路径和端口

<configuration>
  <property>  
    <name>hbase.rootdir</name>  
    <value>file:///opt/bigdata/hbase/data</value>  
  </property>  
  
  <property>
    <name>hbase.master.info.port</name>
    <value>60010</value>
  </property>
</configuration>

另外,要把机器名字配置到hosts当。 执行hostname,并且将机器名称结果在hosts写入 127.0.0.1  devname

3.进入bin目录,启动hbase

start-hbase.sh

至此,hbase环境就搭建好了

 

thrift环境配置

1.进入thrift官网下载页面,并且进行下载

2.解压缩,并且进行安装

./configure -prefix=/安装目录  -with-php=/usr/bin

make

make install

3.启动hbase的thrift服务

./hbase-daemon.sh start thrift

api接口生成

1. 记得第一步中下载的hbase源码,在这里可以用的上了,解压缩hbase的源码,并且找到文件Hbase.thrift

hbase-2.1.0/hbase-thrift/src/main/resources/org/apache/hadoop/hbase/thrift/Hbase.thrift

进入thrift可执行文件目录bin中,生成php版本的接口。

./thrift --gen php hbase-2.1.0/hbase-thrift/src/main/resources/org/apache/hadoop/hbase/thrift/Hbase.thrift

2.拷贝thrift中php的库文件 hrift-0.11.0/lib/php , 

cp -r thrift-0.11.0/lib/php ./thrift_lib
cp -r gen-php/* ./thrift_lib

 

操作案例

1.在hbase中插入表,并且插入数据,具体详细命令可以参考这里

./hbase shell
create 'test',{NAME=>'cf1',VERSIONS=>1000},{NAME=>'cf2',VERSIONS=>1000}
put 'test','20180814','cf1:column1','test_cf1_column1'
put 'test','20180814','cf2:column1','test_cf2_column1'
scan 'test'

2.依赖于上一步中的thrift_lib,我们开始创建php代码

<?php
$GLOBALS['THRIFT_ROOT'] = '/opt/bigdata/test/test_thrift/thrift_lib';
require_once($GLOBALS['THRIFT_ROOT'].'/Hbase/Hbase.php');
require_once($GLOBALS['THRIFT_ROOT'].'/Hbase/Types.php');

require_once( $GLOBALS['THRIFT_ROOT'].'/lib/Thrift/ClassLoader/ThriftClassLoader.php' );
use Thrift\ClassLoader\ThriftClassLoader;
$loader = new ThriftClassLoader();
$loader->registerNamespace('Thrift',$GLOBALS['THRIFT_ROOT'].'/lib/');
$loader->registerDefinition('shared',$GLOBALS['THRIFT_ROOT']);
$loader->registerDefinition('tutorial',$GLOBALS['THRIFT_ROOT']);
$loader->register();

use Thrift\Protocol\TBinaryProtocol;
use Thrift\Transport\TSocket;
use Thrift\Transport\THttpClient;
use Thrift\Transport\TBufferedTransport;
use Thrift\Exception\TException;
use Hbase\HbaseClient;
use Hbase\Mutation;
use Hbase\ColumnDescriptor;

//$socket = new Thrift\Transport\TSocket( 'localhost', 9090 );
$socket = new TSocket( 'localhost', 9090 );
$socket->setSendTimeout( 10000 ); // Ten seconds (too long for production, but this is just a demo ;)
$socket->setRecvTimeout( 20000 ); // Twenty seconds

$transport = new TBufferedTransport( $socket );

$protocol = new TBinaryProtocol( $transport );

$client = new HBase\HbaseClient( $protocol );
$transport->open();
$table = 'test';

//get table names
$tables = $client->getTableNames();
var_dump($tables);

//get data
$rows = $client->getRows($table,['20180814'],['cf1']);
$rows = $client->getRow($table,'20180814',['cf1']);
$rows = $client->getRowsWithColumns($table,['20180814',],['cf1:column1'],[]);
$rows = $client->getRowTs($table,'20180814',1544214539999,[]);
$rows = $client->getVerTs($table,'20180814','cf1:column1',1544214539933,2,[]);
$rows = $client->getVer($table,'20180814','cf1:column1',20,[]);
var_dump($rows);

//put data
$dataArr = [
    new Mutation(['column'=>'cf1:column1','value'=>'101505033']),
    new Mutation(['column'=>'cf1:column2','value'=>'3']),
    new Mutation(['column'=>'cf1:column3','value'=>'12']),
    new Mutation(['column'=>'cf2:column1','value'=>'101505033']),
    new Mutation(['column'=>'cf2:column2','value'=>'3']),
    new Mutation(['column'=>'cf2:column3','value'=>'12']),
];
$client->mutateRow($table,'20180814',$dataArr,[]);


$transport->close();
?>

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值