可以通过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
# 连接没有密码和没有Kerberos认证的hive
conn = hive.Connection(host='192.168.146.133', port=10000, username='root', database='tmp')#host主机ip,port:端口号,username:用户名,database:使用的数据库名称
# 连接有密码和Kerberos认证的hive
#conn = hive.Connection(host='192.168.146.133', port=10000, username='root', password='root',database='tmp',
auth='LDAP')
cursor=conn.cursor()
cursor.execute('select * from tmp.ods_ext_csv_unc_tmp_customer_sale')#执行查询
for result in cursor.fetchall():
print(result) #将查询结果打印出来
conn.close()