jdbc串連hive0.14,jdbchive0.14
Jdbc串連hive0.14版本
目前官網最新版本是hive0.13,要想下載最新的hive得去git上去clone一個。
Hive0.14最大特點是支援直接插入。
現在做一個jdbc串連hive0.14的例子。
需要的jar包:
不要去引入單獨的一個整合hive的jar因為那個包含了tomcat裡面的幾個jar包。當建立hive工程時,會衝突導致hive的整合套件載入不上。
1.hive串連的工具類:
package com.fish;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ResourceBundle;
/**
* 串連資料庫的工具類,被定義成不可繼承且是私人訪問
*/
public final class DBTool {
final private static String OPTION_FILE_NAME = "hivedatabase";
private static Connection conn;
static ResourceBundle res;
static {
res = ResourceBundle.getBundle(OPTION_FILE_NAME);
try {
String driver = res.getString("jdbc.driver");
Class.forName(driver);
} catch (ClassNotFoundException e) {
e.printStackTrace();
throw new RuntimeException(e);
}
}
/**
* 擷取資料庫的串連
*
* @return conn
*/
public static Connection getConnection() {
if (null == conn) {
try {
String url = res.getString("jdbc.url");
String user = res.getString("jdbc.username");
String password = res.getString("jdbc.password");
conn = DriverManager.getConnection(url, user, password);
} catch (SQLException e) {
e.printStackTrace();
throw new RuntimeException(e);
}
}
return conn;
}
/**
* 釋放資源
*
* @param conn
* @param pstmt
* @param rs
*/
public static void closeJDBC(Connection conn, PreparedStatement pstmt,
ResultSet rs) {
if (null != rs) {
try {
rs.close();
} catch (SQLException e) {
e.printStackTrace();
throw new RuntimeException(e);
} finally {
if (null != pstmt) {
try {
pstmt.close();
} catch (SQLException e) {
e.printStackTrace();
throw new RuntimeException(e);
} finally {
if (null != conn) {
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
throw new RuntimeException(e);
}
}
}
}
}
}
}
}
2.hivedatabase.properties
#hive database setting
jdbc.type=hive
jdbc.driver=org.apache.hive.jdbc.HiveDriver
jdbc.url=jdbc:hive2://192.168.2.150:10000/default
jdbc.username=hadoop
jdbc.password=
3.test是類
public class Test {
public static void main(String[] args) throws Exception {
Connection connection = DBTool.getConnection();
System.out.println(connection);}
}
如果能串連通說明你已經成功串連上hive了。
HIVE JDBC 串連非default db
"jdbc:hive://localhost:10000/default". Currently, the only dbname supported is "default".
寫不寫 dbname都是無用的 他潛在只支援 default
jdbc串連