Connecting to Kerberos secured HBase cluster from Java application
Short Description:
This sample code helps to connect to Kerberos enabled HBase cluster from Java program.
Code Walkthrough:
Create HBaseConfiguration and pass HBase cluster parameters.
Configuration hbaseConfig = HBaseConfiguration.create();
hbaseConfig.addResource("/path_to_hbase_conf/hbase-site.xml");
hbaseConfig.addResource("/path_to_hbase_conf/core-site.xml");
hbaseConfig.set("hadoop.security.authentication", "Kerberos");
User principal and key tab file names. Please make sure key tab files are in the respective folder.
String principal = System.getProperty("kerberosPrincipal", "hbaseuser@EXAMPLE.COM");
String keytab = System.getProperty("kerberosKeytab", "/path_to_keytab/hbase-client.keytab");
The essential Kerberos configuration information is the default realm and the default KDC. As with most Kerberos installations, a Kerberos configuration file krb5.conf is consulted to determine such things as the default realm and KDC. The default location is /etc/krb5.conf (Linux).
If the krb5.conf file is in a different location or you want to pass custom krb5.conf:
System.setProperty("java.security.krb5.conf","src/krb5.conf");
Login user from key tab file:
UserGroupInformation.setConfiguration(hbaseConfig);
UserGroupInformation.loginUserFromKeytab(principal, keytab);
Check the connection:
HBaseAdmin.checkHBaseAvailable(hbaseConfig);
Options to enable debug log:
System.setProperty("sun.security.jgss.debug", "true");
System.setProperty("sun.security.krb5.debug", "true");
System.setProperty("sun.security.jgss.debug", "true");
System.setProperty("java.security.debug", "logincontext,policy,scl,gssloginconfig");
Well, you are good to go now.

