java 通過jdbc 連接hive2 使用kerberos 認證


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>    

  


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM