jdbc连接hive并认证kerberos

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.security.UserGroupInformation;

import java.io.File;
import java.io.IOException;
import java.sql.*;
import java.util.Locale;
import java.util.ResourceBundle;

public class DBUtils1 {

    /**
     * 用于连接Hive所需的一些参数设置 driverName:用于连接hive的JDBC驱动名 When connecting to
     * HiveServer2 with Kerberos authentication, the URL format is:
     * jdbc:hive2://<host>:<port>/<db>;principal=
     * <Server_Principal_of_HiveServer2>
     */
    private static String KRB5_CONF = "krb5.ini";
    private static String KRB5_CONF1 = "krb5.conf";
    private static String PRINCIPAL = "用户";
    private static String KEYTAB = "krb5.keytab";
    private static String HIVE_DRIVER = "org.apache.hive.jdbc.HiveDriver";
    // 注意:这里的principal是固定不变的,其指的hive服务所对应的principal,而不是用户所对应的principal
    private static String URL = "jdbc:hive2://host:2100/test;principal=hive/_HOST@xx.com";
    private static String sql = "";
    private static ResultSet res;
    private static String path = Thread.currentThread().getContextClassLoader().getResource("").getPath();

    public static Connection get_conn() {
        System.out.println(path+KRB5_CONF);
        /** 使用Hadoop安全登录 **/
        Configuration conf = new Configuration();
        conf.set("hadoop.security.authentication", "Kerberos");
        if (System.getProperty("os.name").toLowerCase().startsWith("win")) {
            // 默认:这里不设置的话,win默认会到 C盘下读取krb5.init
            System.setProperty("java.security.krb5.conf", path+KRB5_CONF1);
        } // linux 会默认到 /etc/krb5.conf 中读取krb5.conf,本文笔者已将该文件放到/etc/目录下,因而这里便不用再设置了
        try {
            UserGroupInformation.setConfiguration(conf);
            UserGroupInformation.loginUserFromKeytab(PRINCIPAL, path+KEYTAB);
            UserGroupInformation logUser = UserGroupInformation.getLoginUser();

            if (null == logUser) {
                throw new RuntimeException("登录用户为空!");
            }
        } catch (IOException e1) {
            e1.printStackTrace();
            throw new RuntimeException("Kerberos 认证失败");
        }
        try {
            //注册驱动
            Class.forName(HIVE_DRIVER);
            // 连接数据库
            return DriverManager.getConnection(URL);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
    // 连接
    public static Connection getConnection(){
        try {
            //注册驱动
            Class.forName(HIVE_DRIVER);
            // 连接数据库
            return DriverManager.getConnection(URL);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    public static void close(Connection conn,PreparedStatement ps,ResultSet rs){
        try {
            if (rs != null){
                rs.close();
                rs = null;
            }
            if (ps != null){
                ps.close();
                ps=null;
            }
            if (conn != null){
                conn.close();
                conn=null;
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    public static void main(String[] args) throws SQLException {
        Connection connection = get_conn();
        PreparedStatement ps = connection.prepareStatement("select * from test_lsq limit 10");
        ResultSet rs = ps.executeQuery();
        while (rs.next()) {
            System.out.println(rs.getObject(0));
            System.out.println("连接成功1111");
        }
        System.out.println("连接成功");
        close(connection,ps,rs);
    }
    
    public static String getPath(String fileName){
        String path = Thread.currentThread().getContextClassLoader().getResource(fileName).getPath();
        return path;
    }
}
 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值