zoukankan      html  css  js  c++  java
  • PHP通过Thrift操作Hbase

    PHP通过Thrift操作Hbase
     
     

    HBase是一个开源的NoSQL产品,它是实现了Google BigTable论文的一个开源产品,和Hadoop和HDFS一起,可用来存储和处理海量column family的数据。官方网址是:http://hbase.apache.org

    一 、HBase访问接口

    1.  Native Java API,最常规和高效的访问方式,适合Hadoop MapReduce Job并行批处理HBase表数据
    2.  HBase Shell,HBase的命令行工具,最简单的接口,适合HBase管理使用
    3.  Thrift Gateway,利用Thrift序列化技术,支持C++,PHP,Python等多种语言,适合其他异构系统在线访问HBase表数据
    4.  REST Gateway,支持REST 风格的Http API访问HBase, 解除了语言限制
    5.  Pig,可以使用Pig Latin流式编程语言来操作HBase中的数据,和Hive类似,本质最终也是编译成MapReduce Job来处理HBase表数据,适合做数据统计
    6.  Hive,当前Hive的Release版本尚没有加入对HBase的支持,但在下一个版本Hive 0.7.0中将会支持HBase,可以使用类似SQL语言来访问HBase
    如果使用PHP操作Hbase,推荐使用Facebook开源出来的thrift,官网是:http://thrift.apache.org/ ,它是一个类似ice的中间件,用于不同系统语言间信息交换。

    二、安装Thrift

    在Hadoop和Hbase都已经安装好的集群上安装Thrift,Thrift安装在Hmaster机器上

    1. 下载thrift

    wget http://mirror.bjtu.edu.cn/apache//thrift/0.8.0/thrift-0.8.0.tar.gz

    2. 解压

    tar -xzf thrift-0.8.0.tar.gz

    3 .编译安装:

    如果是源码编译的,首先要使用./boostrap.sh创建文件./configure ,我们这下载的tar包,自带有configure文件了。((可以查阅README文件))

    If you are building from the first time out of the source repository, you will
    need to generate the configure scripts.  (This is not necessary if you
    downloaded a tarball.)  From the top directory, do:
    ./bootstrap.sh

    ./configure
    make ; make install

    4. 启动:

    # ./bin/hbase-daemon.sh start thrift [--port=PORT]
    starting thrift, logging to /home/banping/hbase/hbase-0.90.3/bin/../logs/hbase-root-thrift-localhost.localdomain.out

    Thrift默认监听的端口是9090

    使用jps查看进程,看到ThriftServer进程:

    三、测试:

    1 .php脚本库操作Hbase

    PHP通过Thrift访问Hbase的库是在thrift-0.8.0/lib/php/src目录下,其实这个文件夹下也包含通过Thrift访问Hbase的PHP扩展源代码。

    1)复制thrift-0.8.0/lib/php到相应的php web目录。

    2)然后生成php与hbase接口文件

      #/usr/local/thrift/bin/thrift --gen php /usr/local/hbase/src/main/resources/org/apache/hadoop/hbase/thrift/Hbase.thrift
      #(根据自己的目录设置)
     
       生成目录文件: /usr/local/hbase/gen-php/Hbase
       有文件: Hbase.php,Hbase_types.php

       把Hbase.php,Hbase_types.php copy到:web目录/php/src/packages/Hbase/

    3)使用php脚本测试:

    [php] view plain copy
     
     print?
    1. <?php  
    2.   
    3. ini_set('display_errors', E_ALL);  
    4. $GLOBALS['THRIFT_ROOT'] = './php/src';  
    5.   
    6. require_once( $GLOBALS['THRIFT_ROOT'] . '/Thrift.php' );  
    7. require_once( $GLOBALS['THRIFT_ROOT'] . '/transport/TSocket.php' );  
    8. require_once( $GLOBALS['THRIFT_ROOT'] . '/transport/TBufferedTransport.php' );  
    9. require_once( $GLOBALS['THRIFT_ROOT'] . '/protocol/TBinaryProtocol.php' );  
    10. require_once( $GLOBALS['THRIFT_ROOT'] . '/packages/Hbase/Hbase.php' );  
    11.   
    12. $socket = new TSocket('10.64.60.83', '9090');  
    13.   
    14. $socket->setSendTimeout(10000); // Ten seconds (too long for production, but this is just a demo ;)  
    15. $socket->setRecvTimeout(20000); // Twenty seconds  
    16. $transport = new TBufferedTransport($socket);  
    17. $protocol = new TBinaryProtocol($transport);  
    18. $client = new HbaseClient($protocol);  
    19.   
    20. $transport->open();  
    21.   
    22. //获取表列表  
    23. $tables = $client->getTableNames();  
    24. sort($tables);  
    25. foreach ($tables as $name) {  
    26.   
    27.     echo( "  found: {$name} " );  
    28. }  
    29.    
    30. //创建新表student  
    31. $columns = array(  
    32.     new ColumnDescriptor(array(  
    33.         'name' => 'id:',  
    34.         'maxVersions' => 10  
    35.     )),  
    36.     new ColumnDescriptor(array(  
    37.         'name' => 'name:'  
    38.     )),  
    39.     new ColumnDescriptor(array(  
    40.         'name' => 'score:'  
    41.     )),  
    42. );  
    43.   
    44. $tableName = "student";  
    45. try {  
    46.     $client->createTable($tableName, $columns);  
    47. } catch (AlreadyExists $ae) {  
    48.     echo( "WARN: {$ae->message} " );  
    49. }  
    50. //获取表的描述  
    51.   
    52. $descriptors = $client->getColumnDescriptors($tableName);  
    53. asort($descriptors);  
    54. foreach ($descriptors as $col) {  
    55.     echo( "  column: {$col->name}, maxVer: {$col->maxVersions} " );  
    56. }  
    57.   
    58. //修改表列的数据  
    59. $row = '2';  
    60. $valid = "foobar-xE7x94x9FxE3x83x93";  
    61. $mutations = array(  
    62.     new Mutation(array(  
    63.         'column' => 'score',  
    64.         'value' => $valid  
    65.     )),  
    66. );  
    67. $client->mutateRow($tableName, $row, $mutations);  
    68.   
    69.   
    70. //获取表列的数据  
    71. $row_name = '2';  
    72. $fam_col_name = 'score';  
    73. $arr = $client->get($tableName, $row_name, $fam_col_name);  
    74. // $arr = array  
    75. foreach ($arr as $k => $v) {  
    76. // $k = TCell  
    77.     echo ("value = {$v->value} , <br>  ");  
    78.     echo ("timestamp = {$v->timestamp}  <br>");  
    79. }  
    80.   
    81. $arr = $client->getRow($tableName, $row_name);  
    82. // $client->getRow return a array  
    83. foreach ($arr as $k => $TRowResult) {  
    84. // $k = 0 ; non-use  
    85. // $TRowResult = TRowResult  
    86.     var_dump($TRowResult);  
    87. }  
    88.   
    89. $transport->close();  
    90. ?>  

    通过浏览器查看看到项目中的所有表,证明PHP可以通过thrift访问HBase了。

    2. 使用PHP扩展的方式来使用thrift

    我们使用PHP自带的phpize来生成Thtift的php扩展。该扩展的源码结构:

    hadoop@ubuntu:/usr/local/hbase-0.90.4/thrift-0.8.0/lib/php/src
    $ cd ext/thrift_protocol
    $ /usr/local/php/bin/phpize
    $ ./configure --with-php-config=/usr/local/php/bin/php-config --enable-thrift_protocol
    $ make
    $ make install

    然后把生成的thrift_protocol.so文件配置到php.ini并重启apache服务。
  • 相关阅读:
    Jwt访问api提示401错误 Authorization has been denied for this request
    git commit的规范
    postman中如何使用OAuth
    在outlook中查找Skype的聊天记录
    nuget sources
    NuGet version
    Forcing restore from package sources
    同时打印多个worksheets
    Redis使用认证密码登录
    Linux wait函数详解
  • 原文地址:https://www.cnblogs.com/timssd/p/5448501.html
Copyright © 2011-2022 走看看