资讯

精准传达 • 有效沟通

从品牌网站建设到网络营销策划,从策略到执行的一站式服务

php通过thrift操作hbase

 环境配置 

创新互联专注为客户提供全方位的互联网综合服务,包含不限于成都网站设计、网站建设、赞皇网络推广、成都小程序开发、赞皇网络营销、赞皇企业策划、赞皇品牌公关、搜索引擎seo、人物专访、企业宣传片、企业代运营等,从售前售中售后,我们都将竭诚为您服务,您的肯定,是我们最大的嘉奖;创新互联为所有大学生创业者提供赞皇建站搭建服务,24小时服务热线:13518219792,官方网址:www.cdcxhl.com

操作系统 centos 5.8    hadoop版本cloudera cdh4u3  hbase版本hbase-0.90.4-cdh4u3  php版本5.2

1.  下载并编译thrift

     # wget http://ftp.tc.edu.tw/pub/Apache/thrift/0.8.0/thrift-0.8.0.tar.gz

     安装所需的依赖包

     # yum install automake libtool flex bison pkgconfig gcc-c++ boost-devel libevent-devel zlib-devel python-devel ruby-devel php php-devel

   # tar zxvf  thrift-0.8.0.tar.gz

     # cd thrift-0.8.0

    #   ./configure --prefix=/home/thrift --with-php-config=/usr/bin/php-config

  # make && make install

2  生成php和hbase的接口文件:

    # cd /home/thrift/

   # bin/thrift  --gen php $HBASE_HOME/src/main/resources/org/apache/hadoop/hbase/thrift/Hbase.thrift

  # cd gen-php/Hbase

  # ls

Hbase.php  Hbase_types.php

3. 把PHP客户端需要的包及刚才生成的接口文件复制出来供php程序调用:

#  mkdir -p  /var/www/html/hbasethrift/libs     (/var/www/html为apache的web主目录)

#  cp -a /home/soft/thrift-0.8.0/lib/php/src /var/www/html/hbasethrift/libs     

#  mkdir -p /var/www/html/hbasethrift/libs/packages

#  cp -a /home/thrift/gen-php/Hbase /var/www/html/hbasethrift/libs/packages

4.  启动hbase thrift server,测试php连接hbase

  # ./bin/hbase-daemon.sh start thrift  

 hbase thrift 默认监听端口为9090

测试php连接与操作hbase代码 

# vi hbasethrift.php

 

  1. $GLOBALS['THRIFT_ROOT'] = '/home/www/html/hbasethrift/libs';  
  2. require_once( $GLOBALS['THRIFT_ROOT'].'/Thrift.php' );  
  3. require_once( $GLOBALS['THRIFT_ROOT'].'/transport/TSocket.php' );  
  4. require_once( $GLOBALS['THRIFT_ROOT'].'/transport/TBufferedTransport.php' );  
  5. require_once( $GLOBALS['THRIFT_ROOT'].'/protocol/TBinaryProtocol.php' );  
  6. require_once( $GLOBALS['THRIFT_ROOT'].'/packages/Hbase/Hbase.php' );  
  7. $socket = new TSocket( 'localhost', 9090 );  
  8. $socket->setSendTimeout( 10000 ); // Ten seconds (too long for production, but this is just a demo ;)  
  9. $socket->setRecvTimeout( 20000 ); // Twenty seconds  
  10. $transport = new TBufferedTransport( $socket );  
  11. $protocol = new TBinaryProtocol( $transport );  
  12. $client = new HbaseClient( $protocol );  
  13. $transport->open();  
  14. echo nl2br( "listing tables...\n" );  
  15. $tables = $client->getTableNames();  
  16. sort( $tables );  
  17. foreach ( $tables as $name ) {  
  18. echo nl2br( "  found: {$name}\n" );  
  19. }  
  20. $columns = array(  
  21. new ColumnDescriptor( array(  
  22. 'name' => 'entry:',  
  23. 'maxVersions' => 10  
  24. ) ),  
  25. new ColumnDescriptor( array(  
  26. 'name' => 'unused:'  
  27. ) )  
  28. );  
  29. $t = "table1";  
  30. echo( "creating table: {$t}\n" );  
  31. try {  
  32. $client->createTable( $t, $columns );  
  33. } catch ( AlreadyExists $ae ) {  
  34. echo( "WARN: {$ae->message}\n" );  
  35. }  
  36. $t = "test";  
  37. echo( "column families in {$t}:\n" );  
  38. $descriptors = $client->getColumnDescriptors( $t );  
  39. asort( $descriptors );  
  40. foreach ( $descriptors as $col ) {  
  41. echo( "  column: {$col->name}, maxVer: {$col->maxVersions}\n" );  
  42. }  
  43. $t = "table1";  
  44. echo( "column families in {$t}:\n" );  
  45. $descriptors = $client->getColumnDescriptors( $t );  
  46. asort( $descriptors );  
  47. foreach ( $descriptors as $col ) {  
  48. echo( "  column: {$col->name}, maxVer: {$col->maxVersions}\n" );  
  49. }  
  50. $t = "table1";  
  51. $row = "row_name";  
  52. $valid = "foobar-\xE7\x94\x9F\xE3\x83\x93";  
  53. $mutations = array(  
  54. new Mutation( array(  
  55. 'column' => 'entry:foo',  
  56. 'value' => $valid  
  57. ) ),  
  58. );  
  59. // 多记录批量提交(200提交一次时测试小记录大概在5000/s左右): $rows = array('timestamp'=>$timestamp, 'columns'=>array('txt:col1'=>$col1, 'txt:col2'=>$col2, 'txt:col3'=>$col3)); $records = array(rowkey=>$rows,...); $batchrecord = array(); foreach ($records as $rowkey => $rows) { $timestamp = $rows['timestamp']; $columns = $rows['columns']; // 生成一条记录 $record = array(); foreach($columns as $column => $value) { $col = new Mutation(array('column'=>$column, 'value'=>$value)); array_push($record, $col); } // 加入记录数组 $batchTmp = new BatchMutation(array('row'=>$rowkey, 'mutations'=>$record)); array_push($batchrecord, $batchTmp); } $ret = $hbase->mutateRows('test', $batchrecord);
  60.  
  61. $client->mutateRow( $t, $row, $mutations );  
  62. $table_name = "table1";  
  63. $row_name = 'row_name';  
  64. $fam_col_name = 'entry:foo';  
  65. $arr = $client->get($table_name, $row_name , $fam_col_name);  
  66. // $arr = array  
  67. foreach ( $arr as $k=>$v  ) {  
  68. // $k = TCell  
  69. echo ("value = {$v->value} , 
      ");  
  70. echo ("timestamp = {$v->timestamp}  
    ");  
  71. }  
  72. $table_name = "table1";  
  73. $row_name = "row_name";  
  74. $arr = $client->getRow($table_name, $row_name);  
  75. // $client->getRow return a array  
  76. foreach ( $arr as $k=>$TRowResult  ) {  
  77. // $k = 0 ; non-use  
  78. // $TRowResultTRowResult = TRowResult  
  79. var_dump($TRowResult);  
  80. }  
  81.  
  82. //scannerOpenWithStop($tableName, $startRow, $stopRow, $columns); 
  83. $table_name = 'zTest'; 
  84. $startRow="9-9-20120627-"; 
  85. $stopRow="9-9-20120627_"; 
  86. $columns = Array ('info:'); 
  87. $result =$client->scannerOpenWithStop($table_name,$startRow,$stopRow,$columns); 
  88. while (true) { 
  89.       $record = $client->scannerGet($result); 
  90.       if ($record == NULL) { 
  91.           break; 
  92.       } 
  93.       
  94.       foreach($record as $TRowResult) { 
  95.           $row = $TRowResult->row; 
  96.           $column = $TRowResult->columns; 
  97.            foreach($column as $family_column=>$Tcell){ 
  98.                 echo("$family_column={$Tcell->value}
    ");
  99. echo("timestamp is $Tcell->timestamp");
  100.             } 
  101.       } 
  102.   } 
  103. $transport->close();  
  104. ?>  

通过浏览器访问http://localhost/hbasethrift/hbasethrift.php,如果显示hbase中的表名与新建表table1 ,说明连接成功。

hbase thrift api 参考http://hbase.apache.org/apidocs/org/apache/hadoop/hbase/thrift/doc-files/index.html


参考http://www.banping.com/2011/07/08/hbase-thrift-php/

 


网站题目:php通过thrift操作hbase
网站路径:http://cdkjz.cn/article/ghgggp.html
多年建站经验

多一份参考,总有益处

联系快上网,免费获得专属《策划方案》及报价

咨询相关问题或预约面谈,可以通过以下方式与我们联系

大客户专线   成都:13518219792   座机:028-86922220