php通過thrift訪問HBase 二
阿新 • • 發佈:2018-12-23
[[email protected] html]# cat hbase_code.php <?php # author: zhouhh # date: 2012.10.17 # common file # # Change this to match your thrift root $GLOBALS['ROOT'] = '/var/www/html'; $GLOBALS['THRIFT_ROOT'] = '/var/www/html/src'; $GLOBALS['TRIFTSVR']='hadoop46'; $GLOBALS['TRIFTSVR_PORT']=9090; #require_once 'KLogger.php'; #$log = new KLogger ( $GLOBALS['ROOT']."/mylog" , KLogger::DEBUG ); $tableName="a_rule"; 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' ); # According to the thrift documentation, compiled PHP thrift libraries should # reside under the THRIFT_ROOT/packages directory. If these compiled libraries # are not present in this directory, move them there from gen-php/. require_once( $GLOBALS['THRIFT_ROOT'].'/packages/Hbase/Hbase.php' ); $socket = new TSocket( $GLOBALS['TRIFTSVR'], $GLOBALS['TRIFTSVR_PORT'] ); $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 ); function listTable( ) { global $client; #echo( "listing tables...n" ); $tables = $client->getTableNames(); sort( $tables ); printTables($tables); return $tables; } #$columnsArray = array( # new ColumnDescriptor( array( # 'name' => 'entry:', # 'maxVersions' => 3 # ) ), # new ColumnDescriptor( array( # 'name' => 'info:' # ) ) #); function createTable($tableName, $columnsArray) { global $client; echo( "creating table: {$tableName}n" ); try { $client->createTable( $tableName, $columnsArray ); } catch ( AlreadyExists $ae ) { echo( "WARN: {$ae->message}n" ); } } function getColumnDesc($tableName) { global $client; echo( "column families in {$tableName}:n" ); $descriptors = $client->getColumnDescriptors( $tableName ); asort( $descriptors ); foreach ( $descriptors as $col ) { echo( "column: {$col->name}, maxVer: {$col->maxVersions}n" ); } } function getRows($tableName,$rowNames){ global $client; #echo $rowNames; $get_arr = $client->getRows($tableName, $rowNames, null); if(!empty($get_arr)) { #print_r($get_arr); foreach ( $get_arr as $rowresult ){ printRow($rowresult); } } else { echo("get nothing of $rowNames from $tableName"); } } function getRow($tableName,$rowName){ global $client; echo $rowName; $get_arr = $client->getRow($tableName, $rowName, null); if(!empty($get_arr)) { #print_r($get_arr); foreach ( $get_arr as $rowresult ){ printRow($rowresult); } } else { echo("get nothing of $rowName from $tableName"); } } function printTables($tables){ foreach ( $tables as $name ) { echo( "t{$name}n" ); } } function printRow( $rowresult ) { echo( "{$rowresult->row}n" ); $values = $rowresult->columns; asort( $values ); foreach ( $values as $k=>$v ) { #echo ("column=$k,"); echo( "t{$k}=>{$v->value}," ); echo ("ttimestamp={$v->timestamp}n"); } } function scanTable($tableName,$startRow,$columnArray,$count=1000) { global $client; echo( "Starting scanner of $tableName...n" ); $scanner = $client->scannerOpen( $tableName, $startRow, $columnArray, null); try { $c=0; while ($c < $count){ $get_arr = $client->scannerGetList($scanner,1); // get_arr is an array if($get_arr == null) break; $c+=1; foreach ( $get_arr as $rowresult ){ printRow($rowresult); } } $client->scannerClose( $scanner ); echo( "Scanner finished of $tableNamen" ); } catch ( NotFound $nf ) { $client->scannerClose( $scanner ); echo( "Scanner finishedn" ); } } # $filter="RowFilter(=, 'regexstring:00[1-3]00')"; # $filter="PrefixFilter('aaa')"; # $scan = new TScan(array("filterString" => $filter)); function scanTableWithFilter($tableName,$filter,$count=1000) { global $client; echo( "Starting scanner of $tableName...n" ); $scan = new TScan(); $scan->filterString=$filter; $scanner = $client->scannerOpenWithScan( $tableName, $scan, null); try { $c=0; while ($c < $count){ $get_arr = $client->scannerGetList($scanner,1); // get_arr is an array if($get_arr == null) break; $c+=1; foreach ( $get_arr as $rowresult ){ printRow($rowresult); } } $client->scannerClose( $scanner ); echo( "Scanner finished of $tableNamen" ); } catch ( NotFound $nf ) { $client->scannerClose( $scanner ); echo( "Scanner finishedn" ); } } ?>