zoukankan      html  css  js  c++  java
  • 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>    
    

      

  • 相关阅读:
    linux中压缩、解压缩命令
    linux中的sed指令
    linux中shell编程(一)
    linux中的正则表达式
    linux中的管道和重定向
    linux中用户、组和权限相关指令
    linux中bash常见的指令
    linux文本操作相关指令
    java.lang.OutOfMemoryError 解决程序启动内存溢出问题
    Java常用排序算法/程序员必须掌握的8大排序算法
  • 原文地址:https://www.cnblogs.com/BigWrite/p/13530991.html
Copyright © 2011-2022 走看看