您好,登錄后才能下訂單哦!
本篇內容介紹了“java怎么訪問配置kerberos”的有關知識,在實際案例的操作過程中,不少人都會遇到這樣的困境,接下來就讓小編帶領大家學習一下如何處理這些情況吧!希望大家仔細閱讀,能夠學有所成!
1.準備
1.1 生成主體hive/hive的keytab文件到指定目錄/var/keytab/hive.keytab
[root@fan102 ~]# kadmin.local -q "xst -k /var/keytab/hive.keytab hive/hive@HADOOP.COM"
1.2 查看keytab內容
[root@fan102 ~]# cd var/keytab
[root@fan102 keytab]# klist -e -k hive.keytab
Keytab name: FILE:hive.keytab
KVNO Principal
---- --------------------------------------------------------------------------
3 hive/hive@HADOOP.COM (aes128-cts-hmac-sha1-96)
3 hive/hive@HADOOP.COM (des3-cbc-sha1)
3 hive/hive@HADOOP.COM (arcfour-hmac)
3 hive/hive@HADOOP.COM (camellia256-cts-cmac)
3 hive/hive@HADOOP.COM (camellia128-cts-cmac)
3 hive/hive@HADOOP.COM (des-hmac-sha1)
3 hive/hive@HADOOP.COM (des-cbc-md5)
1.3 驗證是否登錄成功
[root@fan102 ~]# kinit -kt /var/keytab/hive.keytab hive/hive@HADOOP.COM
[root@fan102 ~]# klist
Ticket cache: FILE:/tmp/krb5cc_0
Default principal: hive/hive@HADOOP.COM
Valid starting Expires Service principal
07/14/2020 14:08:26 07/15/2020 14:08:26 krbtgt/HADOOP.COM@HADOOP.COM
renew until 07/21/2020 14:08:26
1.4 將kerberos配置文件和keytab文件拷貝到Windows本地(我的路徑是:D:\keytab)
[root@fan102 ~]# cat /etc/krb5.conf
[root@fan102 ~]# cat /var/keytab/hive.keytab
1.5 查看krb5.conf文件中realms下IP位置內容,如果是服務器名,則要替換成服務器的IP地址,原本是IP
地址無需修改
[realms]
HADOOP.COM = {
kdc = IP
admin_server = IP
}
2.java連接部分
2.1 依賴(我的版本是2.6)
<dependencies>
<!-- https://mvnrepository.com/artifact/org.apache.hadoop/hadoop-common -->
<dependency>
<groupId>org.apache.hadoop</groupId>
<artifactId>hadoop-common</artifactId>
<version>2.6.0</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.hive/hive-jdbc -->
<dependency>
<groupId>org.apache.hive</groupId>
<artifactId>hive-jdbc</artifactId>
<version>1.1.1</version>
</dependency>
</dependencies>
2.2 代碼(IP處替換為自己IP)
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.security.UserGroupInformation;
import java.sql.*;
public class KerberosTest {
private static String JDBC_DRIVER = "org.apache.hive.jdbc.HiveDriver";
private static String CONNECTION_URL =
"jdbc:hive2://IP:10000/;principal=hive/fan102@HADOOP.COM";
static {
try {
Class.forName(JDBC_DRIVER);
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
public static void main(String[] args) throws Exception {
Class.forName(JDBC_DRIVER);
//登錄Kerberos賬號
System.setProperty("java.security.krb5.conf", "D:\\keytab\\krb5.conf");
Configuration configuration = new Configuration();
configuration.set("hadoop.security.authentication", "Kerberos");
UserGroupInformation.setConfiguration(configuration);
UserGroupInformation.loginUserFromKeytab("hive/hive@HADOOP.COM",
"D:\\keytab\\hive.keytab");
Connection connection = null;
ResultSet rs = null;
PreparedStatement ps = null;
try {
connection = DriverManager.getConnection(CONNECTION_URL);
ps = connection.prepareStatement("show databases");
rs = ps.executeQuery();
ResultSetMetaData metaData = null;
while (rs.next()) {
metaData = rs.getMetaData();
System.err.println(rs.getString(1));
}
System.err.println(metaData.getColumnName(1));
} catch (Exception e) {
e.printStackTrace();
}
}
}
3 結果
3.1 java查詢結果
3.2 Linux上,beeline驗證結果
注:1.3 步驟操作無異常
[root@fan102 ~]# beeline -u "jdbc:hive2://fan102:10000/;principal=hive/fan102@HADOOP.COM"
> show databases;
“java怎么訪問配置kerberos”的內容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業相關的知識可以關注億速云網站,小編將為大家輸出更多高質量的實用文章!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。