python操作hive

1 安装依赖包

pip install sasl
pip install thrift
pip install thrift-sasl
pip install PyHive

注意事项:安装sasl可能会安装失败, 这里可以直接到官网下载

2 案例

from pyhive import hive
import pandas as pd

'''
	读取hive表
'''
def select_pyhive(sql):
	# 配置仅显示列名(默认显示 表名.列名)
    hive_config={'hive.resultset.use.unique.column.names': 'false'}
	
    # 创建hive连接
    conn = hive.Connection(host='localhost', 
                           port=10000,  // hiveserver2服务记得开启
                           auth='CUSTOM', // 注意:如果hive jdbc配置密码了 这个参数需要设置
                           username='***', 
                           password='***',
                           configuration = hive_config,
                           database='ads')
    cur = conn.cursor()
    try:
        # c = cur.fetchall()
        df = pd.read_sql(sql, conn)
        return df
    finally:
        if conn:
            conn.close()


if __name__ == '__main__':
	sql='select * from ads_table1 limit 10'
	df = select_pyhive(sql)
	print(df)

3. 遇到的问题及解决方案

注意:TTransportException: Bad status: 3 (b'Error validating the login') 遇到这个报错,就是输入的账号密码报错了(如果不确定hive jdbc账号密码有问题 可以使用dbeaver连接测试一下)

你可能感兴趣的:(hive,python,大数据)