样例代码:
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