Ubunut12.04安装Thrift,方便支持php(四)

Thrift下载地址:http://thrift.apache.org/download/  或 git clone https://git-wip-us.apache.org/repos/asf/thrift.git thrift

安装Thrift 前 根据不同的系统 安装不同的环境 环境配置请参照  : http://thrift.apache.org/docs/install/


1  需要安装thrift  安装步骤

#  ./configure --without-ruby 

#   make && make install

如果没有安装libevent libevent-devel的应该先安装这两个依赖库apt-get install libevent libevent-devel

安装好后启动hive thrift

# ./hive --service hiveserver >/dev/null 2>/dev/null &

查看hiveserver默认端口是否打开10000   如果打开表示成功

注:php版本要求是5.2或者以上

以下地址是下载Hive php接口的地址

http://nousefor.net/55/2011/12/php/hbase-and-hive-thrift-php-client/


http://download.csdn.net/detail/liangpz521/7998457

2.php读取Hive代码示例

$GLOBALS['THRIFT_ROOT'] = dirname(__FILE__).'/thrift/src';
require_once( $GLOBALS['THRIFT_ROOT'].'/Thrift.php' );
require_once( $GLOBALS['THRIFT_ROOT'].'/transport/TSocket.php' );
require_once( $GLOBALS['THRIFT_ROOT'].'/transport/TBufferedTransport.php' );
require_once( $GLOBALS['THRIFT_ROOT'].'/protocol/TBinaryProtocol.php' );
 
//hbase thrift
require_once dirname(__FILE__).'/thrift/Hbase.php';
 
//hive thrift
require_once dirname(__FILE__).'/thrift/ThriftHive.php';
$transport = new TSocket('localhost', 10000);  
$protocol = new TBinaryProtocol($transport);  
$client = new ThriftHiveClient($protocol);  
$transport->open();  
// run queries, metadata calls etc  


$client->execute('add jar /usr/local/hadoop/hive/lib/hive-contrib-0.10.0.jar');  
$client->execute('select * from item');  
$data = $client->fetchAll();
$transport->close(); 
//echo $data[0];
$arr = explode('	',$data[0]);
print_r($arr);
 




2.php读取HBase代码示例

//thrift php
$GLOBALS['THRIFT_ROOT'] = dirname(__FILE__).'/thrift/src';
require_once( $GLOBALS['THRIFT_ROOT'].'/Thrift.php' );
require_once( $GLOBALS['THRIFT_ROOT'].'/transport/TSocket.php' );
require_once( $GLOBALS['THRIFT_ROOT'].'/transport/TBufferedTransport.php' );
require_once( $GLOBALS['THRIFT_ROOT'].'/protocol/TBinaryProtocol.php' );

//hbase thrift
require_once dirname(__FILE__).'/thrift/Hbase.php';

//hive thrift
require_once dirname(__FILE__).'/thrift/ThriftHive.php';

/*
HBase php thrift client
*/

//open connection
$socket = new TSocket( 'localhost', 9090 );
$transport = new TBufferedTransport( $socket );
$protocol = new TBinaryProtocol( $transport );
$client = new HbaseClient( $protocol );
$transport->open();

//show all tables
$tables = $client->getTableNames();
foreach ( $tables as $name ) {
echo( "  found: {$name}\n" );
}

//Create a table
try {
$columns = array(new ColumnDescriptor( array(
'name' =>; 'colFamily:',
'maxVersions' => 10) ));

$client->createTable("tableName", $columns );
} catch ( AlreadyExists $ae ) {
echo( "WARN: {$ae->;message}\n" );
}

//insert data to table
$mutations = array(
new Mutation( array(
'column' => 'colFamily:Col',
'value' => 'value123'
) ),
);
$client->mutateRow( "tableName", "ID_1237846634624", $mutations );

//get table data
$row = $client->getRow("tableName", "ID_1237846634624");

/*
Hive php thrift client
*/

//open connection
$transport = new TSocket("localhost", 10000);
$protocol = new TBinaryProtocol($transport);
$client = new ThriftHiveClient($protocol);
$transport->open();

//show tables
$client->execute('SHOW TABLES');
$tables = $client->fetchAll();
foreach ($tables as $name){
echo( " found: {$name}\n" );
}

//Create Hive table with Hbase table mapping
$mapping = 'CREATE EXTERNAL TABLE tableName(Col String, Col1 String)

STORED BY \'org.apache.hadoop.hive.hbase.HBaseStorageHandler\'
WITH SERDEPROPERTIES ("hbase.columns.mapping" = "colFamily:Col, colFamily:Col")
TBLPROPERTIES("hbase.table.name" = "tableName")';

$client->execute($mapping);

//Query table
$client->execute('SELECT * FROM tableName Limit 10');
var_dump($client->fetchAll());


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值