用jdbc连接impala(kerberos认证)

上代码

import java.sql.Connection;

import java.sql.DriverManager;
import java.sql.ResultSet;


import java.sql.SQLException;
import java.sql.Statement;


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


public class TestImpala {


public static void main(String[] args) {

Configuration conf = new Configuration();
conf.set("hadoop.security.authentication","kerberos");

Connection conn = null;
Statement stmt = null;

System.setProperty("java.security.krb5.conf","D:/krb5.conf");
System.setProperty("java.security.auth.login.config","c:/windows/krb5.ini");

try {
Class.forName("com.cloudera.impala.jdbc41.Driver");

UserGroupInformation.setConfiguration(conf);

//UserGroupInformation.loginUserFromKeytab("sys_dm05", "/home/sys_dm05/keytab/sys_dm05.keytab");
//UserGroupInformation.loginUserFromKeytab("sys_dm05", "E:/work/sys_dm05.keytab");


Class.forName("com.cloudera.impala.jdbc41.Driver");
conn = DriverManager.getConnection("jdbc:impala://10.203.50.100:21050/default;AuthMech=1;KrbHostFQDN=cdhvip.saic-gm.com;KrbRealm=SAIC-GM.COM;KrbServiceName=impala");

stmt = conn.createStatement();
stmt.execute("refresh dm_dm05.TF_ASC_DURATION");
String sql = "select count(*) from dm_dm05.TF_ASC_DURATION";

ResultSet rs = stmt.executeQuery(sql);
if (rs.next()) System.out.print("============"+rs.getInt(1));
// ResultSetMetaData metadata = rs.getMetaData();
// while (rs.next()) {
// int columnCount = metadata.getColumnCount();
// for (int m = 1; m <= columnCount; m++) {
// System.out.print(metadata.getColumnName(m) + ":" + rs.getObject(m));
// System.out.print(" ");
// }
// System.out.println("");
// }
rs.close();
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
} catch(Exception e) {
e.printStackTrace();
}finally {
if (stmt != null) {
try {
stmt.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if (conn != null) {
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}

}


c:\windows\krb5.ini


Client {
com.sun.security.auth.module.Krb5LoginModule required
useKeyTab=true
keyTab="E:/work/sys_dm05.keytab"
principal="[email protected]"
doNotPrompt=true;
};

你可能感兴趣的:(java,impala)