ubuntu使用thrift来连接PHP和HBase

项目需要使用PHP调用Hbase和Hadoop,网上查了一下,需要使用thrift这个中间件,是facebook开源的,下面就介绍一下安装过程,我的系统时新的,需要首先安装一些必要软件,参考http://thrift-tutorial.readthedocs.io/en/latest/installation.html

sudo apt-get install automake bison flex g++ git libboost1.55-all-dev libevent-dev libssl-dev libtool make pkg-config

1. 去thrift官网下载代码https://thrift.apache.org/,我下载的版本是 Apache Thrift v0.9.3

2. 解压

tar -xvf thrift-0.9.3.tar.gz
3. 进入目录,执行命令

./bootstrap.sh
./configure --prefix=/home/hadoop/thrift --with-php-config=/usr/bin/php-config
prefix标志着thrift安装的路径。从控制台输出可以看出当前系统拥有的库,这些库表示你可以使用那些语言,如果现实缺少一些语言的库,那么最好下载配置好,以便以后能使用这些语言


4. 编译

sudo make
sudo make install
make可能比较耗时


5. 测试安装成功与否

thrift -version


显示

Thrift version 0.9.3
说明安装成功

要使用HBase的thrift接口,必须将它的服务启动,命令行为:

hbase-deamon.sh start thrift2


thrift默认的监听端口是9090,可以用netstat -nl | grep 9090看看该端口是否有服务


那么PHP如何使用thrift呢?

Thrift需要使用PHP5

在php文件中需要require_once一些php头文件,这些文件在thrift/lib/php/src下,设置 $GLOBALS['THRIFT_ROOT'] Thrift 安装目录

然后require_once $GLOBALS['THRIFT_ROOT'].'/Thrift.php';

必须在其他 Thrift 包含文件之前.
对MyPackage.thrift,生成的文件要安装到下面目录:
$GLOBALS['THRIFT_ROOT'].'/packages/MyPackage/'

配置HBase,支持Thrift

将Hbase.thrift编译成php文件。

我们在http://www-eu.apache.org/dist/hbase/1.1.5/这里下载hbase-1.1.5-src.tar.gz源代码,解压会发现有在~/workspace/www/Mycloud/hbase-1.1.5/hbase-thrift/src/main/resources/org/apache/hadoop/hbase/thrift2$路径下发现hbase.thrift文件,cd到这个目录,执行命令

 thrift -gen php hbase.thrift
查看生成的内容:

在自己的工程Mycloud下新建hbase_ext目录,用来存放hbase依赖的代码,把刚才生成的gen-php文件夹拷贝到hbase_ext目录下。

在下载的thrift目录/lib/php/lib下找到Thrift的依赖代码,把php整个文件夹都拷贝到hbase_ext目录下,

依赖的目录结构如下图所示:

接下来依赖搞定以后,就可以写操作hbase的代码了。下面是我写的操作hbase的代码。testhbase.php

<?php
/**
 * Created by PhpStorm.
 * User: ss
 * Date: 16-6-21
 * Time: 下午11:43
 */
//echo $view->htmlError();

ini_set('display_errors', E_ALL);
$GLOBALS['THRIFT_ROOT'] = "/home/ss/workspace/www/Mycloud/hbase_ext";


/* Dependencies. In the proper order. */
require_once $GLOBALS['THRIFT_ROOT'] . '/php/lib/Thrift/Transport/TTransport.php';
require_once $GLOBALS['THRIFT_ROOT'] . '/php/lib/Thrift/Transport/TSocket.php';
require_once $GLOBALS['THRIFT_ROOT'] . '/php/lib/Thrift/Protocol/TProtocol.php';
require_once $GLOBALS['THRIFT_ROOT'] . '/php/lib/Thrift/Protocol/TBinaryProtocol.php';
require_once $GLOBALS['THRIFT_ROOT'] . '/php/lib/Thrift/Protocol/TBinaryProtocolAccelerated.php';
require_once $GLOBALS['THRIFT_ROOT'] . '/php/lib/Thrift/Transport/TBufferedTransport.php';
require_once $GLOBALS['THRIFT_ROOT'] . '/php/lib/Thrift/Type/TMessageType.php';
require_once $GLOBALS['THRIFT_ROOT'] . '/php/lib/Thrift/Factory/TStringFuncFactory.php';
require_once $GLOBALS['THRIFT_ROOT'] . '/php/lib/Thrift/StringFunc/TStringFunc.php';
require_once $GLOBALS['THRIFT_ROOT'] . '/php/lib/Thrift/StringFunc/Core.php';
require_once $GLOBALS['THRIFT_ROOT'] . '/php/lib/Thrift/Type/TType.php';
require_once $GLOBALS['THRIFT_ROOT'] . '/php/lib/Thrift/Exception/TException.php';
require_once $GLOBALS['THRIFT_ROOT'] . '/php/lib/Thrift/Exception/TTransportException.php';
require_once $GLOBALS['THRIFT_ROOT'] . '/php/lib/Thrift/Exception/TProtocolException.php';


/* Remember these two files? */
//require_once($GLOBALS['THRIFT_ROOT'] . '/gen-php/THBaseService.php');
//require_once($GLOBALS['THRIFT_ROOT'] . '/gen-php/Types.php');
require_once $GLOBALS['THRIFT_ROOT'] . '/gen-php/Hbase/Types.php';
require_once $GLOBALS['THRIFT_ROOT'] . '/gen-php/Hbase/Hbase.php';

use Thrift\Protocol\TBinaryProtocol;

use Thrift\Transport\TBufferedTransport;


use Thrift\Transport\TSocket;
use Hbase\HbaseClient;
use Hbase\ColumnDescriptor;
use Hbase\Mutation;

function printRow($rowresult)
{
    if (isset($rowresult)) {
        echo("row: {$rowresult->row}, cols: \n");
        $values = $rowresult->columns;
        asort($values);
        foreach ($values as $k => $v) {
            echo("  {$k} => {$v->value}\n");
        }
    }

}


try {
//define host and port
    $host = '192.168.111.73';
    $port = 9090;

    $tablename = "student";

    $socket = new Thrift\Transport\TSocket($host, $port);

    $socket->setSendTimeout(10000); // Ten seconds (too long for production, but this is just a demo ;)
    $socket->setRecvTimeout(20000); // Twenty seconds

    $transport = new TBufferedTransport($socket);
    $protocol = new \Thrift\Protocol\TBinaryProtocolAccelerated($transport);

    $client = new \Hbase\HbaseClient($protocol);

    $transport->open();


    //查看table
    $tables = $client->getTableNames();
        sort($tables);

    foreach ($tables as $name) {
        echo $name . "</br>";
        $t = "";
        if($name == $tablename){
            if($client->isTableEnabled($name)){
                echo (" disabling table: {$name}"."</br>");
                $client->disableTable($name);
            }
            echo (" deleting table: {$name}"."</br>");
            $client->deleteTable($name);
        }

    }


    //新建table
    $columns = array(
        new \Hbase\ColumnDescriptor(array(
            'name'=>'id:',
            'maxVersions'=>10
        )),
        new \Hbase\ColumnDescriptor(array(
            'name'=>'name:',
            'maxVersions'=>10
        )),
        new \Hbase\ColumnDescriptor(array(
            'name'=>'score:',
            'maxVersions'=>10
        ))

    );


    try{
        $client->createTable($tablename,$columns);
    }catch (\Hbase\AlreadyExists $ae)
    {
        var_dump("WARN: ($ae->message)\n");
    }


    //get table descriptors
    $descriptors = $client->getColumnDescriptors($tablename);
    asort($descriptors);
    foreach($descriptors as $col){
        var_dump("column:{$col->name},maxVer: {$col->maxVersions}");
    }



    //set column
    //add update column data
    $time = time();
//    var_dump($time);

    $row = '2';
    $valid = "foobar-".$time;

    $dummy_attributes = array();


    //test UTF-8 handling
    $invalid = "foo-\xfc\xa1\xa1\xa1\xa1\xa1";
    $valid = "foo-\xE7\x94\x9F\xe3\x83\x93\xe3\x83\xbc\xe3\x83\xa8";


    //not utf-8 is fine for data
    $mutations = array(
        new \Hbase\Mutation(
            array(
                'column'=>'id:foo',
                'value'=>$invalid
            )
        )

    );
    $client->mutateRow($tablename,"foo",$mutations,$dummy_attributes);


    $mutations = array(
        new \Hbase\Mutation(
            array(
                'column'=>'id:foo',
                'value'=>$valid
            )
        )

    );
    $client->mutateRow($tablename,$valid,$mutations,$dummy_attributes);



    //non-utf8 is not allowed in row names
//    try{
//        $mutations = array(
//            new \Hbase\Mutation(
//                array(
//                    'column'=>'id:foo',
//                    'value'=>$invalid
//                )
//            )
//
//        );
//        $client->mutateRow($tablename,$invalid,$mutations,$dummy_attributes);
//    }catch (\Hbase\IOError $e)
//    {
//        echo ("except error : {$e->message}");
//    }


    $arry = $client->get($tablename,"foo","id:foo",$dummy_attributes);

    foreach($arry as $k =>$v){

//        echo "------get one : key = {$k}"."</br>";
        echo "------get one : value = {$v->value}"."</br>";
        echo "------get one : timestamp = {$v->timestamp}"."</br>";
    }

//    $client->deleteAll($tablename,"foo","id:foo",$dummy_attributes);


    echo ("Starting scanner..."."</br>");

    $scanner = $client->scannerOpen($tablename,"foo",array("id:foo"),$dummy_attributes);
    try{
        $values = $client->scannerGet($scanner);
        print_r($values);

    }catch (Exception $e){
        $client->scannerClose($scanner);
        echo ("Scaner finished"."</br>");
    }

    $transport->close();

} catch (Exception $e) {
    echo "Exception: $e\r\n";
}

?>
用浏览器访问后的结果是:



hbase数据库增删查改就完成了。









  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 4
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值