手把手教你经过Thrift 访问ApsaraDB for HBase

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的存储服务来作数据的读以及写操做.大概架构图以下:php

​ 

​ 要经过thrift 多语言访问HBase须要如下几步:html

1、开通HBase thriftserver服务:python

​ 在用户本身管控页面点击这里参考开通thriftserver服务化(高可用版本thriftserver),会获得一个host:port的访问入口;或者本身能够选择ECS自建thriftserver方法,参考这里,最终自建ECS的ip (host)以及默认的话9090端口做为访问入口。git

2、用户Thrift client访问:apache

​ 通常客户常见的访问方式是python的访问方式以及php的访问方式 ,这里咱们先一步步给出php的访问方式;架构

2.1 . 以php走thrift访问HBase:app

​ 2.1.1 . 安装thrift 编译环境;socket

​ 咱们云HBase的thrift环境是0.9.0,因此建议客户本身创建本身的thrift环境也是0.9.0,这里能够从这里下载thrift的0.9.0 版本,下载的源码包咱们后面会用到,这里须要先安装thrift编译环境,对于源码安装能够参考thrift官网函数

经过以下命令能够看出安装thrift的版本信息;ui

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----

3、访问HBase thriftserver

​ 3.一、访问机器开通白名单

​ 将访问的机器的ip加入HBase集群的白名单,而后就能够正常执行代码;

文章连接:https://yq.aliyun.com/articles/622400

相关文章
相关标签/搜索