基于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项目地址之外很难再找到其他的,所以 如果你有好的文章或者教程的话,欢迎留言


【技术服务】,详情点击查看: https://mp.weixin.qq.com/s/PtX9ukKRBmazAWARprGIAg

扫一扫 关注微信公众号!号主 专注于搜索和推荐系统,尝试使用算法去更好的服务于用户,包括但不局限于机器学习,深度学习,强化学习,自然语言理解,知识图谱,还不定时分享技术,资料,思考等文章!

评论 8
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值