Python 3.7 使用pyhive (坑)

参考 zfno11:& https://ask.hellobi.com/blog/ysfyb/18251

HiveServer是一种可选服务,允许远程客户端可以使用各种编程语言向Hive提交请求并检索结果。

使用Impala 连接hive

安装前需把相关的包卸载干净,然后重新安装对应的版本

pip3 uninstall sasl  #运行时报错module 'sasl' has no attribute 'Client',说明该包没有删除干净,需要手动删除文件
pip3 install impyla      
pip3 install pure-sasl
pip3 install thrift_sasl==0.2.1 --no-deps

连接代码:

from impala.dbapi import connect
conn = connect(host="xxx.xxx.xx.xxx", port=10000, user="root", auth_mechanism="PLAIN", password='dfghjkl', database="xxx")
cur=conn.cursor()
cur.execute('SHOW TABLES')
for result in cur.fetchall():
    print(result)

 报错:TypeError: can't concat str to bytes    需要在File "C:UsersAdministratorAppDataLocalProgramsPythonPython36libsite-p

ackages hrift_sasl\__init__.py"第94行代码

修改源代码:

 def _send_message(self, status, body):
    header = struct.pack(">BI", status, len(body))
    self._trans.write(header + body)
    self._trans.flush()

改为:

 def _send_message(self, status, body):
    header = struct.pack(">BI", status, len(body))
    if(type(body) is str):
        body = body.encode()
    self._trans.write(header + body)
    self._trans.flush()

修改之后就OK了。

原文地址:https://www.cnblogs.com/wqzn/p/13292816.html