基于python impyla的hive客户端
打开微信扫一扫,关注微信公众号【数据与算法联盟】
转载请注明出处:http://blog.csdn.net/gamer_gyt
博主微博:http://weibo.com/234654758
Github:https://github.com/thinkgamer
写在前边的话
在hive部署的时候我们谈过hive的三种访问方式
- CLI(shell 终端)
- HWI (Hive的web页面操作)
- thrift (启动hiveserver2服务,基于thrift建立hive的操作)
第三种thrift方式的,网友们进行了封装,目前有三个广负盛名的python backage
小主本地环境介绍
- CentOS6.5 /64 位
- python 2.7
这里小主做的web可视化界面比较菜看两张截图吧(这个是HIW项目的一部分 github-HIW)
部署
impyla必须的依赖包括:
- six
- bit_array
- thriftpy(python2.x则是thrift)
为了支持Hive还需要以下两个包:
- sasl
- thrift_sasl
先安装依赖
pip install thrift_sasl pip install sasl
报错:
gcc: error trying to exec 'cc1plus': execvp: No such file or directory error: command 'gcc' failed with exit status 1
解决办法:
缺少g++文件
yum install gcc-c++ (centos中g++叫gcc-c++,如果直接安装g++会出现No package g++ available. Error: Nothing to do )
又报错:
UnicodeDecodeError: 'ascii' codec can't decode byte 0xe2 in position 73: ordinal not i
解决办法:
在python安装目录的site-packages目录下,创建sitecustomize.py,python会自动加载,添加
import sys sys.setdefaultencoding('utf-8') 如果涉及到中文只需将utf-8修改为gb2312 即可
又报错:
sasl/saslwrapper.h:22:23: error: sasl/sasl.h: No such file or directory
解决办法
centos解决办法:
yum install gcc-c++ python-devel.x86_64 cyrus-sasl-devel.x86_64
ubuntu/deepin(小主自己的是deepin)
sudo apt-get install libsasl2-dev
然后执行
pip install thrift_sasl pip install sasl
OK
pip命令安装:
pip install impyla
报错:
bitarray/_bitarray.c:9:20: fatal error: Python.h: 没有那个文件或目录 compilation terminated. error: command 'x86_64-linux-gnu-gcc' failed with exit status 1
解决办法:
由于小主的是deepin 15.2 64位,python版本为2.7,其对应的解决办法是
sudo apt-get install python-dev
python3.X版本对应的命令为
sudo apt-get install python3-dev
然后在执行安装命令即可
pip install impyla
一个小Demo
注意执行该程序之前,必须启动hiveserver2服务
from impala.dbapi import connect #需要注意的是这里的auth_mechanism必须有,但database不必须 conn = connect(host='127.0.0.1', port=10000, database='default', auth_mechanism='PLAIN') cur = conn.cursor() cur.execute('SHOW DATABASES') print(cur.fetchall()) cur.execute('SHOW Tables') print(cur.fetchall())
关于auth_mechanism解释:
auth_mechanism的值取决于hive-site.xml里边的一个配置
<name>hive.server2.authentication</name> <value>NOSASL</value>
auth_mechanism的值默认是NONE,还可以是“NOSASL”,“PLAIN”,“KERBEROS”,“LDAP”
对hql进行连续处理
很简单的应用场景便是我们要处理的N句SQL的连续体,而不是单句的SQL语句,我这里进行的处理办法是切分,使用python的split,然后再进行单句执行
#连接hive def conn(): con = connect(host="192.168.132.27",port=10000,auth_mechanism="PLAIN") cur = con.cursor() return cur #执行HQL语句 def run_hql(sql): sql_list = sql[:-1].split(";") cur=conn() for s in sql_list: cur.execute(s) result=cur.fetchall() return result if __name__=="__main__": sql="show databases;use default;show tables;" print run_hql(sql)
运行结果也是没有问题的
[root@master1 HIW]# python hive/phive.py [('test',)]
后续若有可能的话,我会更新一些impyla的其他一些用法,因为网上关于impyla的资料很少,除了github项目地址之外很难再找到其他的,所以 如果你有好的文章或者教程的话,欢迎留言

低调大师中文资讯倾力打造互联网数据资讯、行业资源、电子商务、移动互联网、网络营销平台。
持续更新报道IT业界、互联网、市场资讯、驱动更新,是最及时权威的产业资讯及硬件资讯报道平台。
转载内容版权归作者及来源网站所有,本站原创内容转载请注明来源。
- 上一篇
elasticsearch spring 集成
elasticsearch spring 集成 项目清单 elasticsearch服务下载包括其中插件和分词 http://download.csdn.net/detail/u014201191/8809619 项目源码 资源文件 app.properties [html] view plain copy print? elasticsearch.esNodes=localhost:9300 elasticsearch.cluster.name=heroscluster app.xml [html] view plain copy print? <?xmlversion="1.0"encoding="UTF-8"?> <beansxmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:util="http://www.springframework.org/schema/util"...
- 下一篇
【MaxCompute学习】隐式转化的问题
有一次计算一个数据的百分比,想把小数结果取2位,并拼接一个百分号展示在结果报表中。用到的sql如下 selectconcat(round(10230/1497409,4)*100,'%')fromdual; 很奇怪局部数据并没有保留2位小数,比如上面的数据返回的是67.99999999999999 我计算了下上面的结果大概得到的数据为0.0068 selectconcat(round(0.0066,4)*100,'%')fromdual;--0.66% selectconcat(round(0.0067,4)*100,'%')fromdual;--0.67% selectconcat(round(0.0068,4)*100,'%')fromdual;--0.6799999999999999% sele
相关文章
文章评论
共有0条评论来说两句吧...
文章二维码
点击排行
推荐阅读
最新文章
- Docker安装Oracle12C,快速搭建Oracle学习环境
- Docker快速安装Oracle11G,搭建oracle11g学习环境
- Linux系统CentOS6、CentOS7手动修改IP地址
- Springboot2将连接池hikari替换为druid,体验最强大的数据库连接池
- SpringBoot2整合MyBatis,连接MySql数据库做增删改查操作
- CentOS6,7,8上安装Nginx,支持https2.0的开启
- SpringBoot2配置默认Tomcat设置,开启更多高级功能
- CentOS7,8上快速安装Gitea,搭建Git服务器
- CentOS7,CentOS8安装Elasticsearch6.8.6
- Windows10,CentOS7,CentOS8安装Nodejs环境