我在资源文件夹中需要读取/写入HDFS的所有4个文件,创建hdfs对象的方法如下所示。
public static FileSystem getHdfsOnPrem(String coreSiteXml, String hdfsSiteXml, String krb5confLoc, String keyTabLoc){
// Setup the configuration object.
try {
Configuration config = new Configuration();
config.addResource(new org.apache.hadoop.fs.Path(coreSiteXml));
config.addResource(new org.apache.hadoop.fs.Path(hdfsSiteXml));
config.set("hadoop.security.authentication", "Kerberos");
config.addResource(krb5confLoc);
config.set("fs.hdfs.impl",org.apache.hadoop.hdfs.DistributedFileSystem.class.getName());
config.set("fs.file.impl",org.apache.hadoop.fs.LocalFileSystem.class.getName());
System.setProperty("java.security.krb5.conf", krb5confLoc);
org.apache.hadoop.security.HadoopKerberosName.setConfiguration(config);
UserGroupInformation.setConfiguration(config);
UserGroupInformation.loginUserFromKeytab("my_username", keyTabLoc);
return org.apache.hadoop.fs.FileSystem.get(config);
}
catch(Exception ex) {
ex.printStackTrace();
return null;
}
}当我在本地运行并作为路径传递到下面时,它就能工作了。
C:\Users\my_username\IdeaProjects\my_project_name\target\scala-2.12\classes\core-site.xml
C:\Users\my_username\IdeaProjects\my_project_name\target\scala-2.12\classes\hdfs-site.xml
C:\Users\my_username\IdeaProjects\my_project_name\target\scala-2.12\classes\krb5.conf
C:\Users\my_username\IdeaProjects\my_project_name\target\scala-2.12\classes\my_username.user.keytab当我在本地运行它时,它运行得很好,但是当我将它打包为JAR并在像kubernetes这样的env中运行它时,它会抛出错误(因为打包为JAR,我可以将资源文件的内容作为流读取,但是我需要传递loginuserFromKeytab方法的路径)。
org.apache.hadoop.security.KerberosAuthException: failure to login: for principal: my_username from keytab file:/opt/spark-3.0.0/jars/foo-project-name!/my_username.user.keytab javax.security.auth.login.LoginException: Unable to obtain password from user如有任何建议或建议,敬请见谅。
发布于 2022-04-04 14:41:13
我建议您使用贾斯配置文件而不是编写这段代码。这有助于从代码中删除安全管道并将其具体化。如果运行您的应用程序的用户没有访问该文件的权限,“无法获得密码”就会发生。
https://stackoverflow.com/questions/71724355
复制相似问题