noSql-Hbase-php读写操作hbase数据库的基本方法
2016-04-12 21:49
706 查看
<?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);
}
?>
----------------------------------------------------
// 列出hbase 裡的所有 table
69 echo( "listing tables...\n" );
70 $tables = $client->getTableNames();
71 sort( $tables );
72 foreach ( $tables as $name )
73 {
74 echo $name."\n";
75 }
76
77 // 刪除table
78 $name = "test2";
79 if($client->isTableEnabled($name))
80 {
81 echo "关闭".$name."资料表\n";
82 $client->disableTable($name);
83 }
84 echo "刪除中...\n";
85 $client->deleteTable($name);
86 echo "刪除完成";
87
88 // 新增table
89 $columns = array(new ColumnDescriptor(array('name' => 'name:')),
90 new ColumnDescriptor(array('name'=> 'scores:',)));
91 $t = "test2";
92 echo("creating table: {$t}\n");
93 try
94 {
95 $client->createTable( $t, $columns );
96 }
97 catch (AlreadyExists $ae)
98 {
99 echo( "WARN: {$ae->message}\n" );
100 }
101
102 //列出table內的column family
103 $t = "results";
104 echo("column families in {$t}:\n");
105 $descriptors = $client->getColumnDescriptors( $t );
106 asort( $descriptors );
$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);
}
?>
----------------------------------------------------
// 列出hbase 裡的所有 table
69 echo( "listing tables...\n" );
70 $tables = $client->getTableNames();
71 sort( $tables );
72 foreach ( $tables as $name )
73 {
74 echo $name."\n";
75 }
76
77 // 刪除table
78 $name = "test2";
79 if($client->isTableEnabled($name))
80 {
81 echo "关闭".$name."资料表\n";
82 $client->disableTable($name);
83 }
84 echo "刪除中...\n";
85 $client->deleteTable($name);
86 echo "刪除完成";
87
88 // 新增table
89 $columns = array(new ColumnDescriptor(array('name' => 'name:')),
90 new ColumnDescriptor(array('name'=> 'scores:',)));
91 $t = "test2";
92 echo("creating table: {$t}\n");
93 try
94 {
95 $client->createTable( $t, $columns );
96 }
97 catch (AlreadyExists $ae)
98 {
99 echo( "WARN: {$ae->message}\n" );
100 }
101
102 //列出table內的column family
103 $t = "results";
104 echo("column families in {$t}:\n");
105 $descriptors = $client->getColumnDescriptors( $t );
106 asort( $descriptors );
相关文章推荐
- 四、编译安装php-5.5.34
- Thinkphp笔记3 模型
- PHP——数组_学习笔记
- ThinkPHP 笔记2 路由 控制器 视图
- ThinkPHP 笔记1 起步
- 分享3个国内可用速度较快的NTP服务器地址
- FTP之FileZilla Server
- PHP——函数二_学习笔记
- PHP——函数_学习笔记
- Phpstorm10 破解方法
- php增删改查等常用方法封装
- laravel创建服务提供者实例
- php 封装分页查询类
- mysql 启动失败 和 php路径错误 以及 php 无法显示的问题
- YII2 日志
- PHP 单例模式
- 嵌入式 busybox自带的tftp、telnet、ftp服务器
- 【PHP】定时计划没有执行PHP脚本以及解决方案
- PHP获取xml中值的几种方法简单总结
- PHP