Method of integrating hbase library in yii2 framework of php

  • 2021-11-01 23:54:58
  • OfStack

Hbase provides multilingual invocation through thrift, a cross-language RPC framework.

Hbase has two sets of thrift interfaces (thrift1 and thrift2), but they are not compatible. According to official documents, thrift1 is likely to be abandoned, and this article takes thrift2 integration as an example.

1. Visit official website http://thrift.apache.org/download and download

thrift-0. 11.0. exe (Generate interface rpc tool, thrift-0. 11.0. exe renamed thrift. exe, 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 official website (http://archive.apache.org/dist/hbase/) and download hbase-1. 2.6-src.tar.gz

Extract and save in D:\ project\ thrift\ hbase-1. 2.6

3. Generate php interface code

Extract the hbase-1. 2.6-src. tar. hbase-1. 2.6\ hbase-thrift\ src\ main\ resources\ org\ apache\ hadoop\ hbase folder. Both thrift2 interface descriptors exist, and only thrift2 is used in this article

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 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 
$HBASE_HOME/bin/hbase-daemon.sh stop thrift2  // Stop 

5. Integration with yii2

Create a new hbase directory in the vendor folder


vendor\hbase\gen-php // Duplicate D:\project\thrift\gen-php
vendor\hbase\php   // Duplicate D:\project\thrift\thrift-0.11.0\lib\php

Because php of thrift2 does not use Composer, and the class library naming method does not completely conform to PSR-4 standard, this paper uses 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();
  }
}


Related articles: