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接口文件
把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); // Ten seconds (too long for production, but this is just a demo ;)
- $socket->setRecvTimeout(20000); // Twenty seconds
- $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" );
- }
- //創建新表student
- $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);
- // $arr = array
- foreach ($arr as $k => $v) {
- // $k = TCell
- echo ("value = {$v->value} , <br> ");
- echo ("timestamp = {$v->timestamp} <br>");
- }
- $arr = $client->getRow($tableName, $row_name);
- // $client->getRow return a array
- foreach ($arr as $k => $TRowResult) {
- // $k = 0 ; non-use
- // $TRowResult = TRowResult
- var_dump($TRowResult);
- }
- $transport->close();
- ?>
通過瀏覽器查看看到項目中的所有表,證明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服務。