天天看点

Linux环境下 python安装hive和impala以及如何使用

环境说明

系统为:centos系统

在安装hive之前

请更新pip并更换镜像,这样的目的是为了使下载速度变成光速下载。

pip install pip -U  
pip config set global.index-url https://pypi.tuna.tsinghua.edu.cn/simple 
           

安装pyhive 

如果直接安装 pip install thrift-sasl , 会报错加上版本号就ok了。

pip install sasl
pip install thrift
pip install thrift-sasl==0.3.0
pip install PyHive
           

使用hive

import pandas as pd
from pyhive import hive
conn = hive.Connection(host='****', port=****, username='****', database='****')
cursor = conn.cursor()
sql_hive ="""
    select  *
    from table
    """
cursor.execute(sql_hive)
data = cursor.fetchall()
results = pd.DataFrame(data)
print(results.shape)
           

安装impala

在安装上面的基础上进操作

pip install impyla
           

使用impala

from impala.dbapi import connect as impala_connect
from impala.util import as_pandas
def impala_db(sql):
	conn = impala_connect(host ='****',port = ****)
	cur = conn.cursor()
	cur.execute(sql)
	results = as_pandas(cur)
	print(results.shape)
sql ="""
    SELECT *
    FROM table
    """
impala_db(sql)