我是靠谱客的博主 重要老师,最近开发中收集的这篇文章主要介绍jdbc连接hive简单代码(带kerberos认证)像吃饭一样简单,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.security.UserGroupInformation;
import org.apache.thrift.TException;
import java.sql.*;
public class Test {
public static void main(String[] args) throws Exception {
// 解决windows中执行可能出现读不到krb5配置文件的问题
//
System.setProperty("java.security.krb5.conf", "krb5.conf");
// 解决Linux中执行可能出现读不到krb5配置文件的问题
System.setProperty("java.security.krb5.conf", "/etc/krb5.conf");
//详细报错信息
System.setProperty("sun.security.krb5.debug", "true");
// 解决windows中执行可能出现找不到HADOOP_HOME或hadoop.home.dir问题
//
System.setProperty("hadoop.home.dir", "C:\Program Files\winutils-master\hadoop-2.8.3");
// Kerberos认证
Configuration configuration = new Configuration();
configuration.set("hadoop.security.authentication", "Kerberos");
configuration.set("keytab.file" , "/etc/hive/conf/hive.keytab" );
configuration.set("kerberos.principal" , "hive/XXX@EXAMPLE.COM" );
UserGroupInformation.setConfiguration(configuration);
UserGroupInformation.loginUserFromKeytab("hive/XXX@EXAMPLE.COM", "/etc/hive/conf/hive.keytab");
// 创建hive连接
Connection connection = null;
ResultSet rs = null;
PreparedStatement ps = null;
try {
Class.forName("org.apache.hive.jdbc.HiveDriver");
connection = DriverManager.getConnection("jdbc:hive2://192.168.xxx.xxx:10000/数据库名;principal=hive/xxx@EXAMPLE.COM","用户名","密码");
if (null != connection) {
ps = connection.prepareStatement("show tables");
rs = ps.executeQuery();
while (rs.next()) {
System.out.println(rs.getString(1));
}
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if (rs != null) {
rs.close();
}
if (ps != null) {
ps.close();
}
if (connection != null) {
connection.close();
}
}
}
}

 

最后

以上就是重要老师为你收集整理的jdbc连接hive简单代码(带kerberos认证)像吃饭一样简单的全部内容,希望文章能够帮你解决jdbc连接hive简单代码(带kerberos认证)像吃饭一样简单所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(65)

评论列表共有 0 条评论

立即
投稿
返回
顶部