一、Maven依赖
<dependency>
<groupId>org.apache.hive</groupId>
<artifactId>hive-jdbc</artifactId>
<version>2.3.9</version>
</dependency>
二、相关配置信息
驱动类:org.apache.hive.jdbc.HiveDriver
连接URL:jdbc:hive2://xxx:2181,xxx:2181/;principal=hadoop/xxx@EMR-GMxxx;serviceDiscoveryMode=zooKeeper;zooKeeperNamespace=kyuubi;?spark.app.name=test-kyuubi;kyuubi.engine.share.level=USER;spark.submit.deployMode=cluster;kyuubi.session.engine.check.interval=PT5M;kyuubi.session.engine.idle.timeout=PT30M;
【注意】:
principal 配置的信息为账号鉴权信息
zooKeeperNamespace 需要指定为 kyuubi
?后的参数为kyuubi配置参数
三、 Kerberos 身份验证
1、需要准备两个文件,可以从集群kerberos配置里拷贝
krb5 /app/data/config/kerberos/krb5.conf
krb5Keytab /app/data/config/kerberos/emr.keytab
2、获取账号
krb5hadoop hadoop/xxxx@EMR-QX9xxxx
3、加载代码
/**
* 加载hive krb 配置
*/
public void loadingHiveKrbConfig() {
try {
log.info("加载hive krb 配置");
System.setProperty("java.security.krb5.conf", krb5);
Configuration configuration = new Configuration();
configuration.setBoolean("hadoop.security.authorization", true);
configuration.set("hadoop.security.authentication", "Kerberos");
UserGroupInformation.setConfiguration(configuration);
if (UserGroupInformation.isLoginKeytabBased()) {
log.info("UserGroupInformation.isLoginKeytabBased() is ture");
UserGroupInformation.getLoginUser().reloginFromKeytab();
} else {
log.info("UserGroupInformation.isLoginKeytabBased() is false");
UserGroupInformation.loginUserFromKeytab(krb5hadoop, krb5Keytab);
}
log.info("ticketCache=====>" + UserGroupInformation.isLoginTicketBased());
} catch (Exception e) {
log.error("加载hive krb 配置 失败:", e);
e.printStackTrace();
}
}
四、连接代码
Connection connection = null;
HiveStatement statement = null;
HiveQueryResultSet resultSet = null;
try {
loadingHiveKrbConfig();
Class.forName("org.apache.hive.jdbc.HiveDriver"));
connection = DriverManager.getConnection(url);
statement = (HiveStatement) connection.createStatement();
resultSet = (HiveQueryResultSet) statement.executeQuery(sql);
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (null != resultSet) {
resultSet.close();
}
if (null != statement) {
statement.close();
}
if (null != connection) {
connection.close();
}
} catch (final Exception e) {
e.printStackTrace();
}
}