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腳本測試:

  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}\n" );  
  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}\n" );  
  49. }  
  50. //獲取表的描述  
  51.   
  52. $descriptors = $client->getColumnDescriptors($tableName);  
  53. asort($descriptors);  
  54. foreach ($descriptors as $col) {  
  55.     echo"  column: {$col->name}, maxVer: {$col->maxVersions}\n" );  
  56. }  
  57.   
  58. //修改表列的數據  
  59. $row = '2';  
  60. $valid = "foobar-\xE7\x94\x9F\xE3\x83\x93";  
  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服務。
發佈了19 篇原創文章 · 獲贊 5 · 訪問量 17萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章