欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

在php的yii2框架中整合hbase庫(kù)的方法

 更新時(shí)間:2018年09月20日 10:54:08   作者:penngo  
這篇文章主要介紹了在php的yii2框架中整合hbase庫(kù)的方法,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧

Hbase通過(guò)thrift這個(gè)跨語(yǔ)言的RPC框架提供多語(yǔ)言的調(diào)用。

Hbase有兩套thrift接口(thrift1和thrift2),但是它們并不兼容。根據(jù)官方文檔,thrift1很可能被拋棄,本文以thrift2整合為例。

1、訪問(wèn)官網(wǎng)http://thrift.apache.org/download,下載

thrift-0.11.0.exe   (生成接口rpc工具,thrift-0.11.0.exe改名thrift.exe,保存在D:\project\thrift\thrift.exe)
thrift-0.11.0.tar.gz(thrift相關(guān)庫(kù),保存在D:\project\thrift\thrift-0.11.0)

2、訪問(wèn)hbase官網(wǎng)(http://archive.apache.org/dist/hbase/),下載hbase-1.2.6-src.tar.gz

解壓保存在D:\project\thrift\hbase-1.2.6

3、生成php接口代碼

解壓hbase-1.2.6-src.tar.gz,hbase-1.2.6\hbase-thrift\src\main\resources\org\apache\hadoop\hbase文件夾同時(shí)存在thrift和thrift2接口描述文件,本文只使用thrift2

在D:\project\thrift目錄中輸入cmd命令,生成對(duì)應(yīng)php的sdk文件。

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

生成的D:\project\thrift\gen-php目錄包含文件:

THBaseService.php
Types.php

4、要通過(guò)thrifc調(diào)用hbase,需要先啟動(dòng)hbase的接口服務(wù)

$HBASE_HOME/bin/hbase-daemon.sh start thrift2 //啟動(dòng)
$HBASE_HOME/bin/hbase-daemon.sh stop thrift2  //停止

5、與yii2整合

在vendor文件夾中新建hbase目錄

vendor\hbase\gen-php //復(fù)制D:\project\thrift\gen-php
vendor\hbase\php   //復(fù)制D:\project\thrift\thrift-0.11.0\lib\php

由于thrift2的php不使用Composer,類庫(kù)命名方式也不完全符合PSR-4標(biāo)準(zhǔn), 所以本文使用include_path方式來(lái)定位并導(dǎo)入類文件。

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

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評(píng)論