search
尋找貓咪~QQ 地點 桃園市桃園區 Taoyuan , Taoyuan

[Hadoop]PHP access HBase – 佛祖球球

請先參考HBase + Thrift + PHP

首先要先引用Thrift

        //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( 'johnson-cloud', 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.列出hbase 裡的所有 table

getTableNames();
        sort( $tables );
        foreach ( $tables as $name )
        {
                echo $name."\n";
        }
?>

2.刪除table

isTableEnabled($name))
        {
                echo "關閉".$name."資料表\n";
                $client->disableTable($name);
        }
        echo "刪除中...\n";
        $client->deleteTable($name);
        echo "刪除完成";
?>

3.新增table

 'name:'
                ) ),
                new ColumnDescriptor( array(
                        'name'=> 'scores:',
                ) )
        );

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

4.列出table內的column family

getColumnDescriptors( $t );
        asort( $descriptors );
        foreach ( $descriptors as $col )
        {
                echo( " column: {$col->name}, maxVer: {$col->maxVersions}\n" );
        }
?>

5.寫入資料

 'name:',
                'value' => $valid
                 ) )
        );
        $client->mutateRow( $t, $row, $mutations );
        echo "新增成功";
?>

6.讀取資料,取得一個 column value

get($table_name, $row_name , $fam_col_name);

        foreach ( $arr as $k=>$v )
        {
                //// $k = TCell物件
                echo "value = {$v->value} , 
"; echo "timestamp = {$v->timestamp}
"; } ?>
Categories: HadoopPHP



熱門推薦

本文由 blogjohnsonluorg 提供 原文連結

寵物協尋 相信 終究能找到回家的路
寫了7763篇文章,獲得2次喜歡
留言回覆
回覆
精彩推薦