- 说明:impala的安装的是基于hive,使用的也是hive的源数据(metastore),故连接使用impala进行数据操作同样也是连接的HiveServer2服务,在连接impala配置上的参数和连接hive时配置的基本参数相同;区别于使用的库不同。
- 1、安装依赖库
pip install impyla
# 安装impala所依赖的thrift库
pip install thrift
pip install thrift-sasl
- 2、测试连接
from impala.dbapi import connect
# 读取数据
def test_connection(sql):
# 创建hive连接
conn = connect(host='IP', # HiveServer2主机的IP地址
port=10000, # HiveServer2服务端口号
user='root', # 连接hive数据库的用户名
database='databseName', # 具体数据库名
auth_mechanism='NOSASL' # 客户端的认证模式
)
cur = conn.cursor()
# 执行查询
cur.execute(sql)
# 获取查询结果
results = cur.fetchall()
return results
sql = "show tables"
res = test_connection(sql)
注:如果之前连接过hive就可以看出,除了使用的库不同和初始化连接参数上有细微差别外,其他没有什么不同
- 相关hive的配置连接操作见:Python连接Hive相关配置_MY测试之道的博客-CSDN博客