cdh kerberosed hive jdbc demo

cdh启用kerberos后,通过jdbc连接hive需要额外的连接配置,具体代码如下:

System.setProperty("java.security.krb5.conf", "/Users/zhouxiang/develop/test/krb5.conf");
Class.forName("org.apache.hive.jdbc.HiveDriver").newInstance();
Configuration conf = new Configuration();
conf.set("hadoop.security.authentication", "kerberos");
UserGroupInformation.setConfiguration(conf);
String principal = System.getProperty("kerberosPrincipal", "hive@TEST.COM"); String keytabLocation = System.getProperty("kerberosKeytab", "/Users/zhouxiang/develop/test/hive.keytab"); UserGroupInformation.loginUserFromKeytab(principal, keytabLocation); String url = "jdbc:hive2://10.127.60.4:10000/default;auth=KERBEROS;principal=hive/bdp04@TEST.COM;"; Connection connection = DriverManager.getConnection(url, "", ""); Assert.assertNotNull(connection); Statement statement = connection.createStatement(); ResultSet resultSet = statement.executeQuery("select count(*) from ods.test"); while(resultSet.next()) { System.out.println(resultSet.getInt(1)); }

发表评论

电子邮件地址不会被公开。 必填项已用*标注