Search in sources :

Example 11 with OperationState

use of org.apache.hive.service.cli.OperationState 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(), 1);
    // 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)

Aggregations

OperationState (org.apache.hive.service.cli.OperationState)11 OperationStatus (org.apache.hive.service.cli.OperationStatus)5 OperationHandle (org.apache.hive.service.cli.OperationHandle)4 Test (org.junit.Test)4 HashMap (java.util.HashMap)3 HiveSQLException (org.apache.hive.service.cli.HiveSQLException)3 SessionHandle (org.apache.hive.service.cli.SessionHandle)3 RowSet (org.apache.hive.service.cli.RowSet)2 QueryStatus (co.cask.cdap.proto.QueryStatus)1 IOException (java.io.IOException)1 UnsupportedEncodingException (java.io.UnsupportedEncodingException)1 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 Method (java.lang.reflect.Method)1 SQLException (java.sql.SQLException)1 RejectedExecutionException (java.util.concurrent.RejectedExecutionException)1 FetchTask (org.apache.hadoop.hive.ql.exec.FetchTask)1 SerDeException (org.apache.hadoop.hive.serde2.SerDeException)1 TGetOperationStatusReq (org.apache.hive.service.rpc.thrift.TGetOperationStatusReq)1 TGetOperationStatusResp (org.apache.hive.service.rpc.thrift.TGetOperationStatusResp)1 TException (org.apache.thrift.TException)1