我正在用 Python 编写一些 ETL 流程,对于该流程的一部分,使用 Hive。根据文档,Cloudera 的 impyla 客户端可与 Impala 和 Hive 一起使用。
根据我的经验,客户端为 Impala 工作,但是当我尝试连接到 Hive 时挂起:
from impala.dbapi import connect
conn = connect(host='host_running_hs2_service', port=10000, user='awoolford', password='Bzzzzz')
cursor = conn.cursor() <- hangs here
cursor.execute('show tables')
results = cursor.fetchall()
print results
Run Code Online (Sandbox Code Playgroud)
如果我单步执行代码,它会在尝试打开会话时挂起(hiveserver2.py 的第 873 行)。
起初,我怀疑可能是防火墙端口阻止了连接,因此我尝试使用 Java 进行连接。令我惊讶的是,这有效:
public class Main {
private static String driverName = "org.apache.hive.jdbc.HiveDriver";
public static void main(String[] args) throws SQLException {
try {
Class.forName(driverName);
} catch (ClassNotFoundException e) {
e.printStackTrace();
System.exit(1);
}
Connection connection = DriverManager.getConnection("jdbc:hive2://host_running_hs2_service:10000/default", "awoolford", "Bzzzzz");
Statement statement = connection.createStatement();
ResultSet resultSet = statement.executeQuery("SHOW TABLES");
while (resultSet.next()) {
System.out.println(resultSet.getString(1));
}
}
}
Run Code Online (Sandbox Code Playgroud)
由于 Hive 和 Python 是如此常用的技术,我很想知道是否有其他人遇到过这个问题,如果有,你做了什么来解决它?
版本:
我尝试了 Dropbox 的PyHive包,它运行得很好:
from pyhive import hive
conn = hive.Connection(host="host_running_hs2_service", port=10000, username="awoolford")
cursor = conn.cursor()
cursor.execute("SHOW TABLES")
for table in cursor.fetchall():
print table
Run Code Online (Sandbox Code Playgroud)
我不确定为什么 Impyla 客户端不起作用,但至少我们可以继续前进。