一、pom文件:
导入hivejdbc驱动
<dependency>
<groupId>org.apache.hive</groupId>
<artifactId>hive-jdbc</artifactId>
<version>3.1.2</version>
</dependency>
二、代码
import java.sql.*;
public class HiveTest {
final static String DRIVER ="org.apache.hive.jdbc.HiveDriver";
final static String DATABASE_PATH
="jdbc:hive2://192.168.77.10:10000";
final static String USER_NAME ="root";
final static String PASSWORD ="";
public static void main(String[] args)throws SQLException {
Connection conn =null;
Statement stmt =null;
ResultSet rs =null;
String hql ="select count(*) from person";
int count =0;
try{
//1.注册驱动;
Class.forName(DRIVER);
//2.创建连接
conn= DriverManager.getConnection(DATABASE_PATH, USER_NAME, PASSWORD);
//3创建statement对象
stmt = conn.createStatement();
//4执行HQL语句
rs = stmt.executeQuery(hql);
//5.处理结果集
if(rs.next()){
count = rs.getInt(1);
}
}catch(Exception e){
e.printStackTrace();
}finally{
//6.关闭连接
if(rs !=null){ rs.close(); }
if(stmt !=null){ stmt.close(); }
if(conn !=null){ conn.close(); }
}
System.out.println(count);
}
}
运行结果: