首页 > 编程语言 >java连接接kerberos认证下的hive

java连接接kerberos认证下的hive

时间:2022-10-09 16:47:57浏览次数:36  
标签:jdbc java kerberos hadoop hive master org servlet

1.pop.xml配置

hive与hadoop的配置版本要匹配,一般hive 2.X版本要选择hadoop2.x版本,否则会不兼容

<dependency>
            <groupId>org.apache.hadoop</groupId>
            <artifactId>hadoop-client</artifactId>
            <version>${hadoop.version}</version>
            <exclusions>
                <exclusion>
                    <artifactId>slf4j-reload4j</artifactId>
                    <groupId>org.slf4j</groupId>
                </exclusion>
                <exclusion>
                    <artifactId>javax.servlet-api</artifactId>
                    <groupId>javax.servlet</groupId>
                </exclusion>
                <exclusion>
                    <artifactId>jersey-servlet</artifactId>
                    <groupId>com.sun.jersey</groupId>
                </exclusion>
                <exclusion>
                    <artifactId>jetty-servlet</artifactId>
                    <groupId>org.eclipse.jetty</groupId>
                </exclusion>
                <exclusion>
                    <artifactId>servlet-api</artifactId>
                    <groupId>javax.servlet</groupId>
                </exclusion>
            </exclusions>
        </dependency>

        <dependency>
            <groupId>org.apache.hive</groupId>
            <artifactId>hive-jdbc</artifactId>
            <version>2.1.1</version>
            <exclusions>
                <exclusion>
                    <artifactId>slf4j-reload4j</artifactId>
                    <groupId>org.slf4j</groupId>
                </exclusion>
                <exclusion>
                    <artifactId>log4j-slf4j-impl</artifactId>
                    <groupId>org.apache.logging.log4j</groupId>
                </exclusion>
                <exclusion>
                    <groupId>javax.servlet</groupId>
                    <artifactId>servlet-api</artifactId>
                </exclusion>
                <exclusion>
                    <artifactId>javax.servlet</artifactId>
                    <groupId>org.eclipse.jetty.orbit</groupId>
                </exclusion>
                <exclusion>
                    <groupId>org.eclipse.jetty.aggregate</groupId>
                    <artifactId>*</artifactId>
                </exclusion>
                <exclusion>
                    <artifactId>guice-servlet</artifactId>
                    <groupId>com.google.inject.extensions</groupId>
                </exclusion>
                <exclusion>
                    <artifactId>servlet-api-2.5</artifactId>
                    <groupId>org.mortbay.jetty</groupId>
                </exclusion>
            </exclusions>
        </dependency>

        <dependency>
            <groupId>org.apache.hadoop</groupId>
            <artifactId>hadoop-common</artifactId>
            <version>2.8.5</version>
            <exclusions>
                <exclusion>
                    <artifactId>slf4j-reload4j</artifactId>
                    <groupId>org.slf4j</groupId>
                </exclusion>
                <exclusion>
                    <artifactId>javax.servlet-api</artifactId>
                    <groupId>javax.servlet</groupId>
                </exclusion>
                <exclusion>
                    <artifactId>jersey-servlet</artifactId>
                    <groupId>com.sun.jersey</groupId>
                </exclusion>
                <exclusion>
                    <artifactId>jetty-servlet</artifactId>
                    <groupId>org.eclipse.jetty</groupId>
                </exclusion>
                <exclusion>
                    <artifactId>servlet-api</artifactId>
                    <groupId>javax.servlet</groupId>
                </exclusion>
            </exclusions>
        </dependency>

2、示例代码

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.security.UserGroupInformation;
 
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
 
public class App {
    private static String JDBC_DRIVER = "org.apache.hive.jdbc.HiveDriver";
    private static String CONNECTION_URL ="jdbc:hive2://10.23.13.196:10000/ods;principal=hive/[email protected]";
 
    static {
        try {
            Class.forName(JDBC_DRIVER);
 
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
    }
 
    public static void main(String[] args) throws Exception  {
        Class.forName(JDBC_DRIVER);
 
        //登录Kerberos账号
        System.setProperty("java.security.krb5.conf", "/root/sample-cdh/ticket/krb5.conf");
 
        Configuration configuration = new Configuration();
        configuration.set("hadoop.security.authentication" , "Kerberos" );
        configuration.setBoolean("hadoop.security.authorization", true);
        UserGroupInformation. setConfiguration(configuration);
        UserGroupInformation.loginUserFromKeytab("hive/[email protected]" , "/root/sample-cdh/ticket/hive.keytab");
 
        Connection connection = null;
        ResultSet rs = null;
        PreparedStatement ps = null;
        try {
            connection = DriverManager.getConnection(CONNECTION_URL);
            ps = connection.prepareStatement("show databases");
            rs = ps.executeQuery();
            while (rs.next()) {
                System.out.println(rs.getString(1));
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        
    }
}

 

3、注意事项:

(1)krb5.conf、test_user.keytab 2个文件,路径要改对

(2)jar依赖包版本和服务器hadoop、hive版本要匹配,否则会报错:

    Caused by: org.apache.thrift.TApplicationException: Required field 'client_protocol' is unset! Struct:TOpenSessionReq

    原因:This indicates a version mismatch between client and server, namely that the client is newer than the server, which is your case.

    客户端和服务端包的版本不一致;

(3)错误:Error: Could not open client transport with JDBC Uri: jdbc:hive2://master:10000/default: Peer indicated failure:

    Unsupported mechanism type PLAIN (state=08S01,code=0)

    解决: jdbc连接url时,缺少principal,整个url连接如下:jdbc:hive2://master:10000/default;principal=hive/[email protected]


(4)错误: Could not open client transport with JDBC Uri: jdbc:hive2://master:10000/default;principal=hdfs/[email protected]: Peer indicated failure:

    GSS initiate failed (state=08S01,code=0)
  解决: jdbc连接url时,principal的用户有问题,整个url连接如下:
    principal=hdfs/[email protected] 替换为:hive/[email protected]
    jdbc:hive2://master:10000/default;principal=hive/[email protected]

 

标签:jdbc,java,kerberos,hadoop,hive,master,org,servlet
From: https://www.cnblogs.com/kevinlucky/p/16772661.html

相关文章

  • 18_Java中的内部类
    Java的内部类一、内部类概述1、内部类:其实就是在一个类中定义一个类(类似于C语言中的结构体的嵌套)2、定义格式:publicclass类名{修饰符class类名{......
  • Java加解密-SM4国密算法
    SM4国密算法简介SM4依赖包SM4类SM4_Context类SecuritySM4类=================================== SM4国密算法简介与DES和AES算法相似,国密SM4算法是一种分组加密......
  • Java 多线程(三)静态代理模式
    静态代理模式:1.真实角色和代理角色实现同一个接口2.代理角色要代理真实角色3.代理角色可以做真实角色做不了的事4.真实角色专注做自己的事publicclassStaticProxy......
  • Java开发页面接口过程中,应该注意的点
    列表接口1、查询条件是否需要去除前后空格;2、列表的时间格式是否正确;有的显示2022-07-28这种格式,有的显示:2022-07-2719:00:003、在前端传递时间的参数时,若定义的是Da......
  • Java语言中多态的机制
    1.方法解析Class文件的编译过程中,不包含传统编译中的连接步骤,一切方法的调用在Class文件中存储的都只是符号引用,而不是方法在实际运行时内存布局中的入口地址。这个特性......
  • Kerberos 认证错误 :Can't get Kerberos realm
    原始代码为:org.apache.hadoop.security.UserGroupInformation.setConfiguration(conf)出现错误:Can'tgetKerberosrealm 原因:程序读取不到krb5.conf 解决办法......
  • 【Java复健指南03】递归思想
    【递归】递归重要规则1.执行一个方法时,就创建一个新的受保护的独立空间(栈空间)方法的局部变量是独立的,不会相互影响,比如n变量如果方法中使用的是引用类型变量(比......
  • java序列化
    一、序列化与反序列化序列化:指堆内存中的java对象数据,通过某种方式把对存储到磁盘文件中,或者传递给其他网络节点(网络传输)。这个过程称为序列化,通常是指将数据结构或对象转......
  • kerberos开启后服务报错Unable to obtain password from user
    异常日志022-10-0910:26:16,230ERRORorg.apache.hadoop.hdfs.qjournal.server.JournalNode:Failedtostartjournalnode.org.apache.hadoop.security.KerberosAuthExce......
  • 003Java的诞生
    003Java的诞生1、计算机语言发展史(1)第一代语言机器语言我们都知道计算机的基本计算方式都是基于二进制的方式。二进制:010111001010110010110100这种代码是直接输......