php通过 thrift访问hadoop的hive_lianxiang_biancheng的博客-CSDN博客


本站和网页 https://blog.csdn.net/lianxiang_biancheng/article/details/8864846 的作者无关,不对其内容负责。快照谨为网络故障时之索引,不代表被搜索网站的即时页面。

php通过 thrift访问hadoop的hive_lianxiang_biancheng的博客-CSDN博客
php通过 thrift访问hadoop的hive
lianxiang_biancheng
于 2013-04-28 19:52:14 发布
6120
收藏
分类专栏:
hadoop
文章标签:
Hadoop
PHP
thrift
Linux
版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/lianxiang_biancheng/article/details/8864846
版权
hadoop
专栏收录该内容
1 篇文章
0 订阅
订阅专栏
本文讲解php通过sql查询hadoop中的数据。主要使用的技术是:php通过thrift向hive提交sql查询,hive将sql查询转换成hadoop任务,等到hadoop执行完毕后,返回一个结果uri,然后我们只需要读取这个uri中的内容。
Thrift的诞生是为了解决不同语言之间的访问的问题,可以支持多种程序语言,如c++、php、java、python等。Thrift是由facebook开发的轻量级跨语言的服务框架,现在已经移交到apache基金会下。和它类似的有google出的protocol buffer和ice。Thrift的一大优势是支持的语言很丰富,它使用自己的IDL语言来服务接口和数据交换的格式。
Hive是可以使用类似sql的语言访问HBase。而HBase是一个开源的nosql产品,它实现了google bigtable论文的一个开源产品,和hadoop、hdfs一起,可以用来存储和处理海量column family数据。
Thrift的官方网址:http://thrift.apache.org/
一.服务器端下载安装thrift
在hadoop和hbase都已经安装好的集群上安装thrift。 (1)下载:wget http://mirror.bjtu.edu.cn/apache//thrift/0.8.0/thrift-0.8.0.tar.gz,下载thrift客户端源码包。
(2)解压:tar -xzf thrift-0.8.0.tar.gz (3)编译安装:如果是源码编译的,首先需要执行./bootstrap.sh创建./configure文件; 接下来执行./configure; 再执行make && make install (4)启动:./bin/hbase-daemon.sh start thrift Thrift默认监听的端口是9090。 参考链接:http://blog.csdn.net/hguisu/article/details/7298456
二.创建.thrift文件
Thrift编译器安装好之后,需要创建一个thrift文件。该文件为一个接口定义文件,需要定义thrift的类型(types)和服务(services)。该文件中定义的服务(services)是由服务器端实现的,并由客户端进行调用。Thrift编译器的作用是将你写的thrift文件生成为客户端接口源码,该接口源码是由不同的客户端库和你写的服务来生成的。为了通过thrift文件产生不同语言的接口源码,我们需要运行:
thrift --gen <language> <Thrift filename>
三.thrift文件描述
支持的变量类型
类型 描述
bool #true, false
byte #8位的有符号整数
i16 #16位的有符号整数
i32 #32位的有符号整数
i64 #64位的有符号整数
double #64位的浮点数
string #UTF-8编码的字符串
binary #字符数组
struct #结构体
list<type> #有序的元素列表,类似于STL的vector
set<type> #无序的不重复元素集,类似于STL的set
map<type1,type2> #key-value型的映射,类似于STL的map
exception #是一个继承于本地语言的exception基类
service #服务包含多个函数接口(纯虚函数)
四.从服务端到客户端如何开发
1.简单的helloworld程序
这里使用python做服务端,php做客户端,当然也可以使用c++来做服务端。下面开始介绍开发流程。
(1)首先我们在服务器端写个helloworld.thrift文件,如下所示:
service HelloWorld{
string ping(1: string name),
string getpng(),
}(2)
在服务器端编译helloworld.thrift
编译helloworld.thrift文件,会产生服务器端和客户端相应语言的接口源码。 /usr/local/thrift/bin/thrift -r --gen py helloworld.thrift   /usr/local/thrift/bin/thrift -r --gen php helloworld.thrift   #会在当前目录下生成 gen-* 目录。 产生的gen-py目录放在服务器上,产生的gen-php目录放在客户端上。 (3)编写服务器端代码
import sys
sys.path.append('./gen-py')
from helloworld import HelloWorld
from helloworld.ttypes import *
from thrift.transport import TSocket
from thrift.transport import TTransport
from thrift.protocol import TBinaryProtocol
from thrift.server import TServer
class HellowordHandler:
def __init__ (self):
pass
def ping (self, name):
print name + ' from server.'
return "%s from server." % name
def getpng (self):
f = open("./logo.png", "rb")
c = f.read()
f.close()
return c
handler = HellowordHandler()
processor = HelloWorld.Processor(handler)
transport = TSocket.TServerSocket(9090)
tfactory = TTransport.TBufferedTransportFactory()
pfactory = TBinaryProtocol.TBinaryProtocolFactory()
server = TServer.TSimpleServer(processor, transport, tfactory, pfactory)
# You could do one of these for a multithreaded server
#server = TServer.TThreadedServer(processor, transport, tfactory, pfactory)
#server = TServer.TThreadPoolServer(processor, transport, tfactory, pfactory)
print 'Starting the server...'
server.serve()
print 'done.'
(4)编写客户端代码
先将gen-php目录拷贝到客户端上。
<?php
try{
//包含thrift客户端库文件
$GLOBALS['THRIFT_ROOT'] = './php/src';
require_once $GLOBALS['THRIFT_ROOT'].'/Thrift.php';
require_once $GLOBALS['THRIFT_ROOT'].'/protocol/TBinaryProtocol.php';
require_once $GLOBALS['THRIFT_ROOT'].'/transport/TSocket.php';
require_once $GLOBALS['THRIFT_ROOT'].'/transport/THttpClient.php';
require_once $GLOBALS['THRIFT_ROOT'].'/transport/TBufferedTransport.php';
error_reporting(E_NONE);
//包含helloworld接口文件
$GEN_DIR = './gen-php';
require_once $GEN_DIR.'/helloworld/HelloWorld.php';
error_reporting(E_ALL);
$socket = new TSocket('*.*.*.*', 9090);
$transport = new TBufferedTransport($socket, 1024, 1024);
$protocol = new TBinaryProtocol($transport);
$client = new HelloWorldClient($protocol);
$transport->open();
$a = $client->ping('xyq ');
echo $a;
$transport->close();
}catch(TException $tx){
print 'TException: '.$tx->getMessage()."/n";
?>最后给出一篇参考链接:
http://blog.csdn.net/heiyeshuwu/article/details/5982222
2.thrift官网上给出的例子
Apache thrift能够让你在一个简单的.thrift文件中,定义数据类型和服务接口。将该.thrift文件作为输入文件,通过编译器编译产生服务端和客户端源码,从而构建了RPC客户端和服务器端之间的跨语言编程。 下面直接给出关键代码。
(1)thrift定义文件
/*定义的接口数据类型*/
struct UserProfile {
1: i32 uid,
2: string name,
3: string blurb
/*定义的接口函数*/
service UserStorage {
void store(1: UserProfile user),
UserProfile retrieve(1: i32 uid)
}(2)客户端python实现
# Make an object
up = UserProfile(uid=1,
name="Test User",
blurb="Thrift is great")
# Talk to a server via TCP sockets, using a binary protocol
transport = TSocket.TSocket("localhost", 9090)
transport.open()
protocol = TBinaryProtocol.TBinaryProtocol(transport)
# Use the service we already defined
service = UserStorage.Client(protocol)
service.store(up)
# Retrieve something as well
up2 = service.retrieve(2)
(3)服务器端c++实现
class UserStorageHandler : virtual public UserStorageIf {
public:
UserStorageHandler() {
// Your initialization goes here
void store(const UserProfile& user) {
// Your implementation goes here
printf("store\n");
void retrieve(UserProfile& _return, const int32_t uid) {
// Your implementation goes here
printf("retrieve\n");
};
int main(int argc, char **argv) {
int port = 9090;
shared_ptr<UserStorageHandler> handler(new UserStorageHandler());
shared_ptr<TProcessor> processor(new UserStorageProcessor(handler));
shared_ptr<TServerTransport> serverTransport(new TServerSocket(port));
shared_ptr<TTransportFactory> transportFactory(new TBufferedTransportFactory());
shared_ptr<TProtocolFactory> protocolFactory(new TBinaryProtocolFactory());
TSimpleServer server(processor, serverTransport, transportFactory, protocolFactory);
server.serve();
3.实战经历
(1).thrift接口文件
文件名为hive.thrift,如下所示:
namespace java com.gj.data.hive.thrift
/**
* 向HADOOP提交HIVE任务类。典型的用法是提交任务,轮询任务是否完成,取得任务的结果URI,读取结果文件。
*这里展示的是客户端为java语言时的用法。
* long taskId = client.submitTask("abc@gj.com", "web", "select * from table1 where dt = '2013-04-10' limit 10;");
* if (taskId <=0) {
* System.out.println("error submit");
* return;
* }
//轮询任务是否完成
* int count = 50;
* while(count >= 0) {
* try {
* Thread.sleep(30 * 1000);
* } catch (InterruptedException ex) {}
* if (client.isTaskFinished(taskId)) {
* System.out.println(client.getResultURI(taskId));
* break;
* }
* count --;
* }
*/
service Hive {
/** 提交任务
* user - 用户名,工作邮箱,如abc@xxx.com
* env - 提交的环境。目前支持两个环境: mobile - 移动端, web - 主站。
* sql - 提交的sql语句。
* 返回值:成功提交返回大于0的任务id值,此id用于后续查询。失败返回0或-1.
*/
i64 submitTask(1:string user, 2:string env, 3:string sql);
/** 查看任务是否完成
* taskId - 任务号
* 返回值:完成返回true,未完成返回false
*/
bool isTaskFinished(1:i64 taskId);
/** 取得任务结果的URI,可以用此URI获得结果数据
* taskId - 任务号
* 返回值:任务有结果,返回URI,否则返回空字符串
*/
string getResultURI(1:i64 taskId);
/** 取得用户的所有任务列表
* user - 用户名,完整的ganji邮箱
* 返回值:任务号列表,如果没有任务,返回空
*/
list<i64> getTasksByUserName(1:string user);
}(2).生成php与hbase的接口文件(服务器端实现)
/usr/local/thrift/bin/thrift --gen php hive.thrift 然后会在gen-php目录下生成了Hive.php和Hive_types.php文件。 然后把Hive.php和Hive_types.php文件拷贝到:客户端php开发的目录下。
(3).配置php客户端
php作为客户端使用thrift时,需要进行如下配置。
(a)准备thrift php客户端基础类
这些基础类可以从thrift的源码包中找到。在thriftsrc/lib/php/src下,一班有如下目录和文件:ext/,protocol/,transport/目录,和thrift.php、autoload.php文件。我们把这些目录和文件拷贝到客户端的/server/www/thrift_part/thrift-0.5.0/目录下。
(b)配置php的thrift扩展,使其支持thrift
如果php想要使用thrift,还需要安装php的thrift扩展。 如下所示: 首先下载相应的php的thrift扩展,进行解压; 进入源码下的ext/thrift_protocol; /usr/local/php/bin/phpize ./configure --with-php-config=/usr/local/php/bin/php-config --enable-thrift_protocol make make install 然后把生成的thrift_protocol.so文件配置到php.ini并重启apache服务。
(4).php客户端的实现
文件名为:updateHiveData.php
<?php
$GLOBALS['THRIFT_ROOT'] = '/server/www/third_part/thrift-0.5.0';
require_once $GLOBALS['THRIFT_ROOT'].'/Thrift.php';
require_once $GLOBALS['THRIFT_ROOT'].'/packages/scribe/scribe.php';
require_once $GLOBALS['THRIFT_ROOT'].'/protocol/TBinaryProtocol.php';
require_once $GLOBALS['THRIFT_ROOT'].'/transport/TSocket.php';
require_once $GLOBALS['THRIFT_ROOT'].'/transport/THttpClient.php';
require_once $GLOBALS['THRIFT_ROOT'].'/transport/TFramedTransport.php';
require_once $GLOBALS['THRIFT_ROOT'].'/transport/TBufferedTransport.php';
//生成的文件
require_once dirname(__FILE__) . '/Hive.php';
//require_once dirname(__FILE__) .'/hive_types.php';
ERROR_REPORTING(E_ALL);
INI_SET('DISPLAY_ERRORS','ON');
$socket = new TSocket('hive.corp.gj.com',13080);
$socket->setDebug(TRUE);
// 设置接收超时(毫秒)
$socket->setSendTimeout(10000);
$socket->setRecvTimeout(10000);
//$transport = new TBufferedTransport($socket, 1024, 1024);
$transport = new TFramedTransport($socket);
$protocol = new TBinaryProtocol($transport);
$client = new HiveClient($protocol);
try{
$transport->open();
}catch(TException $tx){
echo $tx->getMessage();
//获取各个类目某一天的 PV UV
$taskId = $client->submitTask('xxx@xxx.com','web',"select regexp_extract(gjch, '^/([^/]+)', 1), count(*), count(distinct uuid) from table1 where dt = '2013-04-22' and gjch regexp '[^@]*/detail' GROUP BY regexp_extract(gjch, '^/([^/]+)', 1);");
if($taskId <= 0){
echo 'error submit';
exit;
echo $taskId . "\n";
$count = 50;
while($count > 0){
try{
//sleep以秒为单位,这里3分钟轮询一次
sleep(3*60);
}catch(TException $tx){}
if($client->isTaskFinished($taskId)){
//echo $client->getResultURI($taskId);
$url = $client->getResultURI($taskId);
//echo $url;
$handle = fopen($url,"rb");
$content = stream_get_contents($handle);
echo $content;
fclose($handle);
break;
$count--;
$transport->close();
?>
由于服务器端不是本人负责,所以当时只根据thrift定义文件,实现了php客户端。运行结果如下:
其中这里url是$client->getResultURI取得的结果,网页内容是这个uri对应的内容。
五.thrift类说明
TSocket:采用TCP socket进行数据传输; Transport类(传输层): 负责数据传输,介绍几个常用类: TBufferedTransport:对某个Transport对象操作的数据进行buffer,即从buffer中读取数据进行传输,或者将数据直接写入buffer; TFramedTransport:同TBufferdTransport类型,也会对数据进行buffer,同时它支持定长数据发送和接受; TFileTransport:文件(日志)传输类,允许client将文件传给server,语序server将受到的数据写到文件中; Protocol类(协议): 负责数据编码,主要有以下几个常用类: TBinaryProtocol:二进制编码; TJSONProtocol:JSON编码。
lianxiang_biancheng
关注
关注
点赞
收藏
打赏
评论
php通过 thrift访问hadoop的hive
本文讲解php通过sql查询hadoop中的数据。主要使用的技术是:php通过thrift向hive提交sql查询,hive将sql查询转换成hadoop任务,等到hadoop执行完毕后,返回一个结果uri,然后我们只需要读取这个uri中的内容。Thrift的诞生是为了解决不同语言之间的访问的问题,可以支持多种程序语言,如c++、php、java、python等。Thrift是由faceboo
复制链接
扫一扫
专栏目录
使用php连接hive
08-05
NULL
博文链接:https://serisboy.iteye.com/blog/2065966
php链接HIVE的例子代码
09-01
php链接HIVE的例子代码,通过thrift使得php和hive可以进行轻松的通信,达到php操作hive的目的。特别是想要用php做hive的数据挖掘界面展示的用。
参与评论
您还未登录,请先
登录
后发表或查看评论
php 连接hive,PHP连接Hive执行sql查询
weixin_42153615的博客
03-10
270
使用php连接hive的条件1 需要安装thrift 安装步骤# ./configure --without-ruby# make && make install如果没有安装libevent libevent-devel的应该先安装这两个依赖库yum -y install libevent libevent-devel安装好后启动hive thrift# ./hive-...
通过Thrift 方式访问 Hive
天生我材必有用,千金散尽还复来
03-21
402
package com.netqin.hive.kpi;
import org.apache.hadoop.hive.service.HiveClient;
import org.apache.hadoop.hive.service.HiveServerException;
import org.apache.log4j.Logger;
import org.apache.thr...
Hive的thrift服务
wang_ying_198的专栏
04-20
9906
Hive具有一个可选的组件叫HiveServer或HiveThrift,其允许通过指定端口访问Hive。
启动Thrift Server
进入Hive安装目录 使用如下命令开启服务
hive --service hiveserver &
检查HiveServer是否启动成功使用如下命令
netstat -nl | grep 10000
配置Groovy使用HiveSe
Hive最全常见错误及解决方案
xia1140418216的博客
10-13
4703
1)SecureCRT 7.3出现乱码或者删除不掉数据,免安装版的SecureCRT 卸载或者用虚拟机直接操作或者换安装版的SecureCRT
2)连接不上mysql数据库
(1)导错驱动包,应该把mysql-connector-java-5.1.27-bin.jar导入/opt/module/hive/lib的不是这个包。错把mysql-connector-java-5.1.27.tar.gz导入hive/lib包下。
(2)修改user表中的主机名称没有都修改为%,而是修改为localhost
3)hi
php连接hive执行sql查询
热门推荐
东杰书屋
07-01
1万+
使用php连接hive的条件1 需要安装thrift 安装步骤# ./configure --without-ruby # make && make install如果没有安装libevent libevent-devel的应该先安装这两个依赖库yum -y install libevent libevent-devel安装好后启动hive thrift# ./h
php 连接hive,php使用thrift 0.8访问操作hive1.0
weixin_33335559的博客
03-10
118
/*** Hive 的PHP操作类* Rain <563268276@qq.com>*/defined('HIVE_LIB_PATH') or define('HIVE_LIB_PATH', realpath(dirname(__FILE__)).'/');//为了避免HIVE查询的结果内存超出,设定返回所有的hive查询记录的最大值define('MAX_ROWS', 1000000...
php 连接hive,php连接hive执行sql查询 | 学步园
weixin_32417571的博客
03-10
214
使用php连接hive的条件1 需要安装thrift 安装步骤# ./configure --without-ruby# make && make install如果没有安装libevent libevent-devel的应该先安装这两个依赖库yum -y install libevent libevent-devel安装好后启动hive thrift# ./hive-...
php调用hive,如何进行hive的简单操作
最新发布
weixin_39530557的博客
03-21
278
搭建Hive的图形界面 添加war包到hive的lib目录,需要大家耐心学习。1、 启动hive因为需要借助于MySQL保存Hive的元数据,所以,请首先启动MySQL数据库service mysql start # 可以在Linux的任何目录下执行该命令由于Hive是基于Hadoop的数据仓库,使用HiveQL语言撰写的查询语句,最终都会被Hive自动解析成MapReduce任务由Hadoop去...
thrift 访问hive
comecheer.com
09-10
1365
编译hive_service
/hadoop/hive-0.7.1-cdh3u5/src/service/if
修改hive_service.thrift中
include "/home/peter/libs/thrift-0.8.0/contrib/fb303/if/fb303.thrift"
include "/hadoop/hive-0.7.1-cdh3u5/
PHP 连接 Hive 执行 SQL 查询
qq271757232的专栏
08-21
3655
$ cd /opt/hive/lib/php/packages/
$ mv hive_service hive_service.bak
$ mv hive_service.bak/hive_service ./
$ cd /opt/hive/lib/php/packages/queryplan/queryplan
$ cp queryplan_types.php ../
$ cd /opt/h
php通过thrift开发hive
liangtingac的专栏
12-19
3724
在安装好hadoop,hive并且以mysql作为hive的元数据库配置完成的前提下
1.下载thrift(apache thrift官网)
2.解压:tar -zxvf 压缩包名
3.安装依赖库:libevent和libevent-devel
4.在thrift路径下执行:./configure【具体见./configure --help】;然后make & make install
php连接hive thrift的lib依赖包
07-01
php连接hive thrift server的lib包,经测试用hive压缩包中自带的php lib无法测试成功
php连接hive执行HQL查询
hzcyclone的专栏
07-02
1409
使用php连接hive的条件
1 需要安装thrift 安装步骤
# ./configure --without-ruby
# make && make install
如果没有安装libevent libevent-devel的应该先安装这两个依赖库yum -y install libevent libevent-devel
安装好后启动hive thr
php连接hive各种问题记录
liangtingac的专栏
12-19
3417
启动hiveserver:
1.org.apache.thrift.transport.TTransportException: Could not create ServerSocket on address 0.0.0.0/0.0.0.0:10000.
python通过thrift方式连接hive
weixin_34408717的博客
04-29
273
2019独角兽企业重金招聘Python工程师标准>>>
...
“相关推荐”对你有帮助么?
非常没帮助
没帮助
一般
有帮助
非常有帮助
提交
©️2022 CSDN
皮肤主题:大白
设计师:CSDN官方博客
返回首页
lianxiang_biancheng
CSDN认证博客专家
CSDN认证企业博客
码龄13年
暂无认证
26
原创
25万+
周排名
130万+
总排名
10万+
访问
等级
1346
积分
27
粉丝
14
获赞
12
评论
37
收藏
私信
关注
热门文章
PyQt开发讲解
15817
文件锁和Python多进程的使用
11137
Python中网络页面抓取和页面分析
9853
Python+Eric+PyQt的安装配置和第一个程序HelloWorld
6287
php通过 thrift访问hadoop的hive
6120
分类专栏
数据结构/算法
4篇
C++
2篇
Linux
9篇
.NET
Python
7篇
hadoop
1篇
node.js
1篇
PHP
1篇
金融知识
2篇
最新评论
文件锁和Python多进程的使用
Tisfy:
他能使人有三月不知肉味,使人有余音穿梁,三日不绝的感受
银行业务中的常规概念理解
清风九里:
讲得很好 前辈 但是借贷那块儿没太理解
linux网络编程中select/poll/epoll的比较分析
lianxiang_biancheng
回复
岁月小龙:
谢谢关注!
linux网络编程中select/poll/epoll的比较分析
岁月小龙:
太强大了
百度2012年春季实习生校园招聘笔试题和答案
没有昵称阿:
找兄弟单词,质数互乘可以做,但是效率不高!用字典树!
您愿意向朋友推荐“博客详情页”吗?
强烈不推荐
不推荐
一般般
推荐
强烈推荐
提交
最新文章
银行业务中的常规概念理解
公司信贷基础知识介绍
统计ip的发送频率和该ip发送的有效消息(去除相似消息)的数目
2013年11篇
2012年15篇
目录
目录
分类专栏
数据结构/算法
4篇
C++
2篇
Linux
9篇
.NET
Python
7篇
hadoop
1篇
node.js
1篇
PHP
1篇
金融知识
2篇
目录
评论
被折叠的 条评论
为什么被折叠?
到【灌水乐园】发言
查看更多评论
打赏作者
lianxiang_biancheng
你的鼓励将是我创作的最大动力
¥2
¥4
¥6
¥10
¥20
输入1-500的整数
余额支付
(余额:-- )
扫码支付
扫码支付:¥2
获取中
扫码支付
您的余额不足,请更换扫码支付或充值
打赏作者
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额
抵扣说明:
1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。 2.余额无法直接购买下载,可以购买VIP、C币套餐、付费专栏及课程。
余额充值