jdbc连接hive0.14版本 目前官网最新版本是hive0.13,要想下载最新的hive得去git上去clone一个。 hive0.14最大特点是支持直接插入。 现在做一个jdbc连接hive0.14的例子。 需要的jar包: 数据库的工具类,被定义成不可继承且是私有访问 */ public final class d
jdbc连接hive0.14版本
目前官网最新版本是hive0.13,要想下载最新的hive得去git上去clone一个。
hive0.14最大特点是支持直接插入。
现在做一个jdbc连接hive0.14的例子。
需要的jar包:
数据库的工具类,被定义成不可继承且是私有访问
*/
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了。