php通过thrift操作hbase

本文详细介绍如何在CentOS 5.8环境下搭建HBase与PHP的交互环境,包括Thrift的编译安装、生成PHP与HBase的接口文件、复制相关文件到web目录以及测试PHP连接与操作HBase的步骤。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

环境配置

操作系统 centos 5.8    hadoop版本cloudera cdh3u3  hbase版本hbase-0.90.4-cdh3u3  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

 

01. <?php
02. $GLOBALS['THRIFT_ROOT'] = '/home/www/html/hbasethrift/libs';
03. require_once$GLOBALS['THRIFT_ROOT'].'/Thrift.php' );
04. require_once$GLOBALS['THRIFT_ROOT'].'/transport/TSocket.php' );
05. require_once$GLOBALS['THRIFT_ROOT'].'/transport/TBufferedTransport.php' );
06. require_once$GLOBALS['THRIFT_ROOT'].'/protocol/TBinaryProtocol.php' );
07. require_once$GLOBALS['THRIFT_ROOT'].'/packages/Hbase/Hbase.php' );
08. $socket new TSocket( 'localhost', 9090 );
09. $socket->setSendTimeout( 10000 ); // Ten seconds (too long for production, but this is just a demo ;)
10. $socket->setRecvTimeout( 20000 ); // Twenty seconds
11. $transport new TBufferedTransport( $socket );
12. $protocol new TBinaryProtocol( $transport );
13. $client new HbaseClient( $protocol );
14. $transport->open();
15. echo nl2br"listing tables...\n" );
16. $tables $client->getTableNames();
17. sort( $tables );
18. foreach $tables as $name ) {
19. echo nl2br"  found: {$name}\n" );
20. }
21. $columns array(
22. new ColumnDescriptor( array(
23. 'name' => 'entry:',
24. 'maxVersions' => 10
25. ) ),
26. new ColumnDescriptor( array(
27. 'name' => 'unused:'
28. ) )
29. );
30. $t "table1";
31. echo"creating table: {$t}\n" );
32. try {
33. $client->createTable( $t$columns );
34. } catch ( AlreadyExists $ae ) {
35. echo"WARN: {$ae->message}\n" );
36. }
37. $t "test";
38. echo"column families in {$t}:\n" );
39. $descriptors $client->getColumnDescriptors( $t );
40. asort( $descriptors );
41. foreach $descriptors as $col ) {
42. echo"  column: {$col->name}, maxVer: {$col->maxVersions}\n" );
43. }
44. $t "table1";
45. echo"column families in {$t}:\n" );
46. $descriptors $client->getColumnDescriptors( $t );
47. asort( $descriptors );
48. foreach $descriptors as $col ) {
49. echo"  column: {$col->name}, maxVer: {$col->maxVersions}\n" );
50. }
51. $t "table1";
52. $row "row_name";
53. $valid "foobar-\xE7\x94\x9F\xE3\x83\x93";
54. $mutations array(
55. new Mutation( array(
56. 'column' => 'entry:foo',
57. 'value' => $valid
58. ) ),
59. );
60. $client->mutateRow( $t$row$mutations );
61. $table_name "table1";
62. $row_name 'row_name';
63. $fam_col_name 'entry:foo';
64. $arr $client->get($table_name$row_name $fam_col_name);
65. // $arr = array
66. foreach $arr as $k=>$v  ) {
67. // $k = TCell
68. echo ("value = {$v->value} , <br>  ");
69. echo ("timestamp = {$v->timestamp}  <br>");
70. }
71. $table_name "table1";
72. $row_name "row_name";
73. $arr $client->getRow($table_name$row_name);
74. // $client->getRow return a array
75. foreach $arr as $k=>$TRowResult  ) {
76. // $k = 0 ; non-use
77. // $TRowResult = TRowResult
78. var_dump($TRowResult);
79. }
80. $transport->close();
81. ?>

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值