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

相关文章

连接数据库的方式:第一种方式:ODBC:开放数据库连接是微软...
JDBCRequest 使用VariableNamesmysql:数据库连接池对象var...
 1.JDBCDBC(JavaDataBaseConnectivity):Java数据库连接技术...
1.需要jar包的支持:java.sqljavax.sqlmysql-conneter-java....
1.简介Activiti是一个业务流程管理(BPM)框架,它是覆盖了业务...
1.JDBC体系系统一组规范:接口JDBC接口(API)包括两个层次:...