三、测试:
1 .php脚本库操作Hbase
PHP通过Thrift访问Hbase的库是在thrift-0.8.0/lib/php/src目录下,其实这个文件夹下也包含通过Thrift访问Hbase的PHP扩展源代码。
复制thrift-0.8.0/lib/php到相应的php web目录。然后使用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();
- ?>
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服务。
