Java通过Kerberos认证连接hiveServer2

import jodd.util.PropertiesUtil;
import org.apache.hadoop.security.UserGroupInformation;

import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Properties;

/**
* hive工具类
*/
public class hiveUtil {
    private static Properties props=null;
    
    static {
        props=new Properties();
        InputStream in = PropertiesUtil.class.getClassLoader().getResourceAsStream("cdhConfig.properties");
        try {
            props.load(in);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
//    private static String hiveJDBCurl = "jdbc:hive2://**:10000/;principal=hive/**@DWSP.COM";// hive库地址+库名

// 认证kerberos
    private static void authKrb5() throws IOException {
        // 设置jvm启动时krb5的读取路径参数
        System.setProperty("java.security.krb5.conf", (String) props.get("java.security.krb5.conf"));
        // 配置kerberos认证
        org.apache.hadoop.conf.Configuration conf = new org.apache.hadoop.conf.Configuration();
//        conf.setBoolean("hadoop.security.authorization", true);
        conf.set("hadoop.security.authentication", (String) props.get("hadoop.security.authentication"));
        //配置将要登陆kerberos的用户,和其对应的keytab的所在路径
        UserGroupInformation.setConfiguration(conf);
        try {
            UserGroupInformation.loginUserFromKeytab(
                    (String) props.get("kerberos.principal"),
                    (String) props.get("kerberos.keytab"));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static Connection getConn() throws ClassNotFoundException,
            SQLException, IOException {
        //认证kerberos
        authKrb5();
        Class.forName("org.apache.hive.jdbc.HiveDriver");
        Connection conn = DriverManager.getConnection((String) props.get("hive.JDBC.url"));
        return conn;
    }
}


你可能感兴趣的:(HIVE,大数据)