hbase – PHP – Thrift 0.9.0 – Class 'Thrift\Transport\TTransport' not found in TSocket.php-ThrowExceptions

Exception or error:

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!

How to solve:

Name space Like this:

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

Answer:

Use ClassLoader. Hbase classes do not support classLoader but uses Thrift namespaces.

<?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. This code work for your directory structure:

enter image description here

Answer:

you need to keep Thrift parent dirname, don’t rename it;
the directory structure like this

Thrift/Base
Thrift/ClassLoader
Thrfit/xxxx

example
I put whole Thrift folder in htdocs\thrift-0.9.3\

then it look like this img
enter image description here

the php code should be like this:

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

Leave a Reply

Your email address will not be published. Required fields are marked *