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>    

  

原文地址:https://www.cnblogs.com/BigWrite/p/13530991.html