参考博客:
Hiveserver2参考:
https://blog.csdn.net/qq_41955099/article/details/89284009
Hive-JDBC参考:
https://blog.csdn.net/u010429286/article/details/78780145
HiveServer2连接问题:
https://blog.csdn.net/baidu_17353319/article/details/80177244
本人构建maven项目的pom.xml文件依赖如下:
<dependencies>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.48</version>
</dependency>
<dependency>
<groupId>org.apache.hive</groupId>
<artifactId>hive-jdbc</artifactId>
<exclusions>
<exclusion>
<groupId>org.apache.hive</groupId>
<artifactId>hive-service-rpc</artifactId>
</exclusion>
<exclusion>
<groupId>org.apache.hive</groupId>
<artifactId>hive-service</artifactId>
</exclusion>
</exclusions>
<version>1.1.0</version>
</dependency>
<dependency>
<groupId>org.apache.hive</groupId>
<artifactId>hive-service</artifactId>
<version>1.1.0</version>
</dependency>
<dependency>
<groupId>org.apache.hadoop</groupId>
<artifactId>hadoop-common</artifactId>
<version>2.6.0-cdh5.15.1</version>
</dependency>
</dependencies>
Hive-JDBC的测试Demo:
public class HiveClient {
private static String driverName = "org.apache.hive.jdbc.HiveDriver";
public static void main(String[] args) throws SQLException {
try{
Class.forName(driverName);
} catch (ClassNotFoundException e) {
e.printStackTrace();
System.exit(1);
}
Connection connection = DriverManager.getConnection("jdbc:hive2://hadoop001:10000/test","","");
//默认没有用户名和密码,且CDH中Hiveserver2默认是开启的,而apache原生hive需要手动启动。
Statement statement = connection.createStatement();
String sql = "show tables";
ResultSet resultSet = statement.executeQuery(sql);
while(resultSet.next()){
System.out.println(resultSet.getString(1));
}
resultSet.close();
statement.close();
connection.close();
}
}