教培參考
教育培訓(xùn)行業(yè)知識(shí)型媒體
發(fā)布時(shí)間: 2025年05月18日 06:19
原理架構(gòu)
?
通過HDFS提供的接口獲取(具體接口信息可以參考HDFS二次開發(fā)指南)。
private Boolean login(Configuration conf){
boolean flag = false;
UserGroupInformation.setConfiguration(conf);
try {
UserGroupInformation.loginUserFromKeytab(conf.get(PRINCIPAL),conf.get(KEYTAB));
System.out.println("UserGroupInformation.isLoginKeytabbased():" +UserGroupInformation.isLoginKeytabbased());
flag = true;
} catch (IOException e) {
e.printStackTrace();
}
return flag;
}
通過客戶端shell命令以kinit方式獲取,具體使用方式可參考Shell操作維護(hù)命令說明書。認(rèn)證代碼實(shí)例講解?
private void init() throws IOException {
Configuration conf = new Configuration();
// conf file
conf.addResource(new Path(System.getProperty("user.dir")
+ File.separator + "conf" + File.separator + "hdfs-site.xml"));
conf.addResource(new Path(System.getProperty("user.dir")
+ File.separator + "conf" + File.separator + "core-site.xml"));
// security mode
if ("kerberos".equalsIgnoreCase(conf
.get("hadoop.security.authentication"))) {
// 注[1]
conf.set(PRINCIPAL,"hdfstest@HADOOP.COM");
// keytab file
conf.set(KEYTAB,System.getProperty("user.dir") + File.separator
+ "conf" + File.separator + "user.keytab");
// kerberos path
String krbfilepath = System.getProperty("user.dir")
+ File.separator + "conf" + File.separator + "krb5.conf";
System.setProperty("java.security.krb5.conf",krbfilepath);
login(conf); //注[2]
}
// get filesystem
try {
fSystem = FileSystem.get(conf); //注[3]
} catch (IOException e) {
throw new IOException("Get fileSystem failed.");
}
}
private Boolean login(Configuration conf){
boolean flag = false;
UserGroupInformation.setConfiguration(conf);
try {
UserGroupInformation.loginUserFromKeytab(conf.get(PRINCIPAL),conf.get(KEYTAB));
System.out.println("UserGroupInformation.isLoginKeytabbased(): " +UserGroupInformation.isLoginKeytabbased());
flag = true;
} catch (IOException e) {
e.printStackTrace();
}
return flag;
}
微信掃碼關(guān)注公眾號(hào)
獲取更多考試熱門資料