hadoop version:3.1
private static Connection initConn(String hive_driverName, String krb5FilePath, String krb5KeyTabPath, String krbPrincipal, String userName, String url) throws SQLException, IOException, ClassNotFoundException { System.setProperty("java.security.krb5.conf", krb5FilePath); System.setProperty("sun.security.krb5.debug", "true"); // 解決windows中執行可能出現找不到HADOOP_HOME或hadoop.home.dir問題 // Kerberos認證 org.apache.hadoop.conf.Configuration configuration = new org.apache.hadoop.conf.Configuration(); configuration.set("hadoop.security.authentication", "Kerberos"); configuration.set("keytab.file", krb5KeyTabPath); configuration.set("kerberos.principal", krbPrincipal); UserGroupInformation.setConfiguration(configuration); UserGroupInformation.loginUserFromKeytab(userName, krb5KeyTabPath); // 創建hive連接 Class.forName(hive_driverName); Connection connection = DriverManager.getConnection(url); if (connection == null) { throw new NullPointerException("獲取connection失敗"); } return connection; }
一般報 Can't get Kerberos realm 錯誤是因為,找不到 krb5.conf文件路徑
pom依賴
<!--hive連接--> <dependency> <groupId>org.apache.hadoop</groupId> <artifactId>hadoop-common</artifactId> <version>3.2.0</version> <exclusions> <exclusion> <groupId>jdk.tools</groupId> <artifactId>jdk.tools</artifactId> </exclusion> </exclusions> </dependency> <dependency> <groupId>org.apache.hive</groupId> <artifactId>hive-jdbc</artifactId> <version>3.1.2</version> <exclusions> <exclusion> <groupId>org.eclipse.jetty</groupId> <artifactId>*</artifactId> </exclusion> </exclusions> </dependency>
阿里雲鏡像
<repository> <id>aliyun</id> <name>aliyun</name> <url>http://maven.aliyun.com/nexus/content/groups/public/</url> </repository>