pom文件
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.hainiu</groupId>
<artifactId>ImpalaJDBC</artifactId>
<version>1.0</version>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.3</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>org.apache.hadoop</groupId>
<artifactId>hadoop-common</artifactId>
<version>3.0.0</version>
</dependency>
<dependency>
<groupId>org.apache.hadoop</groupId>
<artifactId>hadoop-client</artifactId>
<version>3.0.0</version>
</dependency>
<dependency>
<groupId>org.apache.hbase</groupId>
<artifactId>hbase-client</artifactId>
<version>2.1.0</version>
</dependency>
<dependency>
<groupId>org.apache.hbase</groupId>
<artifactId>hbase-server</artifactId>
<version>2.1.0</version>
</dependency>
<dependency>
<groupId>org.apache.phoenix</groupId>
<artifactId>phoenix-core</artifactId>
<version>5.0.0-HBase-2.0</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
</dependencies>
</project>
kerberos工具类
package Utils;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.security.UserGroupInformation;
import java.io.IOException;
/**
* @author : xiniu
* @date : 2021/5/29
* @describe :kerberos认证工具类
*/
public class KerberosAuthenticator {
/**
* kerberos认证
* @param configuration hadoop配置文件
* @param krb5Path kerberos配置文件(krb5.conf)路径
* @param principal 认证主体名
* @param keytabPath keytab文件路径
*/
public void kerberosAuth(Configuration configuration, String krb5Path, String principal, String keytabPath){
System.setProperty("java.security.krb5.conf",krb5Path);
configuration.set("hadoop.security.authentication","Kerberos");
UserGroupInformation.setConfiguration(configuration);
try {
UserGroupInformation.loginUserFromKeytab(principal,keytabPath);
} catch (IOException e) {
e.printStackTrace();
}
}
}
配置文件
准备krb5配置文件、keytab配置文件、hadoop配置文件如下
操作代码
import Utils.KerberosAuthenticator;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.security.UserGroupInformation;
import org.apache.log4j.Logger;
import java.security.PrivilegedAction;
import java.sql.*;
/**
* @author : xiniu
* @date : 2021/5/29
* @describe :kerberos环境下通过jdbc操作impala
*/
public class ImpalaJDBC {
public static void main(String[] args) {
Logger logger = Logger.getLogger(ImpalaJDBC.class);
//kerberos conf
Configuration configuration = new Configuration();
String krb5path = "src/main/conf/krb5.conf";
String keytabpath = "src/main/conf/impala.keytab";
String principal = "impala@HAINIU.COM";
//JDBC
String JDBC_DRIVER = "com.cloudera.impala.jdbc41.Driver";
String JDBC_URL = "jdbc:impala://worker-1:21050/default;AuthMech=1;KrbHostFQDN=worker-1;KrbServiceName=impala";
//kerberos authentication
KerberosAuthenticator kerberosAuthenticator = new KerberosAuthenticator();
kerberosAuthenticator.kerberosAuth(configuration,krb5path,principal,keytabpath);
//connect to impala by kerberos authentication
try {
UserGroupInformation loginUser = UserGroupInformation.getLoginUser();
loginUser.doAs(new PrivilegedAction<Object>() {
@Override
public Object run() {
Connection connection = null;
ResultSet rs = null;
PreparedStatement ps = null;
try {
Class.forName(JDBC_DRIVER);
connection = DriverManager.getConnection(JDBC_URL);
ps = connection.prepareStatement("select count(1) from xinniu.hainiu1");
rs = ps.executeQuery();
while (rs.next()){
int anInt = rs.getInt(1);
System.out.println("anInt = " + anInt);
}
} catch (Exception e) {
logger.error(e);
}finally {
if (ps != null){
try {
ps.close();
} catch (Exception e) {
logger.error(e);
}
}
if (connection != null){
try {
connection.close();
} catch (Exception e) {
logger.error(e);
}
}
}
return null;
}
});
} catch (Exception e) {
e.printStackTrace();
}
}
}