php读写操作hbase数据库的基本方法

php读写操作hbase数据库的基本方法 

<?php

$GLOBALS['THRIFT_ROOT'] = '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( '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 "start ...\r\n";
echo "check table exist\r\n";

$t = 'test_table';
$table = $client->getTableNames();

print_r($table);

/**
foreach ($table as $value){
        if($value == $t){
                echo "find $t,delete it\r\n";
                if($client->isTableEnabled($value)){
                        echo "disabling $t\r\n";
                        $client->disableTable($value);
                }
                echo "deleting $t\r\n";
                $client->deleteTable($value);
        }
}

echo "Create new table $t\r\n";

$aritcle = new ColumnDescriptor(array('name'=>'aritcle:'));
$author = new ColumnDescriptor(array('name'=>'author:'));
$columns = array($aritcle,$author);

echo "Creating table $t\r\n";
try {
        $client->createTable($t,$columns);
} catch (AlreadyExists $ae){
        echo "$ae\r\n";
}

**/

echo "Start insert some records\r\n";

$record1 = array(new Mutation(array('column'=>'aritcle:title','value'=>'hello,world!')));
$record2 = array(new Mutation(array('column'=>'aritcle:content','value'=>'welcome to hbase')));

$client->mutateRow($t,'1',$record1);
$client->mutateRow($t,'1',$record2);
/**
echo "In sert 1000 records\r\n";
$time_start = microtime_float();

for ($i=0;$i<10000;$i++){
        $record = array(new Mutation(array('column'=>'aritcle:title','value'=>$i)));
        $client->mutateRow($t,$i,$record);
}

$time_end = microtime_float();
$time = $time_end - $time_start;

echo "Did nothing in $time seconds\n";

echo "rand read row \r\n";
**/
$row = $client->getRow($t,rand(1,10000));
print_r($row);

$row = $client->getRowWithColumns($t,1,'aritcle:');
print_r($row);

$transport->close();

function microtime_float()
{
    list($usec, $sec) = explode(" ", microtime());
    return ((float)$usec + (float)$sec);
}

?>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值