1

Trying out using PHP with thrift and I can't run it properly due to being unable to find certain classes. I was able to do this in 0.8.0 fine, now that I've downloaded 0.9.0 I'm at a loss as to how I should include the thrift files properly.

Here is my snippet of code:

$GLOBALS['THRIFT_ROOT'] = '/home/user/final/Thrift';
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( 'Hbase/Hbase.php');
require_once( 'Hbase/Types.php');

use Hbase\HbaseClient;

try
{
    $socket = new TSocket('127.0.0.1', 9090);
    $transport = new TBufferedTransport($socket, 1024, 1024);
    $protocol = new TBinaryProtocolAccelerated($transport);
    $client = new HbaseClient( $protocol );
    $transport->open();

    //show all tables
    $tables = $client->getTableNames();
    foreach ( $tables as $name )
    {
        echo( "  found: {$name}\n" );
    }
}
catch (Exception $e)
{
    echo "Exception: %e\r\n";
}

All files are layed out properly in the directories as seen here:

HBase Includes - PHP But when I run the file from the command line (php -f index.php) I'm receiving this error:

Class 'Thrift\Transport\TTransport' not found in /home/user/final/Thrift/Transport/TSocket.php on line 35

I'm really at a loss as to what I should do next, I'm not familiar with using the "use" command or "namespace" in PHP, which I have a feeling would help solve this. The thrift README for php also mentions using symfony, which is just further confusing me.

Any advice would be greatly appreciated!

Thanks!

4

3 回答 3

1

命名空间 像这样:

use Thrift\Transport\TSocket;
use Thrift\Transport\TBufferedTransport;
use Thrift\Protocol\TBinaryProtocolAccelerated;

    try {
                $socket = new TSocket('xxxxx', 9090);
                $transport = new TBufferedTransport($socket, 1024, 1024);
                $protocol = new TBinaryProtocolAccelerated($transport);
                $client = new HbaseClient($protocol);
                $transport -> open();

                //show all tables
                $tables = $client -> getTableNames();
                foreach ($tables as $name) {
                    echo("  found: {$name}\n");
                }
            } catch (Exception $e) {
                print_r($e);
            }
于 2013-11-09T22:50:14.553 回答
0

使用类加载器。Hbase 类不支持 classLoader,但使用 Thrift 命名空间。

<?php
define('THRIFT_PATH', __DIR__);

require_once THRIFT_PATH . '/Thrift/ClassLoader/ThriftClassLoader.php';

$classLoader = new Thrift\ClassLoader\ThriftClassLoader();
$classLoader->registerNamespace('Thrift', THRIFT_PATH);
$classLoader->register();

// (!) include after classLoader
require_once 'Hbase/Hbase.php';
require_once 'Hbase/Types.php';

try
{
    $socket = new Thrift\Transport\TSocket('127.0.0.1', 9090);
    $transport = new Thrift\Transport\TBufferedTransport($socket, 1024, 1024);
    $protocol = new Thrift\Protocol\TBinaryProtocolAccelerated($transport);
    $client = new Hbase\HbaseClient($protocol);
    $transport->open();

    //show all tables
    $tables = $client->getTableNames();
    foreach ($tables as $name)
    {
        echo "found: {$name}\n";
    }
}
catch (Exception $e)
{
    echo "Exception: %e\r\n";
}

PS 此代码适用于您的目录结构:

在此处输入图像描述

于 2014-11-14T14:37:55.600 回答
0

您需要保留 Thrift 父目录名,不要重命名它;像这样的目录结构

Thrift/Base Thrift/ClassLoader Thrfit/xxxx

例如我把整个 Thrift 文件夹放在 htdocs\thrift-0.9.3\

然后它看起来像这个img 在此处输入图像描述

php代码应该是这样的:

<?php
ini_set('display_error', E_ALL);
$THRIFT_ROOT = __DIR__.'/thrift-0.9.3';
require_once $THRIFT_ROOT . '/Thrift/ClassLoader/ThriftClassLoader.php';
use Thrift\ClassLoader\ThriftClassLoader;

$classLoader = new ThriftClassLoader();
$classLoader->registerNamespace('Thrift', $THRIFT_ROOT);
$classLoader->register();

use Thrift\Transport\TSocket;
use Thrift\Transport\TBufferedTransport;
use Thrift\Protocol\TBinaryProtocolAccelerated;

// (!) include after classLoader
/* Dependencies. In the proper order. */
require_once    './hbase-1.2.0-thrift2/THBaseService.php';
require_once    './hbase-1.2.0-thrift2/Types.php';

$host = 'xxxxx';
$port = 19090;
$tableName='test';
$rowKey='b';

$socket = new TSocket($host, $port);
$transport = new TBufferedTransport($socket);
$protocol = new TBinaryProtocolAccelerated($transport);
$client = new THBaseServiceClient($protocol);
$transport->open();

$get = new TGet();
$get->row = $rowKey;

$arr = $client->get($tableName, $get);
$data = array();
$results = $arr->columnValues;
foreach($results as $result)
{
    $qualifier = (string)$result->qualifier;
    $value = $result->value;
    $data[$qualifier] = $value;
}
var_dump($data);
$transport->close();
于 2020-03-17T09:04:57.110 回答