复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52import 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认证)像吃饭一样简单内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复