【Python】window10 python connect hive

需要在window10下,使用python去連hive,獲取在hive中的數據,然後訓練模型,將模型訓練好後,相關模型結果寫回到hive進行持久化。目的是不讓數據在本地建模環境有存儲!!!

 

環境:

操作系統 window 10
python python 3.6.5
hive 1.2.1

 

python所需要的第三方依賴

包名 版本 安裝命令
bitarray 0.8.1 pip install bitarray==0.8.1 -i https://pypi.tuna.tsinghua.edu.cn/simple
impyla 0.16.0 pip install impyla==0.16.0 -i https://pypi.tuna.tsinghua.edu.cn/simple
pure_sasl 0.5.1 pip install pure_sasl==0.5.1 -i https://pypi.tuna.tsinghua.edu.cn/simple
thrift 0.9.3 pip install thrift==0.9.3 -i https://pypi.tuna.tsinghua.edu.cn/simple
thrift_sasl 0.2.1 pip install thrift_sasl==0.2.1 -i https://pypi.tuna.tsinghua.edu.cn/simple
thriftpy 0.3.9 pip install thriftpy==0.3.9 -i https://pypi.tuna.tsinghua.edu.cn/simple
thriftpy2 0.4.7 pip insta thriftpy2==0.4.7 -i https://pypi.tuna.tsinghua.edu.cn/simple

ps 暫時在實踐過程中發現的包衝突問題如下:

1、impyla 對 thrift 庫的要求是<=0.9.3,而pyhive 0.6.1不兼容thrift 0.9.3

2、thrift_sasl保證在0.2.1及以下,要不然會報錯誤。待會後面會介紹報的錯誤

3、有了thrift_sasl後,需要將另一個第三方包卸載。sasl需要卸載。這個包在使用pyhive的時候會有所使用到。

 

連接hive的代碼

from impala.dbapi import connect
from impala.util import as_pandas

conn = connect(host='*.*.*.*', port=10000, auth_mechanism='PLAIN', database='src')
cursor = conn.cursor()
cursor.execute('select * from table_name limit 10')

print(cursor.description) #打印字段名
#print(as_pandas(cursor)) #打印結果

#轉化爲dataframe
df = as_pandas(cursor)
print(df)

cursor.close()
conn.close()

運行結果如下:

 

在如上過程中。遇到如下一些坑。記錄下,以防後續備忘

 

問題一:

解決方案:

conn = connect(host='*.*.*.*', port=10000, database='src')

#在這句話中,添加上auth_mechanism='NONE'

conn = connect(host='*.*.*.*', port=10000, auth_mechanism='NONE', database='src')

 

問題二:

解決方案:

conn = connect(host='*.*.*.*', port=10000, auth_mechanism='NONE', database='src')

#將如上這句代碼中的auth_mechanism='NONE'修改爲auth_mechanism='PLAIN'

conn = connect(host='*.*.*.*', port=10000, auth_mechanism='PLAIN', database='src')

我之所以將auth_mechanism='NONE'的原因是由於hive-site.xml這個文件中的如下參數是NONE

代碼中應該修改爲auth_mechanism='PLAIN'

 

問題三:

 

解決方案:

#將sasl該第三方包卸載。
pip uninstall sasl

 

問題四:

解決方案:

將thrift_sasl該包中的__init__.py第94行代碼修改下下

修改後如下:

 

問題五:

解決方案

#將thrift_sasl 0.3.0卸載,安裝0.2.1
pip install thrift_sasl==0.2.1

 

至此。問題都解決。看到希望!!!

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章