Home  >  Article  >  Database  >  jdbc连接hive0.14

jdbc连接hive0.14

WBOY
WBOYOriginal
2016-06-07 16:09:491329browse

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包:

a婘籬櫰瓖婘骺a婘喀歓rF爕?~+!Ζ婍崼诓〣農亘燏i妀h诮??饨晏jv爖块?籧j霈猄雤柅y原j爪z{Ζ婍崼诓㏎z衰?歜殮+?痡圣I呐签?'b殮+?痡隻?贶xW喀熼?楗╧y数据库的工具类,被定义成不可继承且是私有访问

*/

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了。

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn