我正在尝试使用Java中的JDBC和Kerberos身份验证连接到Hive。 为此,我提出了以下代码。
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.security.UserGroupInformation;
public class DbManager {
private static Connection hiveConnection;
// get Hive Connection
public static Connection qwtHiveConnection() throws IOException, SQLException {
System.out.println("Preparing Hive connection1");
Configuration conf = new Configuration();
conf.set("hadoop.security.authentication", "Kerberos");
UserGroupInformation.setConfiguration(conf);
UserGroupInformation.loginUserFromKeytab("username@HLC.ABC.QW.COM", "path of keytab");
// Hive Connection
try {
Class.forName("org.apache.hive.jdbc.HiveDriver");
if(hiveConnection == null) {
hiveConnection = DriverManager.getConnection("jdbc:hive2://abcd1dq000.hlc.abc.qw.com:10000/finance;principal=hive/_HOST@HLC.ABC.QW.COM", "usrname", "pwd");
return hiveConnection;
} else {
return hiveConnection;
}
} catch (ClassNotFoundException e) {
e.printStackTrace();
return null;
} catch (SQLException e) {
e.printStackTrace();
return null;
}
}
}
我从命令'hadoop version'中看到的Hadoop版本: Hadoop 2.7.3.2.6.2.0-205 此命令使用/usr/hdp/2.6.2.0-205/hadoop/hadoop-common-2.7.3.2.6.2.0-205.jar
运行我在项目中使用以下jar文件:
hive-jdbc-2.2.0.jar,
postgresql-9.4.1212.jar,
apache-commons.jar,
slf4j-api-1.7.25.jar,
hadoop-commons-2.6.2.jar,
guava-18.0.jar,
commons-configuration-1.9.jar,
commons-lang-2.6.jar,
hadoop-auth-2.6.2.jar,
hadoop-core-1.2.0.jar
当我运行jar时,我得到了例外:
Preparing Hive connection1
SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".
SLF4J: Defaulting to no-operation (NOP) logger implementation
SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details.
Kerberos Connected
Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/hive/service/rpc/thrift/TCLIService$Iface
at org.apache.hive.jdbc.HiveDriver.connect(HiveDriver.java:107)
at java.sql.DriverManager.getConnection(DriverManager.java:664)
at java.sql.DriverManager.getConnection(DriverManager.java:247)
at com.dbconnections.gphive.DbManager.getHiveConnection(DbManager.java:35)
at com.recordcount.dao.GpHiveRecords.getHiveTableCount(GpHiveRecords.java:93)
at com.recordcount.entry.StartCount.main(StartCount.java:13)
Caused by: java.lang.ClassNotFoundException: org.apache.hive.service.rpc.thrift.TCLIService$Iface
at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:335)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
... 6 more
答案 0 :(得分:2)
看起来您错过了以下依赖项:
<dependency>
<groupId>org.apache.hive</groupId>
<artifactId>hive-service-rpc</artifactId>
<version>2.2.0</version>
</dependency>
此jar包含缺少的界面。
更新:
看起来你还需要:
<dependency>
<groupId>org.apache.hive</groupId>
<artifactId>hive-service</artifactId>
<version>2.2.0</version>
</dependency>
还有mybe其他的jar。