Search in sources :

Example 81 with SessionHandle

use of org.apache.hive.service.cli.SessionHandle in project hive by apache.

the class ThriftCLIServiceTest method testExecuteStatementAsync.

/**
 * Test asynchronous query execution and error reporting to the client
 * @throws Exception
 */
@Test
public void testExecuteStatementAsync() throws Exception {
    Map<String, String> opConf = new HashMap<String, String>();
    // Open a new client session
    SessionHandle sessHandle = client.openSession(USERNAME, PASSWORD, opConf);
    // Session handle should not be null
    assertNotNull("Session handle should not be null", sessHandle);
    OperationHandle opHandle;
    OperationStatus opStatus;
    OperationState state = null;
    // Change lock manager to embedded mode
    String queryString = "SET hive.lock.manager=" + "org.apache.hadoop.hive.ql.lockmgr.EmbeddedLockManager";
    client.executeStatement(sessHandle, queryString, opConf);
    // Drop the table if it exists
    queryString = "DROP TABLE IF EXISTS TEST_EXEC_ASYNC_THRIFT";
    client.executeStatement(sessHandle, queryString, opConf);
    // Create a test table
    queryString = "CREATE TABLE TEST_EXEC_ASYNC_THRIFT(ID STRING)";
    client.executeStatement(sessHandle, queryString, opConf);
    // Execute another query
    queryString = "SELECT ID+1 FROM TEST_EXEC_ASYNC_THRIFT";
    System.out.println("Will attempt to execute: " + queryString);
    opHandle = client.executeStatementAsync(sessHandle, queryString, opConf);
    assertNotNull(opHandle);
    // Poll on the operation status till the query is completed
    boolean isQueryRunning = true;
    long pollTimeout = System.currentTimeMillis() + 100000;
    while (isQueryRunning) {
        // Break if polling times out
        if (System.currentTimeMillis() > pollTimeout) {
            System.out.println("Polling timed out");
            break;
        }
        opStatus = client.getOperationStatus(opHandle, false);
        assertNotNull(opStatus);
        state = opStatus.getState();
        System.out.println("Current state: " + state);
        if (state == OperationState.CANCELED || state == OperationState.CLOSED || state == OperationState.FINISHED || state == OperationState.ERROR) {
            isQueryRunning = false;
        }
        Thread.sleep(1000);
    }
    // Expect query to be successfully completed now
    assertEquals("Query should be finished", OperationState.FINISHED, state);
    // Execute a malformed query
    // This query will give a runtime error
    queryString = "CREATE TABLE NON_EXISTING_TAB (ID STRING) location 'hdfs://localhost:10000/a/b/c'";
    System.out.println("Will attempt to execute: " + queryString);
    opHandle = client.executeStatementAsync(sessHandle, queryString, opConf);
    assertNotNull(opHandle);
    opStatus = client.getOperationStatus(opHandle, false);
    assertNotNull(opStatus);
    isQueryRunning = true;
    pollTimeout = System.currentTimeMillis() + 100000;
    while (isQueryRunning) {
        // Break if polling times out
        if (System.currentTimeMillis() > pollTimeout) {
            System.out.println("Polling timed out");
            break;
        }
        state = opStatus.getState();
        System.out.println("Current state: " + state);
        if (state == OperationState.CANCELED || state == OperationState.CLOSED || state == OperationState.FINISHED || state == OperationState.ERROR) {
            isQueryRunning = false;
        }
        Thread.sleep(1000);
        opStatus = client.getOperationStatus(opHandle, false);
    }
    // Expect query to return an error state
    assertEquals("Operation should be in error state", OperationState.ERROR, state);
    // sqlState, errorCode should be set to appropriate values
    assertEquals(opStatus.getOperationException().getSQLState(), "08S01");
    assertEquals(opStatus.getOperationException().getErrorCode(), 40000);
    // Cleanup
    queryString = "DROP TABLE TEST_EXEC_ASYNC_THRIFT";
    client.executeStatement(sessHandle, queryString, opConf);
    client.closeSession(sessHandle);
}
Also used : HashMap(java.util.HashMap) OperationStatus(org.apache.hive.service.cli.OperationStatus) SessionHandle(org.apache.hive.service.cli.SessionHandle) OperationHandle(org.apache.hive.service.cli.OperationHandle) OperationState(org.apache.hive.service.cli.OperationState) Test(org.junit.Test)

Example 82 with SessionHandle

use of org.apache.hive.service.cli.SessionHandle in project hive by apache.

the class ThriftCLIServiceTest method testOpenSession.

@Test
public void testOpenSession() throws Exception {
    // Open a new client session
    SessionHandle sessHandle = client.openSession(USERNAME, PASSWORD, new HashMap<String, String>());
    // Session handle should not be null
    assertNotNull("Session handle should not be null", sessHandle);
    // Close client session
    client.closeSession(sessHandle);
}
Also used : SessionHandle(org.apache.hive.service.cli.SessionHandle) Test(org.junit.Test)

Example 83 with SessionHandle

use of org.apache.hive.service.cli.SessionHandle in project hive by apache.

the class ThriftCLIServiceTest method testGetFunctions.

@Test
public void testGetFunctions() throws Exception {
    SessionHandle sessHandle = client.openSession(USERNAME, PASSWORD, new HashMap<String, String>());
    assertNotNull("Session handle should not be null", sessHandle);
    String catalogName = null;
    String schemaName = null;
    String functionName = "*";
    OperationHandle opHandle = client.getFunctions(sessHandle, catalogName, schemaName, functionName);
    assertNotNull("Operation handle should not be null", opHandle);
    client.closeSession(sessHandle);
}
Also used : SessionHandle(org.apache.hive.service.cli.SessionHandle) OperationHandle(org.apache.hive.service.cli.OperationHandle) Test(org.junit.Test)

Aggregations

SessionHandle (org.apache.hive.service.cli.SessionHandle)83 OperationHandle (org.apache.hive.service.cli.OperationHandle)47 HiveSQLException (org.apache.hive.service.cli.HiveSQLException)39 Test (org.junit.Test)36 IOException (java.io.IOException)20 TException (org.apache.thrift.TException)18 UnknownHostException (java.net.UnknownHostException)16 LoginException (javax.security.auth.login.LoginException)16 ServiceException (org.apache.hive.service.ServiceException)16 HashMap (java.util.HashMap)13 ExploreException (co.cask.cdap.explore.service.ExploreException)12 QueryHandle (co.cask.cdap.proto.QueryHandle)11 CLIServiceClient (org.apache.hive.service.cli.CLIServiceClient)11 RowSet (org.apache.hive.service.cli.RowSet)8 TimeoutException (java.util.concurrent.TimeoutException)7 CLIService (org.apache.hive.service.cli.CLIService)6 ThriftCLIServiceClient (org.apache.hive.service.cli.thrift.ThriftCLIServiceClient)6 File (java.io.File)4 HiveConf (org.apache.hadoop.hive.conf.HiveConf)4 ArrayList (java.util.ArrayList)3