php通過thrift操作hbase

2021-06-07 05:29:36 字數 3910 閱讀 1026

環境配置 

作業系統 centos 5.8    hadoop版本cloudera cdh3u3  hbase版本hbase-0.90.4-cdh3u3  php版本5.2

# wget 

安裝所需的依賴包

# yum install automake libtool flex bison pkgconfig gcc-c++ boost-devel libevent-devel zlib-devel python-devel ruby-devel php php-devel

# tar zxvf  thrift-0.8.0.tar.gz

# cd thrift-0.8.0

#   ./configure --prefix=/home/thrift --with-php-config=/usr/bin/php-config

# make && make install

2  生成php和hbase的介面檔案:

# cd /home/thrift/

# bin/thrift  --gen php $hbase_home/src/main/resources/org/apache/hadoop/hbase/thrift/hbase.thrift

# cd gen-php/hbase

# ls

hbase.php  hbase_types.php

3. 把php客戶端需要的包及剛才生成的介面檔案複製出來供php程式呼叫:

#  mkdir -p /var/www/html/hbasethrift/libs    (/var/www/html為apache的web主目錄)

#  cp -a /home/soft/thrift-0.8.0/lib/php/src /var/www/html/hbasethrift/libs     

#  mkdir -p /var/www/html/hbasethrift/libs/packages

#  cp -a /home/thrift/gen-php/hbase /var/www/html/hbasethrift/libs/

packages

4.  啟動hbase thrift server,測試php連線hbase

# ./bin/hbase-daemon.sh start thrift  

hbase thrift 預設監聽埠為9090

測試php連線與操作hbase** 

# vi hbasethrift.php

<?php

$globals['thrift_root'] = '/home/www/html/hbasethrift/libs';

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( 'localhost', 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();

echo nl2br( "listing tables...\n" );

$tables = $client->gettablenames();

sort( $tables );

foreach ( $tables as $name ) \n" );

}$columns = array(

new columndescriptor( array(

'name' => 'entry:',

'maxversions' => 10

) ),

new columndescriptor( array(

'name' => 'unused:'

) ));

$t = "table1";

echo( "creating table: \n" );

try catch ( alreadyexists $ae ) \n" );

}$t = "test";

echo( "column families in :\n" );

$descriptors = $client->getcolumndescriptors( $t );

asort( $descriptors );

foreach ( $descriptors as $col ) , maxver: \n" );

}$t = "table1";

echo( "column families in :\n" );

$descriptors = $client->getcolumndescriptors( $t );

asort( $descriptors );

foreach ( $descriptors as $col ) , maxver: \n" );

}$t = "table1";

$row = "row_name";

$valid = "foobar-\xe7\x94\x9f\xe3\x83\x93";

$mutations = array(

new mutation( array(

'column' => 'entry:foo',

'value' => $valid

) ),

);$client->mutaterow( $t, $row, $mutations );

$table_name = "table1";

$row_name = 'row_name';

$fam_col_name = 'entry:foo';

$arr = $client->get($table_name, $row_name , $fam_col_name);

// $arr = array

foreach ( $arr as $k=>$v ) ,

");echo ("timestamp =

");}

$table_name = "table1";

$row_name = "row_name";

$arr = $client->getrow($table_name, $row_name);

// $client->getrow return a array

foreach ( $arr as $k=>$trowresult )

$transport->close();

?>

通過瀏覽器訪問http://localhost/hbasethrift/hbasethrift.php,如果顯示hbase中的表名與新建表table1 ,說明連線成功。

hbase thrift api 參考

參考

windows下C 通過Thrift操作HBase

4。在windows命令列執行如下這兩條命令 thrift 0.10.0.exe gen csharp tutorial.thrift thrift 0.10.0.exe gen csharp shared.thrift 執行完以後就會在當前目錄下生成乙個名為gen csharp的目錄,到時候需要把...

mybatis連線phoenix操作hbase

直接上 configuration public class hbaseconfig bean configurationproperties prefix spring.datasource.druid.hbase public datasource phoenixdatasource bean ...

使用C 通過Thrift訪問HBase

因為專案需要要為客戶程式提供c net的hbase訪問介面,而hbase並沒有提供原生的.net客戶端介面,可以通過啟動hbase的thrift服務來提供多語言支援。thrift介紹 把環境貼出來是因為不同版本的元件之間可能會存在相容性的問題 thrift安裝 傳送門 hbase 1.0.3 thr...