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://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());