Search in sources :

Example 1 with OperationState

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

the class ThriftCLIServiceTest method testExecuteStatement.

/**
   * Test synchronous query execution
   * @throws Exception
   */
@Test
public void testExecuteStatement() 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);
    // 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_THRIFT";
    client.executeStatement(sessHandle, queryString, opConf);
    // Create a test table
    queryString = "CREATE TABLE TEST_EXEC_THRIFT(ID STRING)";
    client.executeStatement(sessHandle, queryString, opConf);
    // Execute another query
    queryString = "SELECT ID+1 FROM TEST_EXEC_THRIFT";
    OperationHandle opHandle = client.executeStatement(sessHandle, queryString, opConf);
    assertNotNull(opHandle);
    OperationStatus opStatus = client.getOperationStatus(opHandle, false);
    assertNotNull(opStatus);
    OperationState state = opStatus.getState();
    // Expect query to be completed now
    assertEquals("Query should be finished", OperationState.FINISHED, state);
    // Cleanup
    queryString = "DROP TABLE TEST_EXEC_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 2 with OperationState

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

the class ThriftCliServiceTestWithCookie method testExecuteStatement.

/**
   * Test synchronous query execution
   * @throws Exception
   */
@Test
public void testExecuteStatement() 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);
    // 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_THRIFT";
    client.executeStatement(sessHandle, queryString, opConf);
    // Create a test table
    queryString = "CREATE TABLE TEST_EXEC_THRIFT(ID STRING)";
    client.executeStatement(sessHandle, queryString, opConf);
    // Execute another query
    queryString = "SELECT ID+1 FROM TEST_EXEC_THRIFT";
    OperationHandle opHandle = client.executeStatement(sessHandle, queryString, opConf);
    assertNotNull(opHandle);
    OperationStatus opStatus = client.getOperationStatus(opHandle, false);
    assertNotNull(opStatus);
    OperationState state = opStatus.getState();
    // Expect query to be completed now
    assertEquals("Query should be finished", OperationState.FINISHED, state);
    // Cleanup
    queryString = "DROP TABLE TEST_EXEC_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 3 with OperationState

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

the class ThriftCLIServiceClient method getOperationStatus.

/* (non-Javadoc)
   * @see org.apache.hive.service.cli.ICLIService#getOperationStatus(org.apache.hive.service.cli.OperationHandle)
   */
@Override
public OperationStatus getOperationStatus(OperationHandle opHandle, boolean getProgressUpdate) throws HiveSQLException {
    try {
        TGetOperationStatusReq req = new TGetOperationStatusReq(opHandle.toTOperationHandle());
        req.setGetProgressUpdate(getProgressUpdate);
        TGetOperationStatusResp resp = cliService.GetOperationStatus(req);
        // Checks the status of the RPC call, throws an exception in case of error
        checkStatus(resp.getStatus());
        OperationState opState = OperationState.getOperationState(resp.getOperationState());
        HiveSQLException opException = null;
        if (opState == OperationState.ERROR) {
            opException = new HiveSQLException(resp.getErrorMessage(), resp.getSqlState(), resp.getErrorCode());
        }
        return new OperationStatus(opState, resp.getTaskStatus(), resp.getOperationStarted(), resp.getOperationCompleted(), resp.isHasResultSet(), opException);
    } catch (HiveSQLException e) {
        throw e;
    } catch (Exception e) {
        throw new HiveSQLException(e);
    }
}
Also used : TGetOperationStatusResp(org.apache.hive.service.rpc.thrift.TGetOperationStatusResp) HiveSQLException(org.apache.hive.service.cli.HiveSQLException) OperationStatus(org.apache.hive.service.cli.OperationStatus) TGetOperationStatusReq(org.apache.hive.service.rpc.thrift.TGetOperationStatusReq) OperationState(org.apache.hive.service.cli.OperationState) HiveSQLException(org.apache.hive.service.cli.HiveSQLException) TException(org.apache.thrift.TException)

Example 4 with OperationState

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

the class SQLOperation method runQuery.

private void runQuery() throws HiveSQLException {
    try {
        OperationState opState = getStatus().getState();
        // Operation may have been cancelled by another thread
        if (opState.isTerminal()) {
            LOG.info("Not running the query. Operation is already in terminal state: " + opState + ", perhaps cancelled due to query timeout or by another thread.");
            return;
        }
        // In Hive server mode, we are not able to retry in the FetchTask
        // case, when calling fetch queries since execute() has returned.
        // For now, we disable the test attempts.
        driver.setTryCount(Integer.MAX_VALUE);
        response = driver.run();
        if (0 != response.getResponseCode()) {
            throw toSQLException("Error while processing statement", response);
        }
    } catch (Throwable e) {
        /**
       * If the operation was cancelled by another thread, or the execution timed out, Driver#run
       * may return a non-zero response code. We will simply return if the operation state is
       * CANCELED, TIMEDOUT or CLOSED, otherwise throw an exception
       */
        if ((getStatus().getState() == OperationState.CANCELED) || (getStatus().getState() == OperationState.TIMEDOUT) || (getStatus().getState() == OperationState.CLOSED)) {
            LOG.warn("Ignore exception in terminal state", e);
            return;
        }
        setState(OperationState.ERROR);
        if (e instanceof HiveSQLException) {
            throw (HiveSQLException) e;
        } else {
            throw new HiveSQLException("Error running query: " + e.toString(), e);
        }
    }
    setState(OperationState.FINISHED);
}
Also used : HiveSQLException(org.apache.hive.service.cli.HiveSQLException) OperationState(org.apache.hive.service.cli.OperationState)

Example 5 with OperationState

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

the class Operation method setState.

protected final OperationState setState(OperationState newState) throws HiveSQLException {
    state.validateTransition(newState);
    OperationState prevState = state;
    this.state = newState;
    currentStateScope = updateOperationStateMetrics(currentStateScope, MetricsConstant.OPERATION_PREFIX, MetricsConstant.COMPLETED_OPERATION_PREFIX, state);
    onNewState(state, prevState);
    this.lastAccessTime = System.currentTimeMillis();
    return this.state;
}
Also used : OperationState(org.apache.hive.service.cli.OperationState)

Aggregations

OperationState (org.apache.hive.service.cli.OperationState)10 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 CommandNeedRetryException (org.apache.hadoop.hive.ql.CommandNeedRetryException)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