HBase是一個開源的NoSQL產品,它是實現了Google BigTable論文的一個開源產品,和Hadoop和HDFS一塊兒,可用來存儲和處理海量column family的數據。官方網址是:http://hbase.apache.orgphp
一 、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的中間件,用於不一樣系統語言間信息交換。
2、安裝Thrift
在Hadoop和Hbase都已經安裝好的集羣上安裝Thrift,Thrift安裝在Hmaster機器上web
1. 下載thrift
wget http://mirror.bjtu.edu.cn/apache//thrift/0.8.0/thrift-0.8.0.tar.gzapache
2. 解壓
tar -xzf thrift-0.8.0.tar.gz編程
3 .編譯安裝:
若是是源碼編譯的,首先要使用./boostrap.sh建立文件./configure ,咱們這下載的tar包,自帶有configure文件了。((能夠查閱README文件))bootstrap
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.shubuntu
./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.outdom
Thrift默認監聽的端口是9090socket
使用jps查看進程,看到ThriftServer進程:編程語言
3、測試:
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
-
- ini_set('display_errors', E_ALL);
- $GLOBALS['THRIFT_ROOT'] = './php/src';
-
- 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('10.64.60.83', '9090');
-
- $socket->setSendTimeout(10000);
- $socket->setRecvTimeout(20000);
- $transport = new TBufferedTransport($socket);
- $protocol = new TBinaryProtocol($transport);
- $client = new HbaseClient($protocol);
-
- $transport->open();
-
- $tables = $client->getTableNames();
- sort($tables);
- foreach ($tables as $name) {
-
- echo( " found: {$name}\n" );
- }
-
- $columns = array(
- new ColumnDescriptor(array(
- 'name' => 'id:',
- 'maxVersions' => 10
- )),
- new ColumnDescriptor(array(
- 'name' => 'name:'
- )),
- new ColumnDescriptor(array(
- 'name' => 'score:'
- )),
- );
-
- $tableName = "student";
- try {
- $client->createTable($tableName, $columns);
- } catch (AlreadyExists $ae) {
- echo( "WARN: {$ae->message}\n" );
- }
-
- $descriptors = $client->getColumnDescriptors($tableName);
- asort($descriptors);
- foreach ($descriptors as $col) {
- echo( " column: {$col->name}, maxVer: {$col->maxVersions}\n" );
- }
-
- $row = '2';
- $valid = "foobar-\xE7\x94\x9F\xE3\x83\x93";
- $mutations = array(
- new Mutation(array(
- 'column' => 'score',
- 'value' => $valid
- )),
- );
- $client->mutateRow($tableName, $row, $mutations);
-
-
- $row_name = '2';
- $fam_col_name = 'score';
- $arr = $client->get($tableName, $row_name, $fam_col_name);
- foreach ($arr as $k => $v) {
- echo ("value = {$v->value} , <br> ");
- echo ("timestamp = {$v->timestamp} <br>");
- }
-
- $arr = $client->getRow($tableName, $row_name);
- foreach ($arr as $k => $TRowResult) {
- var_dump($TRowResult);
- }
-
- $transport->close();
- ?>
經過瀏覽器查看看到項目中的全部表,證實PHP能夠經過thrift訪問HBase了。
2. 使用PHP擴展的方式來使用thrift
咱們使用PHP自帶的phpize來生成Thtift的php擴展。該擴展的源碼結構:
![](http://static.javashuo.com/static/loading.gif)
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服務。