【问题标题】:PHP - Thrift 0.9.0 - Class 'Thrift\Transport\TTransport' not found in TSocket.phpPHP - Thrift 0.9.0 - 在 TSocket.php 中找不到类“Thrift\Transport\TTransport”
【发布时间】:2023-05-18 09:46:01
【问题描述】:

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

这是我的sn-p代码:

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

所有文件都在目录中正确布局,如下所示:

但是当我从命令行(php -f index.php)运行文件时,我收到了这个错误:

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

我真的不知道接下来应该做什么,我不熟悉在 PHP 中使用“use”命令或“namespace”,我觉得这将有助于解决这个问题。 php 的节俭自述文件也提到了使用 symfony,这让我更加困惑。

任何建议将不胜感激!

谢谢!

【问题讨论】:

  • 这是因为 thrift 0.9.0 php 使用命名空间,您必须使用支持 thrift 的全新库。如果您想使用 0.9.0,请忘记对 0.8.0 的支持
  • 如果您仍然对这个主题感兴趣,请查看我在 github 上的示例 thrift 应用程序:github.com/tkoomzaaskz/wealthy-laughing-duck。它运行一个 java 服务器和一个轻量级 PHP 客户端。代码在 maven 结构下,但你会在 src/main/php 中找到 PHP 代码

标签: php hbase thrift thrift-protocol


【解决方案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);
            }

【讨论】:

    【解决方案2】:

    使用类加载器。 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";
    }
    

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

    【讨论】:

      【解决方案3】:

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

      节俭/基地 节俭/类加载器 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();
      

      【讨论】:

        最近更新 更多