title: 详解PHP连接Impala安装与配置
date: 2017-12-08
categories:
- 技术
tags: - PHP
- impala
Impala的SQL语法参考
https://www.cloudera.com/documentation/enterprise/latest/topics/impala_langref_sql.html
PHP通过Thrift连接Impala
安装thrift服务
- Thrift最初由Facebook开发用做系统内各语言之间的RPC通信.
- Thrift是一款可伸缩跨语言的服务开发框架, 该框架已经开源并且加入的Apache项目.
- Thrift主要功能是: 通过自定义的Interface Definition Language(IDL), 可以创建基于RPC的客户端和服务端的服务代码.
- 数据和服务代码的生成是通过Thrift内置的代码生成器来实现的, Thrift的跨语言性体现在:它可以生成C++/Java/Python/PHP/Ruby/Erlang/Perl/Haskell/C#/Cocoa/JavaScript/Node.js/Smalltalk/OCaml/Delphi等语言的代码,且它们之间可以进行透明的通信.
[root@localhost tools]# rpm -ivh thrift-0.9.0-28.1.i686.rpm
warning: thrift-0.9.0-28.1.i686.rpm: Header V3 DSA/SHA1 Signature, key ID a949b429: NOKEY
error: Failed dependencies:
libc.so.6 is needed by thrift-0.9.0-28.1.i686
libc.so.6(GLIBC_2.0) is needed by thrift-0.9.0-28.1.i686
libc.so.6(GLIBC_2.1) is needed by thrift-0.9.0-28.1.i686
libc.so.6(GLIBC_2.1.3) is needed by thrift-0.9.0-28.1.i686
libc.so.6(GLIBC_2.3) is needed by thrift-0.9.0-28.1.i686
libc.so.6(GLIBC_2.3.4) is needed by thrift-0.9.0-28.1.i686
libc.so.6(GLIBC_2.4) is needed by thrift-0.9.0-28.1.i686
libgcc_s.so.1 is needed by thrift-0.9.0-28.1.i686
libgcc_s.so.1(GCC_3.0) is needed by thrift-0.9.0-28.1.i686
libm.so.6 is needed by thrift-0.9.0-28.1.i686
libstdc++.so.6 is needed by thrift-0.9.0-28.1.i686
libstdc++.so.6(CXXABI_1.3) is needed by thrift-0.9.0-28.1.i686
libstdc++.so.6(CXXABI_1.3.1) is needed by thrift-0.9.0-28.1.i686
libstdc++.so.6(GLIBCXX_3.4) is needed by thrift-0.9.0-28.1.i686
libstdc++.so.6(GLIBCXX_3.4.11) is needed by thrift-0.9.0-28.1.i686
libstdc++.so.6(GLIBCXX_3.4.9) is needed by thrift-0.9.0-28.1.i686
#报错, 缺少相关依赖.
#解决libc.so.6依赖:
[root@localhost tools]# yum list glibc*
[root@localhost tools]# yum install glibc.i686
#解决libgcc_s.so.1依赖:到https://rpmfind.net/linux/rpm2html/search.php?query=libgcc_s.so.1下载相关依赖rpm包
[root@localhost tools]# rpm -ivh libgcc-4.4.7-18.el6.x86_64.rpm
[root@localhost tools]# rpm -ivh libgcc-4.4.7-18.el6.i686.rpm
#解决libstdc++.so.6依赖:
#到https://rpmfind.net/linux/rpm2html/search.php?query=libstdc++.so.6下载相关依赖rpm包
[root@localhost tools]# rpm -ivh libstdc++-4.4.7-18.el6.i686.rpm
#最后重新安装即可:
[root@localhost tools]# rpm -ivh thrift-0.9.0-28.1.i686.rpm
warning: thrift-0.9.0-28.1.i686.rpm: Header V3 DSA/SHA1 Signature, key ID a949b429: NOKEY
Preparing... ########################################### [100%]
1:thrift ########################################### [100%]
检查是否安装成功
a. 首先创建Thrift的语法规则文件, 命名为server.thrift, 内容如下:
struct message
{
i32 seqId,
string content
}
service serDemo
{
void put(message msg)
}
b. 然后shell下执行命令:
# thrift -gen php server.thrift
该语句用于创建php服务框架, 创建成功后会在该目录下生成gen-php文件夹.
impala的SQL查询参考
http://www.cloudera.com/documentation/archive/impala/2-x/2-1-x/topics/impala_select.html
连接PHP例子参考
https://github.com/Automattic/php-thrift-sql
# git clone https://github.com/Automattic/php-thrift-sql.git
# cd php-thrift-sql/
# php -c php.ini build.php 【重新生成ThriftSQL.phar文件】
# vim test.php
内容如下:
<?php
// Load this lib
require_once __DIR__ . '/ThriftSQL.phar';
// 1.Try out a Hive query
$hive = new \ThriftSQL\Hive('192.168.8.207', 21050);
$hiveTables = $hive
->setSasl(false) // To turn SASL auth off, on by default
->connect()
->queryAndFetchAll('SHOW TABLES');
print_r($hiveTables);
// 2.Try out an Impala query
$impala = new \ThriftSQL\Impala('192.168.8.207');
$impalaTables = $impala
->connect()
->queryAndFetchAll('SHOW TABLES');
print_r($impalaTables);
// 3.Try out an Impala query
$impalaDatas = $impala
->connect()
->queryAndFetchAll('select * from db_mcfx_log.t