環(huán)境配置
創(chuàng)新互聯(lián)專注為客戶提供全方位的互聯(lián)網(wǎng)綜合服務(wù),包含不限于成都網(wǎng)站設(shè)計(jì)、網(wǎng)站建設(shè)、贊皇網(wǎng)絡(luò)推廣、成都小程序開發(fā)、贊皇網(wǎng)絡(luò)營銷、贊皇企業(yè)策劃、贊皇品牌公關(guān)、搜索引擎seo、人物專訪、企業(yè)宣傳片、企業(yè)代運(yùn)營等,從售前售中售后,我們都將竭誠為您服務(wù),您的肯定,是我們最大的嘉獎;創(chuàng)新互聯(lián)為所有大學(xué)生創(chuàng)業(yè)者提供贊皇建站搭建服務(wù),24小時(shí)服務(wù)熱線:13518219792,官方網(wǎng)址:muchs.cn
操作系統(tǒng) centos 5.8 hadoop版本cloudera cdh4u3 hbase版本hbase-0.90.4-cdh4u3 php版本5.2
1. 下載并編譯thrift
# wget http://ftp.tc.edu.tw/pub/Apache/thrift/0.8.0/thrift-0.8.0.tar.gz
安裝所需的依賴包
# 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客戶端需要的包及剛才生成的接口文件復(fù)制出來供php程序調(diào)用:
# 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 默認(rèn)監(jiān)聽端口為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 ) {
- echo nl2br( " found: {$name}\n" );
- }
- $columns = array(
- new ColumnDescriptor( array(
- 'name' => 'entry:',
- 'maxVersions' => 10
- ) ),
- new ColumnDescriptor( array(
- 'name' => 'unused:'
- ) )
- );
- $t = "table1";
- echo( "creating table: {$t}\n" );
- try {
- $client->createTable( $t, $columns );
- } catch ( AlreadyExists $ae ) {
- echo( "WARN: {$ae->message}\n" );
- }
- $t = "test";
- echo( "column families in {$t}:\n" );
- $descriptors = $client->getColumnDescriptors( $t );
- asort( $descriptors );
- foreach ( $descriptors as $col ) {
- echo( " column: {$col->name}, maxVer: {$col->maxVersions}\n" );
- }
- $t = "table1";
- echo( "column families in {$t}:\n" );
- $descriptors = $client->getColumnDescriptors( $t );
- asort( $descriptors );
- foreach ( $descriptors as $col ) {
- echo( " column: {$col->name}, maxVer: {$col->maxVersions}\n" );
- }
- $t = "table1";
- $row = "row_name";
- $valid = "foobar-\xE7\x94\x9F\xE3\x83\x93";
- $mutations = array(
- new Mutation( array(
- 'column' => 'entry:foo',
- 'value' => $valid
- ) ),
- );
- // 多記錄批量提交(200提交一次時(shí)測試小記錄大概在5000/s左右): $rows = array('timestamp'=>$timestamp, 'columns'=>array('txt:col1'=>$col1, 'txt:col2'=>$col2, 'txt:col3'=>$col3)); $records = array(rowkey=>$rows,...); $batchrecord = array(); foreach ($records as $rowkey => $rows) { $timestamp = $rows['timestamp']; $columns = $rows['columns']; // 生成一條記錄 $record = array(); foreach($columns as $column => $value) { $col = new Mutation(array('column'=>$column, 'value'=>$value)); array_push($record, $col); } // 加入記錄數(shù)組 $batchTmp = new BatchMutation(array('row'=>$rowkey, 'mutations'=>$record)); array_push($batchrecord, $batchTmp); } $ret = $hbase->mutateRows('test', $batchrecord);
- $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 ) {
- // $k = TCell
- echo ("value = {$v->value} , <br> ");
- echo ("timestamp = {$v->timestamp} <br>");
- }
- $table_name = "table1";
- $row_name = "row_name";
- $arr = $client->getRow($table_name, $row_name);
- // $client->getRow return a array
- foreach ( $arr as $k=>$TRowResult ) {
- // $k = 0 ; non-use
- // $TRowResultTRowResult = TRowResult
- var_dump($TRowResult);
- }
- //scannerOpenWithStop($tableName, $startRow, $stopRow, $columns);
- $table_name = 'zTest';
- $startRow="9-9-20120627-";
- $stopRow="9-9-20120627_";
- $columns = Array ('info:');
- $result =$client->scannerOpenWithStop($table_name,$startRow,$stopRow,$columns);
- while (true) {
- $record = $client->scannerGet($result);
- if ($record == NULL) {
- break;
- }
- foreach($record as $TRowResult) {
- $row = $TRowResult->row;
- $column = $TRowResult->columns;
- foreach($column as $family_column=>$Tcell){
- echo("$family_column={$Tcell->value}<br>");
- echo("timestamp is $Tcell->timestamp");
- }
- }
- }
- $transport->close();
- ?>
通過瀏覽器訪問http://localhost/hbasethrift/hbasethrift.php,如果顯示hbase中的表名與新建表table1 ,說明連接成功。
hbase thrift api 參考http://hbase.apache.org/apidocs/org/apache/hadoop/hbase/thrift/doc-files/index.html
參考http://www.banping.com/2011/07/08/hbase-thrift-php/
當(dāng)前題目:php通過thrift操作hbase
網(wǎng)站鏈接:http://muchs.cn/article18/ghgggp.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供自適應(yīng)網(wǎng)站、網(wǎng)站建設(shè)、軟件開發(fā)、網(wǎng)站導(dǎo)航、微信公眾號、網(wǎng)站收錄
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)