2011年8月15日 星期一

[Hbase]使用PHP存取Hbase


首先必須與Hbase建立連線
<?php
header ("Content-Type:text/html; charset=utf-8");
//include thrift路徑
$GLOBALS['THRIFT_ROOT'] = '/var/www/hbase/thrift';
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( 'YourIP', 9090 );
$socket->setSendTimeout( 10000 ); // Ten seconds
$socket->setRecvTimeout( 20000 ); // Twenty seconds
$transport = new TBufferedTransport( $socket );
$protocol = new TBinaryProtocol( $transport );
$client = new HbaseClient( $protocol );

//啟動
$transport->open();

//程式碼寫在這邊

$transport->close();
?>
接下來是對資料庫會使用到的基本操作

1.新增資料表
        //定義columns 的物件結構,一個Column需要new出一個ColumnDescriptor物件
        $columns = array(
                new ColumnDescriptor( array(
                        'name' => 'name:'
                ) ),
                new ColumnDescriptor( array(
                        'name'=> 'scores:',
                ) )
        );


        $t = "test";//Table name
        echo( "creating table: {$t}\n" );
        try
        {
                $client->createTable( $t, $columns );
        }
        catch ( AlreadyExists $ae )
        {
                echo( "WARN: {$ae->message}\n" );
        }

2.查閱所有資料表
        echo( "全部的資料表有...\n" );
        $tables = $client->getTableNames();
        sort( $tables );
        foreach ( $tables as $name )
        {
                echo $name."\n";
        }
3.列出 table內的column family
        $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" );
        }
4.新增資料
        $t = "test"; //Table name
        $row = "abc"; //row name
        $valid = "apple";
        $mutations = array(
        new Mutation( array(
                'column' => 'nama:',
                'value' => $valid
                 ) )
        );
        $client->mutateRow( $t, $row, $mutations );
        echo "新增成功";
5.讀取資料
        include "ThriftLib.php";
        $table_name = 'test';//Table name
        $row_name = 'abc';//row name
        $fam_col_name = 'name:';//column name
        $arr = $client->get($table_name, $row_name , $fam_col_name);
        foreach ( $arr as $k=>$v )
        {
                //// $k = TCell物件
                echo "value = {$v->value} , <br> ";
                echo "timestamp = {$v->timestamp} <br>";
        }
6.刪除資料表
        $name = "test"; //要刪除的資料表
        if($client->isTableEnabled($name))
        {
                echo "關閉".$name."資料表\n";
                $client->disableTable($name);
        }
        echo "刪除中...\n";
        $client->deleteTable($name);
        echo "刪除完成";
有問題再留言吧@@

沒有留言:

張貼留言