Integrating hbase Library in yii2 framework of php

Keywords: HBase PHP Apache Hadoop

Hbase provides multilingual calls through thrift, a cross language RPC framework.
Hbase has two sets of thrift interfaces (thrift1 and thrift2), but they are not compatible. According to the official documents, thrift1 is likely to be abandoned. This article takes thrift2 integration as an example.
1. Visit http://thrift.apache.org/download to download

Thrift-0.11.0.exe (generate interface rpc tool, thrift-0.11.0.exe is renamed thrift.exe, and saved in D:\project\thrift\thrift.exe)
thrift-0.11.0.tar.gz (Thrift related library, saved in D:\project\thrift\thrift-0.11.0)

2. Visit HBase website (http://archive.apache.org/dist/hbase/), Download hbase-1.2.6-src.tar.gz
Extract and save it in D:\project\thrift\hbase-1.2.6

3. Generate php interface code
Extract hbase-1.2.6-src.tar.gz, hbase-1.2.6 \ HBase thrift \ SRC \ main \ resources \ ORG \ Apache \ Hadoop \ HBase folder, and there are both thrift and thrift2 interface description files. In this paper, only thrift2 is used
Enter the cmd command in the D:\project\thrift directory to generate the sdk file corresponding to php.

thrift -gen php hbase-1.2.6\hbase-thrift\src\main\resources\org\apache\hadoop\hbase\thrift2\hbase.thrift


The generated D: \ project \ thrift \ Gen PHP directory contains the following files:

THBaseService.php
Types.php

4. To call hbase through thrifc, you need to start the interface service of hbase first

$HBASE_HOME/bin/hbase-daemon.sh start thrift2  //start-up
$HBASE_HOME/bin/hbase-daemon.sh stop thrift2   //Stop it

5. Integration with yii2
Create a new hbase directory in the vendor folder

vendor\hbase\gen-php  //Copy D: \ project \ thrift \ Gen PHP
vendor\hbase\php      //Copy D:\project\thrift\thrift-0.11.0\lib\php

Because the php of thrift2 does not use Composer and the class library naming method does not fully conform to the PSR-4 standard, this paper uses the include ﹣ path method to locate and import class files.

common\models\HArticle.php

<?php
namespace common\models;
require_once dirname(dirname(__DIR__)).'/vendor/hbase/php/lib/Thrift/ClassLoader/ThriftClassLoader.php';
use Thrift\ClassLoader\ThriftClassLoader;
$loader = new ThriftClassLoader();
$loader->registerNamespace('Thrift', dirname(dirname(__DIR__)) . '/vendor/hbase/php/lib');
$loader->register();
require_once dirname(dirname(__DIR__)) . '/vendor/hbase/gen-php/Types.php';
require_once dirname(dirname(__DIR__)) . '/vendor/hbase/gen-php/THBaseService.php';
use Thrift\Protocol\TBinaryProtocol;
use Thrift\Transport\TSocket;
use Thrift\Transport\TBufferedTransport;
use THBaseServiceClient;
use TGet;

class HArticle
{
    private $host = '192.168.1.108';
    private $port = 9090;
    
    public function get($rowKey){
        $socket = new TSocket($this->host, $this->port);
        $transport = new TBufferedTransport($socket);
        $protocol = new TBinaryProtocol($transport);
        $client = new THBaseServiceClient($protocol);
        $transport->open();
        
        $tableName = "article_2018";
        
        $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;
        }
        $transport->close();
        return $data;
    }
}

frontend\controllers\TestController.php

<?php
namespace frontend\controllers;
use yii\web\Controller;
use common\models\HArticle;
class TestController extends Controller
{

    public function actionIndex()
    {
        $hArticle = new HArticle();
        $data = $hbaseNews->get('20180908_1f1be3cd26a36e351175015f450fa3f6');
        var_dump($data);
        exit();
    }
}

Posted by depojones on Tue, 31 Dec 2019 22:15:31 -0800