Reply
New Contributor
Posts: 3
Registered: ‎05-14-2017

Java code to access Kerberized HDFS

Hi Experts,

 

I am unable to access Kerberized HDFS using Java code. Below is the code I am using. I have also tried various methods listed here.

 

package dao;

import org.apache.hadoop.conf.*;
import org.apache.hadoop.security.UserGroupInformation;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.fs.FileSystem;

 

public class WriteHDFSFile {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        try {
            Configuration config = new Configuration();
            config.set("fs.defaultFS", "hdfs://hdfs:8020");
            config.set("hadoop.security.authentication", "kerberos");
            UserGroupInformation.setConfiguration(config);
            UserGroupInformation.loginUserFromKeytab("usename@realm", "keytab file path in local compurter D drive");
            FileSystem dfs = FileSystem.get(config);
            Path src=new Path(dfs.getWorkingDirectory() + "<hdfs dir>/file2.txt");
            dfs.createNewFile(src);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

 

With above code I am getting below error:

 

log4j:WARN No appenders could be found for logger (org.apache.hadoop.metrics2.lib.MutableMetricsFactory).
log4j:WARN Please initialize the log4j system properly.
log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.
java.io.IOException: Login failure for <username>@<realm> from keytab D:\abc.keytab: javax.security.auth.login.LoginException: Unable to obtain password from user

    at org.apache.hadoop.security.UserGroupInformation.loginUserFromKeytab(UserGroupInformation.java:978)
    at dao.WriteHDFSFile.main(WriteHDFSFile.java:32)
Caused by: javax.security.auth.login.LoginException: Unable to obtain password from user

    at com.sun.security.auth.module.Krb5LoginModule.promptForPass(Krb5LoginModule.java:897)
    at com.sun.security.auth.module.Krb5LoginModule.attemptAuthentication(Krb5LoginModule.java:760)
    at com.sun.security.auth.module.Krb5LoginModule.login(Krb5LoginModule.java:617)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at javax.security.auth.login.LoginContext.invoke(LoginContext.java:755)
    at javax.security.auth.login.LoginContext.access$000(LoginContext.java:195)
    at javax.security.auth.login.LoginContext$4.run(LoginContext.java:682)
    at javax.security.auth.login.LoginContext$4.run(LoginContext.java:680)
    at java.security.AccessController.doPrivileged(Native Method)
    at javax.security.auth.login.LoginContext.invokePriv(LoginContext.java:680)
    at javax.security.auth.login.LoginContext.login(LoginContext.java:587)
    at org.apache.hadoop.security.UserGroupInformation.loginUserFromKeytab(UserGroupInformation.java:969)
    ... 1 more

 

Thanks in advance,

Tarun

Announcements