首先要先引用Thrift
1 | <? |
2 | //include thrift路徑 |
3 | $GLOBALS [ 'THRIFT_ROOT' ] = '/var/www/hbase/thrift' ; |
4 | require_once ( $GLOBALS [ 'THRIFT_ROOT' ]. '/Thrift.php' ); |
5 | require_once ( $GLOBALS [ 'THRIFT_ROOT' ]. '/transport/TSocket.php' ); |
6 | require_once ( $GLOBALS [ 'THRIFT_ROOT' ]. '/transport/TBufferedTransport.php' ); |
7 | require_once ( $GLOBALS [ 'THRIFT_ROOT' ]. '/protocol/TBinaryProtocol.php' ); |
8 | require_once ( $GLOBALS [ 'THRIFT_ROOT' ]. '/packages/Hbase/Hbase.php' ); |
9 |
10 | //建立連線 |
11 | $socket = new TSocket( 'johnson-cloud' , 9090 ); |
12 | $socket ->setSendTimeout( 10000 ); // Ten seconds |
13 | $socket ->setRecvTimeout( 20000 ); // Twenty seconds |
14 | $transport = new TBufferedTransport( $socket ); |
15 | $protocol = new TBinaryProtocol( $transport ); |
16 | $client = new HbaseClient( $protocol ); |
17 | //啟動 |
18 | $transport ->open(); |
19 |
20 |
21 | /*程式碼寫在這*/ |
22 |
23 | $transport ->close(); |
24 | ?> |
1.列出hbase 裡的所有 table
1 | <?php |
2 | echo ( "listing tables...\n" ); |
3 | $tables = $client ->getTableNames(); |
4 | sort( $tables ); |
5 | foreach ( $tables as $name ) |
6 | { |
7 | echo $name . "\n" ; |
8 | } |
9 | ?> |
2.刪除table
1 | <?php |
2 | $name = "test2" ; //要刪除的資料表 |
3 | if ( $client ->isTableEnabled( $name )) |
4 | { |
5 | echo "關閉" . $name . "資料表\n" ; |
6 | $client ->disableTable( $name ); |
7 | } |
8 | echo "刪除中...\n" ; |
9 | $client ->deleteTable( $name ); |
10 | echo "刪除完成" ; |
11 | ?> |
3.新增table
1 | <?php |
2 | //定義columns 的物件結構,一個Column需要new出一個ColumnDescriptor物件 |
3 | $columns = array ( |
4 | new ColumnDescriptor( array ( |
5 | 'name' => 'name:' |
6 | ) ), |
7 | new ColumnDescriptor( array ( |
8 | 'name' => 'scores:' , |
9 | ) ) |
10 | ); |
11 |
12 | $t = "test2" ; //Table name |
13 | echo ( "creating table: {$t}\n" ); |
14 | try |
15 | { |
16 | $client ->createTable( $t , $columns ); |
17 | } |
18 | catch ( AlreadyExists $ae ) |
19 | { |
20 | echo ( "WARN: {$ae->message}\n" ); |
21 | } |
22 | ?> |
4.列出table內的column family
1 | <?php |
2 | //列出 table內的column family |
3 | $t = "results" ; //資料表名稱 |
4 | echo ( "column families in {$t}:\n" ); |
5 | $descriptors = $client ->getColumnDescriptors( $t ); |
6 | asort( $descriptors ); |
7 | foreach ( $descriptors as $col ) |
8 | { |
9 | echo ( " column: {$col->name}, maxVer: {$col->maxVersions}\n" ); |
10 | } |
11 | ?> |
5.寫入資料
1 | <?php |
2 | $t = "results" ; //Table name |
3 | $row = "s96113106" ; //row name |
4 | $valid = "Tom" ; |
5 | $mutations = array ( |
6 | new Mutation( array ( |
7 | 'column' => 'name:' , |
8 | 'value' => $valid |
9 | ) ) |
10 | ); |
11 | $client ->mutateRow( $t , $row , $mutations ); |
12 | echo "新增成功" ; |
13 | ?> |
6.讀取資料,取得一個 column value
1 | <?php |
2 | include "ThriftLib.php" ; |
3 | $table_name = 'results' ; //Table name |
4 | $row_name = 's96113106' ; //row name |
5 | $fam_col_name = 'scores:math' ; //column name |
6 | $arr = $client ->get( $table_name , $row_name , $fam_col_name ); |
7 |
8 | foreach ( $arr as $k => $v ) |
9 | { |
10 | //// $k = TCell物件 |
11 | echo "value = {$v->value} , <br> " ; |
12 | echo "timestamp = {$v->timestamp} <br>" ; |
13 | } |
14 | ?> |