其他分享
首页 > 其他分享> > kerberos 激活JDBC连接hive

kerberos 激活JDBC连接hive

作者:互联网

代码片段: 

public Connection getConnection() throws IOException, SQLException, ClassNotFoundException {
    Class.forName("org.apache.hive.jdbc.HiveDriver");

    Properties p = new Properties();
    InputStream in = HiveUtil.class.getResourceAsStream("/config/hive.properties");
    p.load(in);
    Connection conn=null ;
    //检查kerberos开关是否打开
    if("on".equals(p.getProperty("kerberos.authen.function"))){
        String hiveUrl = p.getProperty("hiveUrl");
        org.apache.hadoop.conf.Configuration conf = new org.apache.hadoop.conf.Configuration();
        //下面2行在本地测试需要打开,生产上已经配置到环境变量里,需要注释
     // System.setProperty("java.security.auth.login.config", p.getProperty("jassPath"));
     //  System.setProperty("java.security.krb5.conf",  p.getProperty("kbsConfig"));
        conf.set("hadoop.security.authentication", "Kerberos");
        UserGroupInformation.setConfiguration(conf);
        UserGroupInformation.loginUserFromKeytab(p.getProperty("kbsUser"), p.getProperty("kbsKeyTab"));
        conn = DriverManager.getConnection(hiveUrl);
    }else{
    String hiveUrl = p.getProperty("hiveUrl");
    String username=p.getProperty("username");
    String password=p.getProperty("password");
    if("".equals(username.trim())) {
        conn = DriverManager.getConnection(hiveUrl);
    }else{
        conn = DriverManager.getConnection(hiveUrl, username, password);
    }

    }


    in.close();
    return conn;
}

配置文件:

username=XXX
password=
kerberos.authen.function=on
hiveUrl=jdbc:hive2://XXX.com:10001/default;principal=hive/XXX@XX.COM
jassPath=./zk-jaas.conf
kbsConfig=./krb5.conf
kbsUser=hive/XXX@XX.COM
kbsKeyTab=./hive.keytab

标签:username,JDBC,getProperty,kerberos,hive,hiveUrl,conf,conn
来源: https://blog.csdn.net/weixin_42223833/article/details/121475456