本文提供一个通过Java连接安全集群的Impala服务的示例。
package com.example.jdbc;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.security.UserGroupInformation;
import java.io.IOException;
import java.security.PrivilegedAction;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
public class ImpalaSecureConn {
private static String JDBC_DRIVER = "com.cloudera.impala.jdbc41.Driver";
private static String CONNECTION_URL = "jdbc:impala://worker01.bigdata.zxxk.com:21050;AuthMech=1;KrbRealm=BIGDATA.ZXXK.COM;KrbHostFQDN=worker01.bigdata.zxxk.com;KrbServiceName=impala";
public static void main(String[] args) {
try {
//登录Kerberos账号
System.setProperty("java.security.krb5.conf", "/home/xwd/ws/kerberos/dev/krb5.conf");
Configuration configuration = new Configuration();
configuration.set("hadoop.security.authentication", "Kerberos");
UserGroupInformation.setConfiguration(configuration);
UserGroupInformation.loginUserFromKeytab("xingweidong@BIGDATA.ZXXK.COM", "/home/xwd/ws/kerberos/dev/xingweidong.keytab");
System.out.println(UserGroupInformation.getCurrentUser() + "------" + UserGroupInformation.getLoginUser());
UserGroupInformation loginUser = UserGroupInformation.getLoginUser();
loginUser.doAs(new PrivilegedAction<Object>() {
public Object run() {
Connection connection = null;
ResultSet rs = null;
PreparedStatement ps = null;
try {
Class.forName(JDBC_DRIVER);
connection = DriverManager.getConnection(CONNECTION_URL);
ps = connection.prepareStatement("SELECT * FROM default.201402_trip_data LIMIT 100");
rs = ps.executeQuery();
while (rs.next()) {
System.out.println(rs.getString(1));
}
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
});
} catch (IOException e) {
e.printStackTrace();
}
}
}