Python连接Hive(基于impyla)

如何安装impyla请看我这篇博文:

https://blog.csdn.net/a6822342/article/details/80841056


以下是我的一些包的版本

thirftpy 0.3.9

thirft-sasl 0.2.0

thirft 0.11.0

sasl 0.2.1

pure-sasl 0.3.0

impyla 0.14.1

到这里,我默认大家是装好impaly了。那么我们在pycharm中运行以下程序

# coding=gbk
from impala.dbapi import connect
#需要注意的是这里的auth_mechanism必须有,默认是NOSASL,但database不必须,user也不必须
conn = connect(host='服务器ip', port=10000, database='default',auth_mechanism='NOSASL',user='在hdfs-site中设定的用户名(如果不懂,请看https://blog.csdn.net/a6822342/article/details/80697919)')
cur = conn.cursor()

cur.execute('SHOW DATABASES')
print(cur.fetchall())

cur.execute('SHOW Tables')
print(cur.fetchall())

一开始我运行这段代码的时候报错

ThriftPy does not support generating module with path in protocol 'f'

这里修改报错代码parser文件中第488行

将if url_scheme == '': 注释掉,修改为if len(url_scheme) <= 1:


然后运行,继续报错:'TSocket' object has no attribute 'isOpen'

这里需要将thrift-sasl改为0.2.0版本。


重新运行,然后继续报错:Error in sasl_client_start (-4) SASL(-4): no mechanism available:no mechanism available: Unable to find a callback: 2'")

这里我将hive-site.xml文件的hive.server2.authentication和代码中(原来是PLAIN),一起改为NOSASL。

然后重启hive远程服务

hive --service hiveserver2 

再执行一遍代码,结果如图所示:


和命令行中执行相同操作得到的结果一致:


执行成功,希望能够帮助到大家。

此篇文章是我原创,转载请与我联系,谢谢。


猜你喜欢

转载自blog.csdn.net/a6822342/article/details/80844072
今日推荐