如果想要通过Java进行访问,首先要在引用一下三个Jar包:
<dependency>
<groupId>org.apache.hive</groupId>
<artifactId>hive-jdbc</artifactId>
<version>1.0.0</version>
</dependency>
<dependency>
<groupId>org.apache.hadoop</groupId>
<artifactId>hadoop-common</artifactId>
<version>2.4.1</version>
</dependency>
<dependency>
<groupId>jdk.tools</groupId>
<artifactId>jdk.tools</artifactId>
<version>1.8</version>
<scope>system</scope>
<systemPath>${JAVA_HOME}/lib/tools.jar</systemPath>
</dependency>
需要注意的是,包的版本一定要确认好,切勿版本过高 我采用的是CDH5.12.0 版本
如果发生以下错误:
org.apache.thrift.TApplicationException: Required field 'client_protocol' is unset! Struct:TOpenSessionReq(client_protocol:null)
则极可能的原因是你项目的hive-jdbc版本和服务器不一致的原因造成的,替换成和服务器一致的版本就可以了,
测试代码
public static Connection getHiveConnection() {
Connection conn = null;
try {
Class.forName("org.apache.hive.jdbc.HiveDriver");
conn = DriverManager.getConnection("jdbc:hive2://cdh-hadoop1:10000/default", "root", "");
} catch (ClassNotFoundException | SQLException e1) {
e1.printStackTrace();
}
return conn;
}
/**
*
* 通过传入sql从hive中获取数据
* @param sql
* @return 返回列表信息信息
* @throws SQLException
* @throws ClassNotFoundException
*/
public static List<String> getMessageFromHive(String db, String sql) {
// 这个方法只有下面两种操作
// show table partitions
// show create table
List<String> list = new ArrayList<>();
try {
Connection con = getHiveConnection();
Statement stmt = con.createStatement();
//stmt.executeQuery("use "+db);
ResultSet rs = stmt.executeQuery(sql);
while (rs.next()) {
list.add(rs.getString(1));
}
rs.close();
stmt.close();
con.close();
} catch (SQLException e) {
e.printStackTrace();
System.exit(-1);
}
return list;
}
public static void main(String[] args) {
getMessageFromHive("i8ji", "show partitions i8ji.ods_t_db_to_hive_list_arc ");
}