JDBC+Kerberos认证连接impala

样例代码:

public class getTradingDay {
    public static Connection getConnection() throws IOException {
        String confPath = System.getProperty("user.dir");
        System.out.println(confPath);
        Properties properties = new Properties();
        InputStreamReader inputStreamReader = new InputStreamReader(new FileInputStream(confPath + "/connect.properties"));
        properties.load(inputStreamReader);
        String driverName = properties.getProperty("driverName");
        String connection_url = properties.getProperty("connection_url");
        String KEYTAB_PATH = properties.getProperty("keytab_path");
        String KETTAB_USER = properties.getProperty("keytab_user");
        String KRB5_CONF = properties.getProperty("krb5_conf");
        System.out.println(KRB5_CONF);
        try {
            Class.forName(driverName);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
        Connection connection = null;
        Configuration conf = new Configuration();
        System.setProperty("java.security.krb5.conf", KRB5_CONF);
        System.setProperty("security_krb5_conf", KRB5_CONF);
        conf.set("hadoop.security.authentication", "Kerberos");
        UserGroupInformation.setConfiguration(conf);
        UserGroupInformation dwetl = UserGroupInformation.loginUserFromKeytabAndReturnUGI(KETTAB_USER, KEYTAB_PATH);
        UserGroupInformation.getLoginUser();

        connection = (Connection) dwetl.doAs((PrivilegedAction<Object>) () -> {
            Connection impala_conn = null;
            try {
                impala_conn = DriverManager.getConnection(connection_url);
            } catch (SQLException e) {
                e.printStackTrace();
                System.exit(1);
            }
            return impala_conn;
        });
        return connection;
    }


    public static void main(String[] args) throws SQLException, IOException {

        String str = args[0];
        String[] chars = str.split("");
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMdd");
        Date date = new Date();
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.DAY_OF_YEAR,-1);
        Date time = calendar.getTime();
        String t_dt = simpleDateFormat.format(time);
        String t_date = simpleDateFormat.format(date);

        String sql = "select distinct dt from cde_data.cde_t99_trade_caln_para_q where trim(trade_day_type_cd)='1' and biz_date = ";
        //获取连接
        Connection connection = getTradingDay.getConnection();
        Statement statement = connection.createStatement();
        ResultSet resultSet = statement.executeQuery(sql + t_dt);
        System.out.println(sql + t_dt);

        ArrayList<String> arrayList = new ArrayList();
        while (resultSet.next()) {
            arrayList.add(resultSet.getString(1).replace("-", ""));
        }
        // 对获取到的交易日期进行排序
        Collections.sort(arrayList);
        statement.close();
        connection.close();
        // 获取当前日期在list中的下下标
        int i = arrayList.indexOf(t_date);
        System.out.println(i);
        String tarDt;
        if (chars[1].equals("+")) {
            tarDt = arrayList.get(i + Integer.parseInt(chars[2]));
        } else if (chars[1].equals("-")) {
            tarDt = arrayList.get(i - Integer.parseInt(chars[2]));
        } else {
            tarDt = arrayList.get(i);
        }
        System.out.println(tarDt);
    }

}

配置文件

driverName=com.cloudera.impala.jdbc4.Driver
connection_url=jdbc:hive2://192.168.24.96:21050/default;principal=impala/node96.dsjdev.com@DSJDEV.COM
keytab_path=/home/swhy_user/rjx/dwetl.keytab
keytab_user=dwetl@DSJDEV.COM
krb5_conf=/home/swhy_user/rjx/krb5.conf
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值