概述
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.security.UserGroupInformation;
import java.io.File;
import java.io.IOException;
import java.sql.*;
import java.util.Locale;
import java.util.ResourceBundle;
public class DBUtils1 {
/**
* 用于连接Hive所需的一些参数设置 driverName:用于连接hive的JDBC驱动名 When connecting to
* HiveServer2 with Kerberos authentication, the URL format is:
* jdbc:hive2://<host>:<port>/<db>;principal=
* <Server_Principal_of_HiveServer2>
*/
private static String KRB5_CONF = "krb5.ini";
private static String KRB5_CONF1 = "krb5.conf";
private static String PRINCIPAL = "用户";
private static String KEYTAB = "krb5.keytab";
private static String HIVE_DRIVER = "org.apache.hive.jdbc.HiveDriver";
// 注意:这里的principal是固定不变的,其指的hive服务所对应的principal,而不是用户所对应的principal
private static String URL = "jdbc:hive2://host:2100/test;principal=hive/_HOST@xx.com";
private static String sql = "";
private static ResultSet res;
private static String path = Thread.currentThread().getContextClassLoader().getResource("").getPath();
public static Connection get_conn() {
System.out.println(path+KRB5_CONF);
/** 使用Hadoop安全登录 **/
Configuration conf = new Configuration();
conf.set("hadoop.security.authentication", "Kerberos");
if (System.getProperty("os.name").toLowerCase().startsWith("win")) {
// 默认:这里不设置的话,win默认会到 C盘下读取krb5.init
System.setProperty("java.security.krb5.conf", path+KRB5_CONF1);
} // linux 会默认到 /etc/krb5.conf 中读取krb5.conf,本文笔者已将该文件放到/etc/目录下,因而这里便不用再设置了
try {
UserGroupInformation.setConfiguration(conf);
UserGroupInformation.loginUserFromKeytab(PRINCIPAL, path+KEYTAB);
UserGroupInformation logUser = UserGroupInformation.getLoginUser();
if (null == logUser) {
throw new RuntimeException("登录用户为空!");
}
} catch (IOException e1) {
e1.printStackTrace();
throw new RuntimeException("Kerberos 认证失败");
}
try {
//注册驱动
Class.forName(HIVE_DRIVER);
// 连接数据库
return DriverManager.getConnection(URL);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
// 连接
public static Connection getConnection(){
try {
//注册驱动
Class.forName(HIVE_DRIVER);
// 连接数据库
return DriverManager.getConnection(URL);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
public static void close(Connection conn,PreparedStatement ps,ResultSet rs){
try {
if (rs != null){
rs.close();
rs = null;
}
if (ps != null){
ps.close();
ps=null;
}
if (conn != null){
conn.close();
conn=null;
}
} catch (Exception e) {
throw new RuntimeException(e);
}
}
public static void main(String[] args) throws SQLException {
Connection connection = get_conn();
PreparedStatement ps = connection.prepareStatement("select * from test_lsq limit 10");
ResultSet rs = ps.executeQuery();
while (rs.next()) {
System.out.println(rs.getObject(0));
System.out.println("连接成功1111");
}
System.out.println("连接成功");
close(connection,ps,rs);
}
public static String getPath(String fileName){
String path = Thread.currentThread().getContextClassLoader().getResource(fileName).getPath();
return path;
}
}
最后
以上就是包容芹菜为你收集整理的jdbc连接hive并认证kerberos的全部内容,希望文章能够帮你解决jdbc连接hive并认证kerberos所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
发表评论 取消回复