1. 程式人生 > 實用技巧 >Python 3.7 使用pyhive (坑)

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:\Users\Administrator\AppData\Local\Programs\Python\Python36\lib\site-p

ackages\thrift_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了。