使用 Hive 的一个通用的场景是:应用通过 JDBC 连接 Hive ThriftServe2,通过 SQL 查询 Hive 数据
编辑 pom.xml 文件,添加依赖:
<dependency>
<groupId>org.apache.hive</groupId>
<artifactId>hive-jdbc</artifactId>
<version>${hive.version}</version>
</dependency>
<dependency>
<groupId>org.apache.hadoop</groupId>
<artifactId>hadoop-common</artifactId>
<version>${hadoop.version}</version>
</dependency>
如果不添加 hadoop-common 依赖,会提示找不到 org.apache.hadoop.conf.Configuration
类错误
显示所有数据库:
try (Connection connection = DriverManager.getConnection(url); Statement statement = connection.createStatement(); ResultSet rs = statement.executeQuery("show databases")) {
rs.getString("database_name");
}
查询抛异常:
org.apache.thrift.TApplicationException: Required field 'client_protocol' is unset! Struct:TOpenSessionReq(client_protocol:null, configuration:{use:database=default})
Hive 版本:1.1.0
Hive JDBC 版本:1.2.1
导致该问题的原因是 Hive JDBC 版本过高,修改为和 Hive 版本一致即可