溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

如何通過(guò)Thrift訪問(wèn)ApsaraDB for HBase

發(fā)布時(shí)間:2021-12-09 10:19:11 來(lái)源:億速云 閱讀:144 作者:小新 欄目:云計(jì)算

這篇文章主要介紹如何通過(guò)Thrift訪問(wèn)ApsaraDB for HBase,文中介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們一定要看完!

Thrift 多語(yǔ)言接入

Thrift 提供多語(yǔ)言訪問(wèn)HBase的能力,支持的語(yǔ)言包從Thrift官網(wǎng)看括: C++, Java, Python, PHP, Ruby, Erlang, Perl, Haskell, C#, Cocoa, JavaScript, Node.js, Smalltalk, OCaml , Delphi 以及別的語(yǔ)言.主要流程是用戶thrift Client 通過(guò)Thrift協(xié)議訪問(wèn)HBase的thriftserver,thriftserver做請(qǐng)求轉(zhuǎn)發(fā)給HBase的存儲(chǔ)服務(wù)來(lái)做數(shù)據(jù)的讀以及寫操作.大概架構(gòu)圖如下:

如何通過(guò)Thrift訪問(wèn)ApsaraDB for HBase

要通過(guò)thrift 多語(yǔ)言訪問(wèn)HBase需要以下幾步:

一、開(kāi)通HBase thriftserver服務(wù):

在用戶自己管控頁(yè)面點(diǎn)擊這里參考開(kāi)通thriftserver服務(wù)化(高可用版本thriftserver),會(huì)得到一個(gè)host:port的訪問(wèn)入口;或者自己可以選擇ECS自建thriftserver方法,參考這里,最終自建ECS的ip (host)以及默認(rèn)的話9090端口作為訪問(wèn)入口。

thriftserver

二、用戶Thrift client訪問(wèn):

一般客戶常見(jiàn)的訪問(wèn)方式是python的訪問(wèn)方式以及php的訪問(wèn)方式 ,這里我們先一步步給出php的訪問(wèn)方式;

2.1 . 以php走thrift訪問(wèn)HBase:

2.1.1 . 安裝thrift 編譯環(huán)境;

我們?cè)艸Base的thrift環(huán)境是0.9.0,所以建議客戶自己建立自己的thrift環(huán)境也是0.9.0,這里可以從這里下載thrift的0.9.0 版本,下載的源碼包我們后面會(huì)用到,這里需要先安裝thrift編譯環(huán)境,對(duì)于源碼安裝可以參考thrift官網(wǎng);

通過(guò)如下命令可以看出安裝thrift的版本信息;

thrift --version

2.1.2. 生成thrift訪問(wèn)client的訪問(wèn)文件;

我們從這里下載出我們?cè)艸Base的Hbase.thrift文件,這里我們?cè)艸Base使用的是thrift1協(xié)議,具體可以參考文件看出使用格式,下載完成以后執(zhí)行thrift命令進(jìn)行編譯;

編譯命令如下:

 thrift --gen <language> Hbase.thrift

上述是語(yǔ)言的縮寫,那么常見(jiàn)的有如下:

thrift --gen php Hbase.thrift
thrift --gen cpp Hbase.thrift
thrift --gen py Hbase.thrift

執(zhí)行thrift --gen php Hbase.thrift 以后會(huì)在目錄下得到gen-php 這個(gè)就是我們需要的函數(shù)包文件;

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

此外我們?cè)?.1.1得到thrift的源碼包文件將下載到的Thrift源碼文件夾下的/lib/php/lib下面的Thrift文件夾以及gen-php一起丟在我們的業(yè)務(wù)邏輯代碼一個(gè)src目錄下面,加上我們自己的client.php的代碼,目錄結(jié)果如下所示:

[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訪問(wèn)代碼編寫;

這個(gè)時(shí)候,我們來(lái)編寫我們的client.php代碼邏輯,上述的Thrift文件夾以及gen-php文件夾,可以隨自己項(xiàng)目以及個(gè)人風(fēng)格命名,這里方便大家搞清目錄結(jié)構(gòu),就保留原來(lái)風(fēng)格;下面貼出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); // 發(fā)送超時(shí),單位毫秒
$socket->setRecvTimeout(20000); // 接收超時(shí),單位毫秒
$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';
####寫數(shù)據(jù)####
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
}

###讀數(shù)據(jù)####
echo "---read data---\n";
$result = $client->getRow($tablename, $row, $atrribute);
var_dump($result);

###刪數(shù)據(jù)####
echo "---delete data---\n";
$client->deleteAllRow($tablename, $row, $atrribute);
echo "---get data---\n";
$result = $client->getRow($tablename, $row, $atrribute);
var_dump($result);
?>

代碼執(zhí)行結(jié)果如下:

[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訪問(wèn)流程;

此外還有常見(jiàn)的python的客戶,對(duì)于python的話,有happybase這種python的第三方包含thrift的庫(kù)去做,我們見(jiàn)過(guò)一些客戶使用Happybase進(jìn)行訪問(wèn)HBase thrift,參見(jiàn)文章;此外,python 有豐富的庫(kù),我們通過(guò)pip可以安裝thrift,以及訪問(wèn)HBase的thrift庫(kù);執(zhí)行流程如下,假設(shè)用戶已經(jīng)安裝python以及pip:

pip install thrift //安裝thrift默認(rèn)最新版本
pip install hbase-thrift //安裝hbase thrift接口庫(kù)

上面2步執(zhí)行完成以后,既可以編寫訪問(wèn)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()

對(duì)應(yīng)上述的程序執(zhí)行的結(jié)果如下:

[root@Test ~]# python Hbase_client.py
---list table--
['new']
---write data---
---get data----
row: key, cols: info:cn1 => value
---delete data---
---end----

三、訪問(wèn)HBase thriftserver

3.1、訪問(wèn)機(jī)器開(kāi)通白名單

將訪問(wèn)的機(jī)器的ip加入HBase集群的白名單,然后就可以正常執(zhí)行代碼;

如何通過(guò)Thrift訪問(wèn)ApsaraDB for HBase

以上是“如何通過(guò)Thrift訪問(wèn)ApsaraDB for HBase”這篇文章的所有內(nèi)容,感謝各位的閱讀!希望分享的內(nèi)容對(duì)大家有幫助,更多相關(guān)知識(shí),歡迎關(guān)注億速云行業(yè)資訊頻道!

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI