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
上一篇:Matlab连接MySQL


下一篇:python脚本打包成exe可执行文件