---
title: "使用php连接hive"
author: "serisboy"
date: "2014-05-13T18:02:36Z"
tags: []
categories: ["互联网"]
slug: "php-hive"
draft: false
lastmod: "2014-05-13T18:07:06Z"
id: 2065966
url: "https://serisboy.iteye.com/blog/2065966"
---

使用php连接hive的条件

1  需要安装thrift  安装步骤

安装依赖库


```java
yum install automake libtool flex bison pkgconfig gcc-c++ boost-devel libevent-devel zlib-devel python-devel ruby-devel
```



```java
#  ./configure --without-ruby 

#   make && make install
```


如果没有安装libevent libevent-devel的应该先安装这两个依赖库yum -y install libevent libevent-devel

安装好后启动hive thrift


```java
# ./hive --service hiveserver >/dev/null 2>/dev/null &
```


查看hiveserver默认端口是否打开10000   如果打开表示成功


```java
 netstat -tnlp|grep :10000
```


注：php版本要求是5.2或者以上

php连接hive代码


```java
<?  
  
// php连接hive thrift依赖包路径  
 $GLOBALS['thrift_root'] = '/var/www/html/hivephp/thrift/';  
// load the required files for connecting to Hive  
require_once $GLOBALS['thrift_root'] . 'packages/hive_service/ThriftHive.php';  
require_once $GLOBALS['thrift_root'] . 'transport/TSocket.php';  
require_once $GLOBALS['thrift_root'] . 'protocol/TBinaryProtocol.php';  
// Set up the transport/protocol/client  
$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.7.0-cdh3u0.jar');  
$client->execute('select count(1) from apilog');  
var_dump($client->fetchAll());  
$transport->close();  
  
?>  

```

 

将以上代码存为test.php到apache的www路径/var/www/html/中  php连接hive的依赖包放到/var/www/html/hivephp/thrift/

php连接hive的lib包：见附件

打开浏览器浏览http://localhost/test.php就可以看到查询结果了