中文字幕第五页-中文字幕第页-中文字幕韩国-中文字幕最新-国产尤物二区三区在线观看-国产尤物福利视频一区二区

如何通過Thrift訪問ApsaraDBforHBase

這篇文章主要介紹如何通過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的存儲服務來做數據的讀以及寫操作.大概架構圖如下:

如何通過Thrift訪問ApsaraDB for HBase

要通過thrift 多語言訪問HBase需要以下幾步:

一、開通HBase thriftserver服務:

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

thriftserver

二、用戶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集群的白名單,然后就可以正常執行代碼;

如何通過Thrift訪問ApsaraDB for HBase

以上是“如何通過Thrift訪問ApsaraDB for HBase”這篇文章的所有內容,感謝各位的閱讀!希望分享的內容對大家有幫助,更多相關知識,歡迎關注創新互聯行業資訊頻道!

文章題目:如何通過Thrift訪問ApsaraDBforHBase
文章源于:http://m.2m8n56k.cn/article46/pccgeg.html

成都網站建設公司_創新互聯,為您提供自適應網站網站營銷品牌網站設計品牌網站制作響應式網站

廣告

聲明:本網站發布的內容(圖片、視頻和文字)以用戶投稿、用戶轉載內容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網站立場,如需處理請聯系客服。電話:028-86922220;郵箱:[email protected]。內容未經允許不得轉載,或轉載時需注明來源: 創新互聯

外貿網站制作
主站蜘蛛池模板: 日韩精品在线一区 | 最新亚洲精品国自产在线观看 | 国产一区二区三区精品久久呦 | 亚洲国产精品一区二区九九 | www.99精品| 免费一级欧美大片视频在线 | 国产成人久久精品麻豆二区 | 另类视频在线 | 免费国产不卡午夜福在线观看 | 亚洲第一区香蕉_国产a | 久久精品国产亚洲 | 俺来也俺来也天天夜夜视频 | 一级片网站在线观看 | 成人在线免费小视频 | 成人在线免费视频播放 | 久久青草网站 | 精品久久久久中文字幕日本 | 国产精品免费一区二区三区四区 | 毛片免费网址 | 久久精品国产欧美日韩99热 | 91伊人国产 | 女人把腿劈开让男人桶的网站 | 日韩精品一区二区三区中文在线 | 成人毛片免费 | 日韩麻豆 | 国产成人一级片 | 特级av毛片免费观看 | 日本韩国三级在线观看 | 欧美一级毛片在线一看 | 波多野结衣中文在线播放 | 久久草在线视频 | 国产成人免费观看在线视频 | 欧美毛片在线 | 日本在线视频免费看 | 日本www视频在线观看 | 不卡无毒免费毛片视频观看 | v片免费看| 日韩国产成人资源精品视频 | 欧美真人视频一级毛片 | 成人夜色视频网站在线观看 | 国产日韩欧美在线一二三四 |