Hive JDBC
1.導入pom依賴
<dependency>
<groupId>org.apache.hive</groupId>
<artifactId>hive-jdbc</artifactId>
<version>3.1.0</version>
</dependency>
<dependency>
<groupId>org.apache.hadoop</groupId>
<artifactId>hadoop-common</artifactId>
<version>2.6.5</version>
</dependency>
這裏用的最新的pom依賴
2.獲取JDBC Connection
public class MyHiveConnection {
private static String driverName = "org.apache.hive.jdbc.HiveDriver";
private static String url = "jdbc:hive2://10.10.10.205:10000";
private static String username = "hive";
private static String password = ""; //指定一個用戶運行查詢,忽略密碼
public static Connection getConnection(String url) throws Exception {
Connection hiveConnection = null;
try {
Class.forName(driverName);
hiveConnection = DriverManager.getConnection(url, username, password);
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
return hiveConnection;
}
public static void main(String[] args) throws Exception {
Connection conn = getConnection(url);
if(conn != null){
String schema = conn.getSchema();
System.out.println(schema);
conn.close();
}
else
System.out.println("沒連接上");
}
}
起初的代碼是這樣,第一步我想只是獲得JDBC的連接,後面再去寫一些增刪改查什麼的。
但是發現報錯:
java.sql.SQLException: Could not open client transport with JDBC Uri: jdbc:hive2://10.10.10.205:10000: Could not establish connection to jdbc:hive2://10.10.10.205:10000: Required field 'client_protocol' is unset! Struct:TOpenSessionReq(client_protocol:null, configuration:{set:hiveconf:hive.server2.thrift.resultset.default.fetch.size=1000, use:database=default})
檢查了一下代碼 發現應該沒有問題,但是連接不上很奇怪,那個參數沒設置,但是很明顯連接不需要參數這個參數。 沒有任何問題的情況下,我猜測是pom依賴版本太高了
降了下來之後,就可以了。
<dependency>
<groupId>org.apache.hive</groupId>
<artifactId>hive-jdbc</artifactId>
<version>1.1.0</version>
</dependency>
這樣就可以了