Jdbc connection hive0.14, jdbchive0.14
Jdbc connection to hive0.14
The latest version on the official website is hive0.13. To download the latest hive, go to git to clone it.
Hive0.14 supports direct insertion.
Now we will provide an example of jdbc connection to hive0.14.
Jar package:
Do not introduce a separate hive-integrated jar because it contains several jar packages in tomcat. When a hive project is created, the hive integration package cannot be loaded due to a conflict.
1. Tools for hive connection:
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;
/**
* The tool class used to connect to the database is defined as non-inherited and private access.
*/
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 );
}
}
/**
* Obtain the database connection
*
* @ 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;
}
/**
* Release resources
*
* @ 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 is a class.
Public class Test {
Public static void main (String [] args) throws Exception {
Connection connection = DBTool. getConnection ();
System. out. println (connection );}
}
If the connection is successful, you have successfully connected to hive.
Hive jdbc connection non-default db
"Jdbc: hive: // localhost: 10000/default". Currently, the only dbname supported is "default ".
Writing or not writing dbname is useless. It may only support default.
Jdbc connection