Code Samples: Retrieving a Hadoop Delegation Token
If you are using a Hadoop delegation token for authentication, the token must be provided to the connector in the form of a Base64 URL-safe encoded string. This token can be obtained from the connector using the getDelegationToken()
function, or by utilizing the Hadoop distribution .jar
files.
The code samples below demonstrate the use of the getDelegationToken()
function. For more information about this function, see IHadoopConnection.
The sample below shows how to obtain the token string with the connector using a Kerberos connection:
import com.simba.hiveserver2.hivecommon.core.IHadoopConnection;
public class TestDriverGetDelegationTokenClass
{
public static void main(String[] args) throws SQLException
{
// Create the connection object with Kerberos authentication.
Connection kerbConnection = DriverManager.getConnection(
"jdbc:hive2://localhost:10000;AuthMech=1;KrbRealm=YourRealm;KrbHostFQDN=sample.com;KrbServiceName=hive;");
// Unwrap the java.sql.Connection object to an implementation of IHadoopConnection so the
// methods for delegation token can be called.
String delegationToken = kerbConnection.unwrap(IHadoopConnection.class).getDelegationToken("owner_name", "renewer_name");
// The token can then be used to connect with the connector.
String tokenConnectionString = "jdbc:hive2://localhost:10000;AuthMech=6;DelegationToken=" + delegationToken;
Connection tokenConnection = DriverManager.getConnection(tokenConnectionString);
}
}
The sample below demonstrates how to obtain the encoded string form of the token if the delegation is saved to the UserGroupInformation. This sample requires the hadoop-shims-common-[hadoop version].jar
, hadoop-common-[hadoop version].jar
, and all their dependencies.
import org.apache.hadoop.hive.shims.Utils;
import org.apache.hive.service.auth.HiveAuthFactory;
public class TestHadoopDelegationTokenClass
{
public static void main(String[] args) throws SQLException
{
// Obtain the delegationToken stored in the current UserGroupInformation.
String delegationToken = Utils.getTokenStrForm(HiveAuthFactory.HS2_CLIENT_TOKEN);
// The token can then be used to connect with the connector.
String tokenConnectionString = "jdbc:hive2://localhost:10000;AuthMech=6;DelegationToken=" + delegationToken;
Connection tokenConnection = DriverManager.getConnection(tokenConnectionString);
}
}
- Security and Authentication
- Building the Connection URL
- Authentication Driver Configuration Options on page 1
- Authentication Mechanisms
- Using No Authentication
- Using Kerberos
- Using User Name And Password