PHP - Thrift 0.9.0 - 在TSocket.php中找不到类'Thrift \ Transport \ TTransport'

时间:2013-03-11 18:13:34

标签: php hbase thrift thrift-protocol

尝试使用PHP与thrift,由于无法找到某些类,我无法正常运行它。我能够以0.8.0的速度做到这一点,现在我已经下载了0.9.0,我不知道应该如何正确地包含节俭文件。

以下是我的代码片段:

$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";
}

所有文件都在目录中正确布局:

HBase Includes - PHP 但是当我从命令行运行该文件(php -f index.php)时,我收到此错误:

  

未找到类'Thrift \ Transport \ TTransport'   第35行/home/user/final/Thrift/Transport/TSocket.php

我真的不知道接下来要做什么,我不熟悉在PHP中使用“use”命令或“命名空间”,我有一种感觉会帮助解决这个问题。 PHP的thrift README也提到了使用symfony,这让我更加困惑。

非常感谢任何建议!

谢谢!

3 个答案:

答案 0 :(得分: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);
            }

答案 1 :(得分:0)

使用ClassLoader。 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";
}

P.S。此代码适用于您的目录结构:

enter image description here

答案 2 :(得分:0)

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

节俭/基础 节俭/类加载器 Thrfit / xxxx

示例 我将整个Thrift文件夹放在htdocs \ thrift-0.9.3 \

然后它看起来像这个img enter image description here

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();