IHadoopConnection

IHadoopConnection is an interface implemented by the connector's statement class. It provides access to methods that allow for the retrieval, deletion, and renewal of delegation tokens.

The IHadoopStatement interface is defined by the IHadoopStatement.java file. This file should look like the following example:

// =================================================================================================

/// @file IHadoopConnection.java

///

/// Exposed interface for the retrieval of delegation tokens.

///

/// Copyright (C) 2017 Simba Technologies Incorporated.

// =================================================================================================

package com.simba.hiveserver2.hivecommon.core;

import java.sql.Connection;

import java.sql.SQLException;

/**

* An interface that extends the standard SQL Connection Interface but allows for the

* retrieval/renewal/cancellation of delegation tokens.

*/

public interface IHadoopConnection extends Connection

{

/**

* Sends a cancel delegation token request to the server.

*

* @param tokenString The token to cancel.

* @throws SQLException If an error occurs while sending the request.

*/

public void cancelDelegationToken(String tokenString) throws SQLException;

 

/**

* Sends a get delegation token request to the server and returns the token as an

* encoded string.

*

* @param owner The owner of the token.

* @param renewer The renewer of the token.

*

* @return The token as an encoded string.

* @throws SQLException If an error occurs while getting the token.

*/

public String getDelegationToken(String owner, String renewer) throws SQLException;

 

/**

* Sends a renew delegation token request to the sever.

*

* @param tokenString The token to renew.

* @throws SQLException If an error occurs while sending the request.

*/

public void renewDelegationToken(String tokenString) throws SQLException;

}

The following methods are available for use in IHadoopConnection:

  • getDelegationToken(String owner, String renewer)
  • The connector sends a request to the server to obtain a delegation token with the given owner and renewer.

    The method should be called on a Kerberos-authenticated connection.

  • cancelDelegationToken()
  • The connector sends a request to the server to cancel the provided delegation token.

  • renewDelegationToken()
  • The connector sends a request to the server to renew the provided delegation token.

The following is a basic code sample that demonstrates how to use the above functions:

public class TestDelegationTokenClass

{

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);

 

// Excluding code for work with the tokenConnection ...

 

// The original token (delegationToken) can be cancelled or renewed by unwrapping the java.sql.Connection object again to

// an implementation of IHadoopConnection.

 

// Renewing the token:

kerbConnection.unwrap(IHadoopConnection.class).renewDelegationToken(delegationToken);

 

// Cancelling the token:

kerbConnection.unwrap(IHadoopConnection.class).cancelDelegationToken(delegationToken);

}

}