Connect a local Java client on macOS or Linux to a Kerberos-enabled Hive service in E-MapReduce (EMR) by configuring Kerberos authentication and using the Hive JDBC driver.
Prerequisites
You have created a cluster and enabled Kerberos Authentication in the Advanced Settings section of the Software Configuration page. For more information, see Create a cluster.
Step 1: Get the Kerberos configuration
-
Log on to the cluster's master node by using SSH. For more information, see Log on to a cluster.
-
Run the following command to get the Kerberos configuration file krb5.conf. This file is typically located at /etc/krb5.conf on the master-1-1 node.
cat /etc/krb5.confUse the default_realm value from this file in your Java code. The following example shows the configuration:
[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: Get the keytab file and principal
-
Copy the Hive keytab file to your local development environment.
scp root@<public IP address>:/etc/taihao-apps/hive-conf/keytab/hive.keytab /tmp/hive.keytabReplace <public IP address> with the public IP address of the master node. For more information, see Obtain the public IP address and the name of a node.
-
Verify the validity of the keytab file on your local machine and obtain the principal.
klist -kt /tmp/hive.keytabThe following example shows the output from a successful command:
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.COMYou will use this principal (for example, hive/master-1-1.c-EXAMPLE.cn-hangzhou.emr.aliyuncs.com@EMR.C-EXAMPLE.COM) in your Java code.
Step 3: Configure network access control policies
Configure security group rules to allow your local development environment to access the EMR cluster.
-
Obtain the public IP address of your local development environment.
You can find your public IP address by using an IP address lookup service.
-
Go to the security group page.
-
Log on to the EMR console.
-
In the top navigation bar, select the region and resource group.
-
On the Clusters page, click the cluster ID of the target cluster.
-
On the Basic Information page, click the link next to Cluster Security Group.
-
-
On the Rules page, click Add Rule.
Set Protocol to All Traffic and Source to the public IP address of your local environment. Use the default values for the other parameters. For more information, see Add security group rules.
Step 4: Write the Java code
Configure Maven dependencies
Add the following dependencies to your pom.xml file.
<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>
Java code example
In the sample code below, replace the parameter values with the information from Step 1: Get the Kerberos configuration and Step 2: Get the keytab file and principal, and then add the code to your Main.java file.
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 realm and Key Distribution Center (KDC) address for Kerberos authentication.
System.setProperty("java.security.krb5.realm", "EMR.EXAMPLE.COM");
System.setProperty("java.security.krb5.kdc", "$IPORHOST");
Configuration conf = new Configuration();
conf.set("hadoop.security.authentication", "kerberos");
UserGroupInformation.setConfiguration(conf);
// Log on to Kerberos by using the keytab file.
UserGroupInformation.loginUserFromKeytab(
"hive/master-1-1.c-EXAMPLE.cn-hangzhou.emr.aliyuncs.com@EMR.C-EXAMPLE.COM",
"/tmp/hive.keytab"
);
Class.forName(DRIVER_CLASS);
// Define the Hive principal for JDBC connection authentication.
String hivePrincipal = "hive/master-1-1.c-EXAMPLE.cn-hangzhou.emr.aliyuncs.com@EMR.C-EXAMPLE.COM";
// Construct the Hive JDBC URL, which includes the connection address and principal information.
String hiveUrl = "jdbc:hive2://$IPORHOST: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();
}
}
The following table describes the parameters to configure.
|
Parameter |
Description |
|
java.security.krb5.realm |
The default_realm value in the krb5.conf file obtained in Step 1: Get the Kerberos configuration. |
|
java.security.krb5.kdc |
The KDC server address. Set this to the public IP address or domain name of the master node. Ensure the address is accessible from your client. |
|
hivePrincipal |
The principal obtained in Step 2: Get the keytab file and principal. |
|
UserGroupInformation.loginUserFromKeytab |
The first argument is the hivePrincipal value. |
|
hiveUrl |
$IPORHOST is the java.security.krb5.kdc value. |
To debug the connection, add the following line to the beginning of the main method.
System.setProperty("sun.security.krb5.debug", "true");
Troubleshooting
|
Issue |
Cause |
Solution |
|
Cannot contact any KDC |
The KDC address is incorrect or the network is unreachable. |
Verify that the KDC address in the krb5.conf file is correct, and run |
|
keytab contains no suitable keys |
The keytab file does not match the principal. |
Run |
|
LoginException: Unable to obtain password |
The keytab file is inaccessible. |
Run |
|
GSS initiate failed |
The Kerberos configuration is incorrect. |
Ensure that the |