一.服务器配置
- 配置beeline
- 启动hiveserver2并置后台运行
nohup hive --service hiveserver2 > /dev/null 2>&1 &
二.pom.xml
<dependencies>
<dependency>
<groupId>org.apache.hive</groupId>
<artifactId>hive-jdbc</artifactId>
<version>2.1.1</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>RELEASE</version>
</dependency>
</dependencies>
三.Java JDBC操作
public class HiveJDBC {
private static String driverName = "org.apache.hive.jdbc.HiveDriver";
private static String url = "jdbc:hive2://node3:10000/mytest";
private static String user = "root";
private static String password = "123456";
private static Connection conn = null;
private static Statement stmt = null;
private static ResultSet rs = null;
public static void main(String[] args) throws Exception {
Class.forName(driverName);
conn = DriverManager.getConnection(url,user,password);
stmt = conn.createStatement();
String sql = "select * from t_test";
rs = stmt.executeQuery(sql);
while(rs.next()){
System.out.print(rs.getString("name"));
}
if ( rs != null) {
rs.close();
}
if (stmt != null) {
stmt.close();
}
if (conn != null) {
conn.close();
}
}
}
Comment