手把手教你通过Thrift 访问ApsaraDB for HBase
时间: 2018-08-13来源:OSCHINA
前景提要
【围观】麒麟芯片遭打压成绝版,华为亿元投入又砸向了哪里?>>>
Thrift 多语言接入
​ Thrift 提供多语言访问HBase的能力,支持的语言包从Thrift官网看括: C++, Java, Python, PHP, Ruby, Erlang, Perl, Haskell, C#, Cocoa, JavaScript, Node.js, Smalltalk, OCaml , Delphi 以及别的语言.主要流程是用户thrift Client 通过Thrift协议访问HBase的thriftserver,thriftserver做请求转发给HBase的存储服务来做数据的读以及写操作.大概架构图如下:

​ 要通过thrift 多语言访问HBase需要以下几步:
一、开通HBase thriftserver服务:
​ 在用户自己管控页面点击这里参考开通thriftserver服务化(高可用版本thriftserver),会得到一个host:port的访问入口;或者自己可以选择ECS自建thriftserver方法,参考 这里 ,最终自建ECS的ip (host)以及默认的话9090端口作为访问入口。

二、用户Thrift client访问:
​ 一般客户常见的访问方式是python的访问方式以及php的访问方式 ,这里我们先一步步给出php的访问方式;
2.1 . 以php走thrift访问HBase:
​ 2.1.1 . 安装thrift 编译环境;
​ 我们云HBase的thrift环境是0.9.0,所以建议客户自己建立自己的thrift环境也是0.9.0,这里可以从 这里 下载thrift的0.9.0 版本,下载的源码包我们后面会用到,这里需要先安装thrift编译环境,对于源码安装可以参考 thrift官网 ;
通过如下命令可以看出安装thrift的版本信息; thrift --version
​ 2.1.2. 生成thrift访问client的访问文件;
​ 我们从 这里 下载出我们云HBase的Hbase.thrift文件,这里我们云HBase使用的是thrift1协议,具体可以参考文件看出使用格式,下载完成以后执行thrift命令进行编译;
​ 编译命令如下: thrift --gen <language> Hbase.thrift
​ 上述是语言的缩写,那么常见的有如下: thrift --gen php Hbase.thrift thrift --gen cpp Hbase.thrift thrift --gen py Hbase.thrift
​ 执行thrift --gen php Hbase.thrift 以后会在目录下得到gen-php 这个就是我们需要的函数包文件; thrift git:(last_dev) ll total 56 -rw-r--r-- 1 xuanling.gc staff 24K 3 5 15:06 Hbase.thrift drwxr-xr-x 3 xuanling.gc staff 96B 8 1 16:03 gen-php
​ 此外我们在2.1.1得到thrift的源码包文件将下载到的Thrift源码文件夹下的/lib/php/lib下面的Thrift文件夹以及gen-php一起丢在我们的业务逻辑代码一个src目录下面,加上我们自己的client.php的代码,目录结果如下所示: [root@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
​ 2.1.3. php访问代码编写;
​ 这个时候,我们来编写我们的client.php代码逻辑,上述的Thrift文件夹以及gen-php文件夹,可以随自己项目以及个人风格命名,这里方便大家搞清目录结构,就保留原来风格;下面贴出php的代码,我们下面的所有程序都是在HBase 建了一张表"new": <?php ini_set('display_errors', E_ALL); $GLOBALS['THRIFT_ROOT'] = "/root/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'; $atrribute = array(); $mutations = array( new Mutation(array( 'column' => 'info:cn1', 'value' => $value )), ); try { $client->mutateRow($tablename, $row, $mutations, $atrribute); } catch (Exception $e) { var_dump($e);//这里自己打log } ###读数据#### echo "---read data---\n"; $result = $client->getRow($tablename, $row, $atrribute); var_dump($result); ###删数据#### echo "---delete data---\n"; $client->deleteAllRow($tablename, $row, $atrribute); echo "---get data---\n"; $result = $client->getRow($tablename, $row, $atrribute); var_dump($result); ?>
​ 代码执行结果如下: [root@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) { }
2.2.python访问流程;
​ 此外还有常见的python的客户,对于python的话,有happybase这种python的第三方包含thrift的库去做,我们见过一些客户使用Happybase进行访问HBase thrift,参见 文章 ;此外,python 有丰富的库,我们通过pip可以安装thrift,以及访问HBase的thrift库;执行流程如下,假设用户已经安装python以及pip: pip install thrift //安装thrift默认最新版本 pip install hbase-thrift //安装hbase thrift接口库
​ 上面2步执行完成以后,既可以编写访问HBase的代码: import sys import time import os from thrift import Thrift from thrift.transport import TSocket, TTransport from thrift.protocol import TBinaryProtocol 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.TBinaryProtocol(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()
​ 对应上述的程序执行的结果如下: [root@Test ~]# python Hbase_client.py ---list table-- ['new'] ---write data--- ---get data---- row: key, cols: info:cn1 => value ---delete data--- ---end----
三、访问HBase thriftserver
​ 3.1、访问机器开通白名单
​ 将访问的机器的ip加入HBase集群的白名单,然后就可以正常执行代码;

作者: 玄陵
原文链接
本文为云栖社区原创内容,未经允许不得转载。

科技资讯:

科技学院:

科技百科:

科技书籍:

网站大全:

软件大全:

热门排行