今天在移動的云平臺上通過jdbc連接hive,發現云平臺使用了 kerberos的認證。與寧波實驗環境不同。
發現一文解決了問題,轉載如下:
原文地址:http://blog.csdn.net/zengmingen/article/details/78605086
------------------------------
- 運用 Ambari 搭建的HDP 集群,由于開啟了kerberos ,對外提供Hive數據時統一用JDBC 的方式,所以寫了下面這么一個簡單樣例供第三方數據接入參考。
代碼如下所示:
package com.bmsoft.hive.impl;import org.apache.hadoop.security.UserGroupInformation;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;/*** 簡單的jdbc連接hive實例(已開啟kerberos服務)*/public class HiveSimple2 {/*** 用于連接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 driverName = "org.apache.hive.jdbc.HiveDriver";private static String url = "jdbc:hive2://bigdata40:10000/admin;principal=hive/bigdata40@BIGDATA.COM";private static String sql = "";private static ResultSet res;public static Connection get_conn() throws SQLException, ClassNotFoundException {/** 使用Hadoop安全登錄 **/org.apache.hadoop.conf.Configuration conf = new org.apache.hadoop.conf.Configuration();conf.set("hadoop.security.authentication", "Kerberos");if (System.getProperty("os.name").toLowerCase().startsWith("win")) {System.setProperty("java.security.krb5.conf", "C:/Windows/krbconf/bms/krb5.ini");} try {UserGroupInformation.setConfiguration(conf);UserGroupInformation.loginUserFromKeytab("test2/hdp39@BMSOFT.COM", "./conf/test2.keytab");} catch (IOException e1) {e1.printStackTrace();}Class.forName(driverName);Connection conn = DriverManager.getConnection(url);return conn;}/*** 查看數據庫下所有的表** @param statement* @return*/public static boolean show_tables(Statement statement) {sql = "SHOW TABLES";System.out.println("Running:" + sql);try {ResultSet res = statement.executeQuery(sql);System.out.println("執行“+sql+運行結果:");while (res.next()) {System.out.println(res.getString(1));}return true;} catch (SQLException e) {e.printStackTrace();}return false;}/*** 獲取表的描述信息** @param statement* @param tableName* @return*/public static boolean describ_table(Statement statement, String tableName) {sql = "DESCRIBE " + tableName;try {res = statement.executeQuery(sql);System.out.print(tableName + "描述信息:");while (res.next()) {System.out.println(res.getString(1) + "\t" + res.getString(2));}return true;} catch (SQLException e) {e.printStackTrace();}return false;}/*** 刪除表** @param statement* @param tableName* @return*/public static boolean drop_table(Statement statement, String tableName) {sql = "DROP TABLE IF EXISTS " + tableName;System.out.println("Running:" + sql);try {statement.execute(sql);System.out.println(tableName + "刪除成功");return true;} catch (SQLException e) {System.out.println(tableName + "刪除失敗");e.printStackTrace();}return false;}/*** 查看表數據** @param statement* @return*/public static boolean queryData(Statement statement, String tableName) {sql = "SELECT * FROM " + tableName + " LIMIT 20";System.out.println("Running:" + sql);try {res = statement.executeQuery(sql);System.out.println("執行“+sql+運行結果:");while (res.next()) {System.out.println(res.getString(1) + "," + res.getString(2) + "," + res.getString(3));}return true;} catch (SQLException e) {e.printStackTrace();}return false;}/*** 創建表** @param statement* @return*/public static boolean createTable(Statement statement, String tableName) {sql = "CREATE TABLE test_1m_test2 AS SELECT * FROM test_1m_test"; System.out.println("Running:" + sql);try {boolean execute = statement.execute(sql);System.out.println("執行結果 :" + execute);return true;} catch (SQLException e) {e.printStackTrace();}return false;}public static void main(String[] args) {try {Connection conn = get_conn();Statement stmt = conn.createStatement();String tableName = "test_100m";show_tables(stmt);/** 刪除表 **/createTable(stmt, tableName);conn.close();} catch (Exception e) {e.printStackTrace();} finally {System.out.println("!!!!!!END!!!!!!!!");}}
}
- 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
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
- 83
- 84
- 85
- 86
- 87
- 88
- 89
- 90
- 91
- 92
- 93
- 94
- 95
- 96
- 97
- 98
- 99
- 100
- 101
- 102
- 103
- 104
- 105
- 106
- 107
- 108
- 109
- 110
- 111
- 112
- 113
- 114
- 115
- 116
- 117
- 118
- 119
- 120
- 121
- 122
- 123
- 124
- 125
- 126
- 127
- 128
- 129
- 130
- 131
- 132
- 133
- 134
- 135
- 136
- 137
- 138
- 139
- 140
- 141
- 142
- 143
- 144
- 145
- 146
- 147
- 148
- 149
- 150
- 151
- 152
- 153
- 154
- 155
- 156
- 157
- 158
- 159
- 160
- 161
- 162
- 163
- 164
- 165
- 166
- 167
- 168
- 169
- 170
- 171
- 172
- 173
- 174
- 175
- 176
pom.xml 文件如下所示:
<dependencies><dependency><groupId>org.apache.hive</groupId><artifactId>hive-jdbc</artifactId><version>1.2.1</version></dependency><dependency><groupId>org.apache.hadoop</groupId><artifactId>hadoop-common</artifactId><version>2.7.1</version></dependency><dependency><groupId>org.apache.hive</groupId><artifactId>hive-exec</artifactId><version>1.2.1</version></dependency><dependency><groupId>org.apache.hive</groupId><artifactId>hive-metastore</artifactId><version>1.2.1</version></dependency><dependency><groupId>org.apache.hive</groupId><artifactId>hive-common</artifactId><version>1.2.1</version></dependency><dependency><groupId>org.apache.hive</groupId><artifactId>hive-service</artifactId><version>1.2.1</version></dependency><dependency><groupId>log4j</groupId><artifactId>log4j</artifactId><version>1.2.17</version><type>jar</type></dependency><dependency><groupId>org.apache.hadoop</groupId><artifactId>hadoop-client</artifactId><version>2.7.3</version></dependency>
- 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
參考文檔:?
https://cwiki.apache.org/confluence/display/Hive/HiveServer2+Clients
文檔其中比較值得注意的一點是:
JDBC Client Setup for a Secure Cluster
When connecting to HiveServer2 with Kerberos authentication, the URL format is:
jdbc:hive2://<host>:<port>/<db>;principal=<Server_Principal_of_HiveServer2>
- 這里的principal是固定不變的,其指的hive服務所對應的principal,而不是用戶所對應的principal; 對于這里的可以為不存在的數據庫,但是如果這么做那么在查詢表的時候則需要指出其所在的庫(如db.tablename),否則默認會查詢所對應的表。