PHP通過Thrift操作Hbase

HBase是一個開源的NoSQL產品,它是實現了Google BigTable論文的一個開源產品,和Hadoop和HDFS一起,可用來存儲和處理海量column family的數據。官方網址是:http://hbase.apache.org

一 、HBase訪問接口

1.  Native Java API,最常規和高效的訪問方式,適合Hadoop MapReduce Job并行批處理HBase表數據
2.  HBase Shell,HBase的命令行工具,最簡單的接口,適合HBase管理使用
3.  Thrift Gateway,利用Thrift序列化技術,支持C++,PHP,Python等多種語言,適合其他異構系統在線訪問HBase表數據
4.  REST Gateway,支持REST 風格的Http API訪問HBase, 解除了語言限制
5.  Pig,可以使用Pig Latin流式編程語言來操作HBase中的數據,和Hive類似,本質最終也是編譯成MapReduce Job來處理HBase表數據,適合做數據統計
6.  Hive,當前Hive的Release版本尚沒有加入對HBase的支持,但在下一個版本Hive 0.7.0中將會支持HBase,可以使用類似SQL語言來訪問HBase
如果使用PHP操作Hbase,推薦使用Facebook開源出來的thrift,官網是:http://thrift.apache.org/ ,它是一個類似ice的中間件,用于不同系統語言間信息交換。

二、安裝Thrift

在Hadoop和Hbase都已經安裝好的集群上安裝Thrift,Thrift安裝在Hmaster機器上

1. 下載thrift

wget http://mirror.bjtu.edu.cn/apache//thrift/0.8.0/thrift-0.8.0.tar.gz

2. 解壓

tar -xzf thrift-0.8.0.tar.gz

3 .編譯安裝:

如果是源碼編譯的,首先要使用./boostrap.sh創建文件./configure ,我們這下載的tar包,自帶有configure文件了。((可以查閱README文件))

If you are building from the first time out of the source repository, you will
need to generate the configure scripts.  (This is not necessary if you
downloaded a tarball.)  From the top directory, do:
./bootstrap.sh

./configure
make ; make install

4. 啟動:

# ./bin/hbase-daemon.sh start thrift [–port=PORT]
starting thrift, logging to /home/banping/hbase/hbase-0.90.3/bin/../logs/hbase-root-thrift-localhost.localdomain.out

Thrift默認監聽的端口是9090

使用jps查看進程,看到ThriftServer進程:

1.gif

三、測試:

1 .php腳本庫操作Hbase

PHP通過Thrift訪問Hbase的庫是在thrift-0.8.0/lib/php/src目錄下,其實這個文件夾下也包含通過Thrift訪問Hbase的PHP擴展源代碼。

1)復制thrift-0.8.0/lib/php到相應的php web目錄。

2)然后生成php與hbase接口文件

  #/usr/local/thrift/bin/thrift –gen php /usr/local/hbase/src/main/resources/org/apache/hadoop/hbase/thrift/Hbase.thrift

  #(根據自己的目錄設置)

   生成目錄文件: /usr/local/hbase/gen-php/Hbase

   有文件: Hbase.php,Hbase_types.php

   把Hbase.php,Hbase_types.php copy到:web目錄/php/src/packages/Hbase/

3)使用php腳本測試:

<?php  
  
ini_set('display_errors', E_ALL);  
$GLOBALS['THRIFT_ROOT'] = './php/src';  
  
require_once( $GLOBALS['THRIFT_ROOT'] . '/Thrift.php' );  
require_once( $GLOBALS['THRIFT_ROOT'] . '/transport/TSocket.php' );  
require_once( $GLOBALS['THRIFT_ROOT'] . '/transport/TBufferedTransport.php' );  
require_once( $GLOBALS['THRIFT_ROOT'] . '/protocol/TBinaryProtocol.php' );  
require_once( $GLOBALS['THRIFT_ROOT'] . '/packages/Hbase/Hbase.php' );  
  
$socket = new TSocket('10.64.60.83', '9090');  
  
$socket->setSendTimeout(10000); // Ten seconds (too long for production, but this is just a demo ;)  
$socket->setRecvTimeout(20000); // Twenty seconds  
$transport = new TBufferedTransport($socket);  
$protocol = new TBinaryProtocol($transport);  
$client = new HbaseClient($protocol);  
  
$transport->open();  
  
//獲取表列表  
$tables = $client->getTableNames();  
sort($tables);  
foreach ($tables as $name) {  
  
    echo( "  found: {$name}\n" );  
}  
   
//創建新表student  
$columns = array(  
    new ColumnDescriptor(array(  
        'name' => 'id:',  
        'maxVersions' => 10  
    )),  
    new ColumnDescriptor(array(  
        'name' => 'name:'  
    )),  
    new ColumnDescriptor(array(  
        'name' => 'score:'  
    )),  
);  
  
$tableName = "student";  
try {  
    $client->createTable($tableName, $columns);  
} catch (AlreadyExists $ae) {  
    echo( "WARN: {$ae->message}\n" );  
}  
//獲取表的描述  
  
$descriptors = $client->getColumnDescriptors($tableName);  
asort($descriptors);  
foreach ($descriptors as $col) {  
    echo( "  column: {$col->name}, maxVer: {$col->maxVersions}\n" );  
}  
  
//修改表列的數據  
$row = '2';  
$valid = "foobar-\xE7\x94\x9F\xE3\x83\x93";  
$mutations = array(  
    new Mutation(array(  
        'column' => 'score',  
        'value' => $valid  
    )),  
);  
$client->mutateRow($tableName, $row, $mutations);  
  
  
//獲取表列的數據  
$row_name = '2';  
$fam_col_name = 'score';  
$arr = $client->get($tableName, $row_name, $fam_col_name);  
// $arr = array  
foreach ($arr as $k => $v) {  
// $k = TCell  
    echo ("value = {$v->value} , <br>  ");  
    echo ("timestamp = {$v->timestamp}  <br>");  
}  
  
$arr = $client->getRow($tableName, $row_name);  
// $client->getRow return a array  
foreach ($arr as $k => $TRowResult) {  
// $k = 0 ; non-use  
// $TRowResult = TRowResult  
    var_dump($TRowResult);  
}  
  
$transport->close();  
?>

通過瀏覽器查看看到項目中的所有表,證明PHP可以通過thrift訪問HBase了。

2. 使用PHP擴展的方式來使用thrift

我們使用PHP自帶的phpize來生成Thtift的php擴展。該擴展的源碼結構:

2.gif

hadoop@ubuntu:/usr/local/hbase-0.90.4/thrift-0.8.0/lib/php/src
$ cd ext/thrift_protocol
$ /usr/local/php/bin/phpize
$ ./configure –with-php-config=/usr/local/php/bin/php-config –enable-thrift_protocol
$ make
$ make install

然后把生成的thrift_protocol.so文件配置到php.ini并重啟apache服務。

轉自:http://blog.csdn.net/hguisu/article/details/7298456

原創文章,作者:s19930811,如若轉載,請注明出處:http://www.www58058.com/3076

(0)
s19930811s19930811
上一篇 2015-05-18 17:45
下一篇 2015-05-18 17:48

相關推薦

  • RAID[Redundant Array of Independent Disks | 獨立硬盤冗余陣列 ]

    前言 一個技術要知其然還要知其所以然 RAID 獨立硬盤冗余陣列(RAID, Redundant Array of Independent Disks),舊稱廉價磁盤冗余陣列(Redundant Array of Inexpensive Disks),簡稱磁盤陣列 RAID的基本思想 其基本思想就是把多個相對便宜的硬盤組合起來,成為一個硬盤陣列組,使性能達到…

    Linux干貨 2016-05-20
  • Linux任務計劃,周期性任務執行詳解

    Linux任務計劃,周期性任務執行 概述 本章將為大家介紹一些任務計劃和周期性任務計劃執行相關內容任務計劃的分類主要有由兩種    未來的某時間點執行一次某任務:at,batch    周期性運行某任務:crontab具體分為三個方面來說明:    1、at計劃任務    2、c…

    Linux干貨 2016-09-27
  • 關于軟件包管理

            軟件包管理 CentOS系統上使用rpm命令管理程序包 安裝、卸載、升級、查詢、檢驗、數據庫維護 rpm安裝:         rpm {-i|–install} [安裝-選項] 打包;包         -h …

    系統運維 2016-08-30
  • 系統時間與硬件時間

    1. “系統時間”與“硬件時間”    系統時間: 一般說來就是我們執行 date 命令看到的時間,linux系統下所有的時間調用(除了直接訪問硬件時間的命令)都是使用的這個時間。    硬件時間: 主板上BIOS中的時間,由主板電池供電來維持運行,系統開機時要讀取這個時間,并根…

    Linux干貨 2016-08-05
  • 馬哥教育網絡班22期第一周課程練習2-未聞花名

    語法:export [-fnp][變量名稱]=[變量設置值] 補充說明:在shell中執行程序時,shell會提供一組環境變量。export可新增,修改或刪除環境變量,供后續執行的程序使用。export的效力僅及于該此登陸操作。 參數: -f 代表[變量名稱]中為函數名稱。 -n 刪除指定的變量。變量實際上并未刪除,只是不會輸出到后續指令的執行環境中。 -p…

    Linux干貨 2016-08-15
  • 使用pyenv管理不同版本的python

    安裝: 安裝: $ curl -L https://raw.githubusercontent.com/yyuu/pyenv-installer/master/bin/pyenv-installer | bash 在你的shellrc文件中添加: export PATH=”$HOME/.pyenv/bin:$PATH” eval “$(pyenv init …

    Linux干貨 2015-03-12
欧美性久久久久