可以通過pyhive連接hive
還可以通過impyla 詳見 impyla連接hive
1.安裝相關依賴
目前python3連接hive的方法主要是使用pyhive包,但是要安裝pyhive也不是那麼容易的事情,因爲pyhive要使用系統底層模塊,所以就要先安裝對應的模塊。
sudo yum install cyrus-sasl-devel
sudo yum install gcc-c++
pip3 install sasl
pip3 install thrift
pip3 install thrift-sasl
pip3 install PyHive
2.連接hive查詢數據
# -*- coding: utf-8 -*-
# @Time : 2018/11/10 下午10:28
# @Author : Einstein Yang!!
# @Nickname : 穿着開襠褲上大學
# @FileName: python_hive.py
# @Software: PyCharm
# @PythonVersion: python3.5
# @Blog :https://blog.csdn.net/weixin_41734687
from pyhive import hive
conn = hive.Connection(host='192.168.146.133', port=10000, username='root', database='tmp')#host主機ip,port:端口號,username:用戶名,database:使用的數據庫名稱
cursor=conn.cursor()
cursor.execute('select * from tmp.ods_ext_csv_unc_tmp_customer_sale')#執行查詢
for result in cursor.fetchall():
print(result) #將查詢結果打印出來
conn.close()