云数据库HBase标准版支持Thrift多语言访问,Thrift是云数据库HBase标准版实例中的一种服务组件,基于Apache Thrift(多语言支持的通信框架)开发。本文介绍通过Thrift多语言访问云数据库HBase标准版实例。
前提条件
已创建HBase标准版实例,具体操作请参见购买实例。
访问流程
访问云数据库HBase标准版实例将您的请求发送给Thrift,Thrift通过Java API访问云数据库HBase标准版实例,流程如下图所示。
获取Thrift连接信息
登录HBase管理控制台。
在顶部菜单栏的左上角,选择实例所属的地域。
在集群列表页,单击目标实例ID。
在左侧导航栏中选择数据库连接。
在连接信息区域中,查看HBase Thrift的访问地址。
说明HBase Thrift访问地址为内网地址,不支持使用外网访问。
访问HBase实例(以PHP为例)
下载Thrift安装包。
说明云数据库HBase标准版的Thrift环境是0.9.0,建议使用版本为0.9.0的Thrift。
云数据库HBase使用的是Thrift1协议,下载HBase Thrift1定义文件。
生成对应语言的接口定义文件。
解压thrift-0.9.0.tar.gz包。
使用命令行进入已解压的thrift-0.9.0.tar.gz文件中执行如下命令生成接口定义文件。
thrift --gen php Hbase.thrift
说明为了方便查看,您可以将解压的Thrift文件夹和gen-php接口定义文件同时放在src目录下,得到的目录结果如下所示。
[testuser@xxxxxxxxxxx thrift_client]# ll total 12 -rw-r--r-- 1 zookeeper games 2743 Aug 2 11:16 client.php drwxr-xr-x 3 zookeeper games 4096 Aug 2 01:22 gen-php drwxr-xr-x 12 zookeeper games 4096 Aug 2 01:22 Thrift
以PHP语言为例,用以下程序代码在HBase标准版实例中创建名称为new的表格。
<?php ini_set('display_errors', E_ALL); $GLOBALS['THRIFT_ROOT'] = "/home/thrift_client"; /* Dependencies. In the proper order. */ require_once $GLOBALS['THRIFT_ROOT'] . '/Thrift/Transport/TTransport.php'; require_once $GLOBALS['THRIFT_ROOT'] . '/Thrift/Transport/TSocket.php'; require_once $GLOBALS['THRIFT_ROOT'] . '/Thrift/Protocol/TProtocol.php'; require_once $GLOBALS['THRIFT_ROOT'] . '/Thrift/Protocol/TBinaryProtocol.php'; require_once $GLOBALS['THRIFT_ROOT'] . '/Thrift/Protocol/TBinaryProtocolAccelerated.php'; require_once $GLOBALS['THRIFT_ROOT'] . '/Thrift/Transport/TBufferedTransport.php'; require_once $GLOBALS['THRIFT_ROOT'] . '/Thrift/Type/TMessageType.php'; require_once $GLOBALS['THRIFT_ROOT'] . '/Thrift/Factory/TStringFuncFactory.php'; require_once $GLOBALS['THRIFT_ROOT'] . '/Thrift/StringFunc/TStringFunc.php'; require_once $GLOBALS['THRIFT_ROOT'] . '/Thrift/StringFunc/Core.php'; require_once $GLOBALS['THRIFT_ROOT'] . '/Thrift/Type/TType.php'; require_once $GLOBALS['THRIFT_ROOT'] . '/Thrift/Exception/TException.php'; require_once $GLOBALS['THRIFT_ROOT'] . '/Thrift/Exception/TTransportException.php'; require_once $GLOBALS['THRIFT_ROOT'] . '/Thrift/Exception/TProtocolException.php'; require_once $GLOBALS['THRIFT_ROOT'] . '/gen-php/Hbase/Types.php'; require_once $GLOBALS['THRIFT_ROOT'] . '/gen-php/Hbase/Hbase.php'; use Thrift\Protocol\TBinaryProtocol; use Thrift\Transport\TBufferedTransport; use Thrift\Transport\TSocket; use Hbase\HbaseClient; use Hbase\ColumnDescriptor; use Hbase\Mutation; $host='hb-bp12pt6alr1788y35-001.hbase.rds.aliyuncs.com'; $port=9099; $socket = new TSocket($host, $port); $socket->setSendTimeout(10000); // 发送超时,单位毫秒 $socket->setRecvTimeout(20000); // 接收超时,单位毫秒 $transport = new TBufferedTransport($socket); $protocol = new TBinaryProtocol($transport); $client = new HbaseClient($protocol); $transport->open(); ####列出表#### echo "----list tables----\n"; $tables = $client->getTableNames(); foreach ($tables as $name) { var_dump($tables); } $tablename='new'; ####写数据#### echo "----write data----\n"; $row = 'key'; $value = 'value'; $attribute = array(); $mutations = array( new Mutation(array( 'column' => 'info:cn1', 'value' => $value )), ); try { $client->mutateRow($tablename, $row, $mutations, $attribute); } catch (Exception $e) { var_dump($e); } ###读数据#### echo "---read data---\n"; $result = $client->getRow($tablename, $row, $attribute); var_dump($result); ###删数据#### echo "---delete data---\n"; $client->deleteAllRow($tablename, $row, $attribute); echo "---get data---\n"; $result = $client->getRow($tablename, $row, $attribute); var_dump($result); ###扫描数据### $row = 'ID1'; $value = 'v1'; $mutations = array( new Mutation(array( 'column' => 'info:c1', 'value' => $value )), ); try { $client->mutateRow($tablename, $row, $mutations, $attribute); } catch (Exception $e) { var_dump($e); } $row = 'ID6'; $value = 'v2'; $mutations = array( new Mutation(array( 'column' => 'info:c1', 'value' => $value )), ); try { $client->mutateRow($tablename, $row, $mutations, $attribute); } catch (Exception $e) { var_dump($e); } $row = 'ID3'; $value = 'v3'; $mutations = array( new Mutation(array( 'column' => 'info:c1', 'value' => $value )), ); try { $client->mutateRow($tablename, $row, $mutations, $attribute); } catch (Exception $e) { var_dump($e); } echo 'prefix scan'; $scan = $client->scannerOpenWithPrefix($tablename, 'ID', null, null); $nbRows = 100; $arr = $client->scannerGetList($scan, $nbRows); echo 'count of result :'.count($arr)."\n"; var_dump($arr); foreach ($arr as $k => $TRowResult) { echo "\trow:$TRowResult->row\tcolumns(array):"; foreach ($TRowResult->columns as $key => $value) { echo "key:$key\tvalue:$value->value\ttimestamp:$value->timestamp\n"; } } echo 'range scan'; $scan = $client->scannerOpenWithStop($tablename, 'ID0', 'ID6', null, null); $nbRows = 100; $arr = $client->scannerGetList($scan, $nbRows); echo 'count of result :'.count($arr)."\n"; var_dump($arr); foreach ($arr as $k => $TRowResult) { echo "\trow:$TRowResult->row\tcolumns(array):"; foreach ($TRowResult->columns as $key => $value) { echo "key:$key\tvalue:$value->value\ttimestamp:$value->timestamp\n"; } } ###Increment 操作### echo "do increment on a new row"; $row = 'ID4'; try { $newCount = $client->atomicIncrement($tablename, $row, 'info:c1', 1234); } catch (Exception $e) { var_dump($e); } echo "new count $newCount\n"; ###读写 long型数据到hbase### $row = 'ID5'; $value = pack("J", 4567); $mutations = array( new Mutation(array( 'column' => 'info:c1', 'value' => $value )), ); try { $client->mutateRow($tablename, $row, $mutations, null); } catch (Exception $e) { var_dump($e); } echo "---read data and print it as long ---\n"; $result = $client->getRow($tablename, $row, null); foreach ($result[0]->columns as $key => $value) { $count = unpack("J*mycount", $value->value); var_dump($count); } ?>
代码执行结果如下。
[testuser@xxxxxxxxxxx thrift_client]# php client.php ----list tables---- array(1) { [0]=> string(3) "new" } ----write data---- ---read data--- array(1) { [0]=> object(Hbase\TRowResult)#8 (3) { ["row"]=> string(3) "key" ["columns"]=> array(1) { ["info:cn1"]=> object(Hbase\TCell)#10 (2) { ["value"]=> string(5) "value" ["timestamp"]=> int(1533179795969) } } ["sortedColumns"]=> NULL } } ---delete data--- ---get data--- array(0) { }
访问HBase实例(以Python为例)
如果您已经安装了Python和pip,可以通过pip安装Thrift后访问HBase标准版实例。
说明
以下步骤中提供的示例代码是基于Python 2版本。
使用以下代码安装Thrift。
pip install thrift pip install hbase-thrift
访问HBase标准版实例。
import sys import time import os from thrift import Thrift from thrift.transport import TSocket, TTransport from thrift.protocol import TBinaryProtocol from thrift.protocol.TBinaryProtocol import TBinaryProtocolAccelerated from hbase import ttypes from hbase.Hbase import Client, ColumnDescriptor, Mutation def printRow(entry): print "row: " + entry.row + ", cols:", for k in sorted(entry.columns): print k + " => " + entry.columns[k].value, print transport = TSocket.TSocket('hb-bp12pt6alr1788y35-001.hbase.rds.aliyuncs.com', 9099) transport = TTransport.TBufferedTransport(transport) protocol = TBinaryProtocol.TBinaryProtocolAccelerated(transport) client = Client(protocol) transport.open() print "---list table--" print client.getTableNames() table="new" row="key" print "---write data---" mutations = [Mutation(column="info:cn1", value="value")] client.mutateRow(table, row, mutations, {}) print "---get data----" printRow(client.getRow(table, row, {})[0]) print "---delete data---" client.deleteAllRow(table, row, {}) print "---end----" transport.close()
执行结果如下。
[testuser@Test ~]# python Hbase_client.py ---list table-- ['new'] ---write data--- ---get data---- row: key, cols: info:cn1 => value ---delete data--- ---end----
访问HBase实例(以Go为例)
文档内容是否对您有帮助?