This topic explains how to connect a local Java client to the E-MapReduce (EMR) Hive service with Kerberos authentication enabled, on macOS or Linux. The connection uses the Hive Java Database Connectivity (JDBC) driver and a keytab file for authentication.
Prerequisites
Before you begin, make sure you have:
-
An EMR cluster with Kerberos Authentication turned on. Enable this toggle in the Advanced Settings section of the Software Configuration step when creating the cluster. For details, see Create a cluster.
-
SSH access to the master node of the cluster.
How it works
The connection flow has four stages:
-
Retrieve the Kerberos configuration (
krb5.conf) from the cluster's master node. -
Copy the Hive keytab file to your local machine and get the principal name.
-
Allow your local machine to reach the EMR cluster through a security group rule.
-
Write Java code that authenticates with Kerberos and connects via JDBC.
Step 1: Get the Kerberos configuration
-
Log on to the master node using SSH. For details, see Log on to a cluster.
-
Print the Kerberos configuration file:
cat /etc/krb5.confThe file is located at
/etc/krb5.confon themaster-1-1node. Note the value ofdefault_realm— you will use it in the Java code. Example output:[logging] default = FILE:/mnt/disk1/log/kerberos/krb5libs.log kdc = FILE:/mnt/disk1/log/kerberos/krb5kdc.log admin_server = FILE:/mnt/disk1/log/kerberos/kadmind.log [libdefaults] default_realm = EMR.C-EXAMPLE.COM dns_lookup_realm = false dns_lookup_kdc = false ticket_lifetime = 24h renew_lifetime = 7d forwardable = true rdns = false dns_canonicalize_hostname = true pkinit_anchors = FILE:/etc/pki/tls/certs/ca-bundle.crt kdc_timeout = 30s max_retries = 3 [realms] EMR.C-EXAMPLE.COM = { kdc = master-1-1.c-ce2fcb9c9c0b****.cn-hangzhou.emr.aliyuncs.com:88 admin_server = master-1-1.c-ce2fcb9c9c0b****.cn-hangzhou.emr.aliyuncs.com:749 }
Step 2: Copy the keytab file and get the principal
-
Copy the Hive keytab file from the master node to your local machine:
scp root@<public-ip>:/etc/taihao-apps/hive-conf/keytab/hive.keytab /tmp/hive.keytabReplace
<public-ip>with the public IP address of the master node. For details, see Obtain the public IP address and the name of a node. -
Verify the keytab and get the principal:
klist -kt /tmp/hive.keytabExpected output:
Keytab name: FILE:/tmp/hive.keytab KVNO Timestamp Principal ---- ------------------- ------------------------------------------------------ 2 02/25/2025 10:40:41 hive/master-1-1.c-EXAMPLE.cn-hangzhou.emr.aliyuncs.com@EMR.C-EXAMPLE.COM 2 02/25/2025 10:40:41 hive/master-1-1.c-EXAMPLE.cn-hangzhou.emr.aliyuncs.com@EMR.C-EXAMPLE.COMNote the principal value. In this example, it is
hive/master-1-1.c-EXAMPLE.cn-hangzhou.emr.aliyuncs.com@EMR.C-EXAMPLE.COM. You will use this in the Java code.
Step 3: Configure a security group rule
Add a security group rule so your local machine can reach the EMR cluster.
-
Get your local machine's public IP address at https://myip.ipip.net/.
-
Open the security group details:
-
Log on to the EMR console.
-
In the top navigation bar, select the region where your cluster resides and select a resource group as needed.
-
On the EMR on ECS page, click the cluster name.
-
On the Basic Information tab, in the Security section, click the link next to Cluster Security Group.
-
-
On the Rules page, click Add Rule. Set Protocol to All Traffic and Source to the IP address from step 1. Keep the default values for other parameters. For details, see Add security group rules.
Step 4: Write the Java code
Add Maven dependencies
Add the following dependencies to your pom.xml:
<dependencies>
<dependency>
<groupId>org.apache.hive</groupId>
<artifactId>hive-jdbc</artifactId>
<version>3.1.3</version>
</dependency>
<dependency>
<groupId>org.apache.hadoop</groupId>
<artifactId>hadoop-common</artifactId>
<version>3.2.1</version>
</dependency>
<dependency>
<groupId>org.apache.hadoop</groupId>
<artifactId>hadoop-auth</artifactId>
<version>3.2.1</version>
</dependency>
</dependencies>
Sample code
Replace the placeholder values with the default_realm and principal you retrieved in steps 1 and 2, then copy the code into Main.java.
package com.aliyun.emr.example;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.security.UserGroupInformation;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
public class Main {
private static final String DRIVER_CLASS = "org.apache.hive.jdbc.HiveDriver";
public static void main(String[] args) throws Exception {
// Set the Kerberos realm and KDC address (from krb5.conf in Step 1)
System.setProperty("java.security.krb5.realm", "EMR.EXAMPLE.COM");
System.setProperty("java.security.krb5.kdc", "<master-node-address>");
Configuration conf = new Configuration();
conf.set("hadoop.security.authentication", "kerberos");
UserGroupInformation.setConfiguration(conf);
// Authenticate using the keytab file (principal from Step 2)
UserGroupInformation.loginUserFromKeytab(
"hive/master-1-1.c-EXAMPLE.cn-hangzhou.emr.aliyuncs.com@EMR.C-EXAMPLE.COM",
"/tmp/hive.keytab"
);
Class.forName(DRIVER_CLASS);
// The principal in the JDBC URL must match the principal used in loginUserFromKeytab above.
// Note: the "/" after the port number is required. If you omit it, the driver runs
// HiveServer2 in embedded mode instead of connecting to the remote server.
String hivePrincipal = "hive/master-1-1.c-EXAMPLE.cn-hangzhou.emr.aliyuncs.com@EMR.C-EXAMPLE.COM";
String hiveUrl = "jdbc:hive2://<master-node-address>:10000/;principal=" + hivePrincipal;
Connection connection = DriverManager.getConnection(hiveUrl);
Statement statement = connection.createStatement();
ResultSet resultSet = statement.executeQuery("SHOW DATABASES");
while (resultSet.next()) {
System.out.println(resultSet.getString(1));
}
resultSet.close();
statement.close();
connection.close();
}
}
Parameter reference
| Parameter | Value |
|---|---|
java.security.krb5.realm |
The default_realm value from krb5.conf (Step 1) — for example, EMR.C-EXAMPLE.COM |
java.security.krb5.kdc |
The master node address (public IP address or domain name). Must be network-accessible. |
hivePrincipal |
The value of Principal obtained from klist -kt in Step 2. |
First argument of loginUserFromKeytab |
The principal value from klist -kt (Step 2) |
<master-node-address> in hiveUrl |
Same value as java.security.krb5.kdc |
Troubleshooting
Work through authentication issues in layers: verify network connectivity first, then keytab validity, then Java-level configuration.
Verify network connectivity
Before debugging Java errors, confirm that your machine can reach the KDC port:
nc -zv <master-node-address> 88
A successful connection confirms network access to the KDC. If this fails, review the security group rule from Step 3.
Verify the keytab
Confirm that the keytab is valid and the principal is correct:
klist -kt /tmp/hive.keytab
Then test authentication end-to-end:
kinit -kt /tmp/hive.keytab hive/master-1-1.c-EXAMPLE.cn-hangzhou.emr.aliyuncs.com@EMR.C-EXAMPLE.COM
klist
A ticket in the klist output confirms the keytab works. If kinit fails, the keytab or principal is incorrect.
Enable Kerberos debug output
To see detailed Kerberos negotiation logs, add this line at the start of your main method:
System.setProperty("sun.security.krb5.debug", "true");
This writes Kerberos debug output from the JVM's Kerberos libraries to stdout. The output shows each step of the authentication handshake and helps identify where the negotiation fails. The amount of detail varies depending on which stage fails — exception messages and stack traces typically point to the root cause. For more details, see Troubleshooting security in the Java documentation.
Common errors
| Error | Cause | Solution |
|---|---|---|
Cannot contact any KDC |
KDC address is incorrect or unreachable | Verify the KDC address in krb5.conf. Test connectivity with nc -zv <address> 88. |
keytab contains no suitable keys |
Keytab does not match the principal | Run klist -kt /path/to/hive.keytab and confirm the principal matches exactly. |
LoginException: Unable to obtain password |
Keytab file is not readable | Run chmod 400 /path/to/hive.keytab to set correct permissions. |
GSS initiate failed |
Kerberos is misconfigured | Make sure that the java.security.krb5.conf configuration is correct. |
For errors not listed here, see Common Kerberos error messages (A-M) and Common Kerberos error messages (N-Z) from Oracle, and the MIT Kerberos troubleshooting guide.