JDBC接続Hive(ClassNotFoundException、No suitable driver found、Could not open client transport wit)の使用
2723 ワード
package hive;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
public class App {
public static void main(String[] args) throws Exception {
Class.forName("org.apache.hive.jdbc.HiveDriver");
Connection conn = DriverManager.getConnection("jdbc:hive2://hadoop0:10000/default", "", "");
// default hive
Statement stmt = conn.createStatement();
String querySQL="SELECT * FROM default.t1";
ResultSet res = stmt.executeQuery(querySQL);
while (res.next()) {
System.out.println(res.getInt(1));
}
}
}
接続時の3つの問題
JDBCがHiveに接続する際に発生する可能性のある問題は、主にhiveバージョンの違いによるものです.
Class.forName("org.apache.hive.jdbc.HiveDriver");
ではなくClass.forName("org.apache.hadoop.hive.jdbc.HiveDriver");
hive
は使用されず、hiveserver 2命令を直接使用します.Linux shell:[root@hadoop0 ~]# hiveserver2 &
[1] Connect from Java to Hive using JDBC
[2] java.sql.SQLException: No suitable driver found for jdbc:hive://localhost:10000/default
[3] How to start hiveserver2 as service