Search in sources :

Example 11 with QueryStatus

use of co.cask.cdap.proto.QueryStatus in project cdap by caskdata.

the class BaseHiveExploreService method execute.

@Override
public QueryHandle execute(NamespaceId namespace, String[] statements) throws ExploreException, SQLException {
    Preconditions.checkArgument(statements.length > 0, "There must be at least one statement");
    startAndWait();
    try {
        SessionHandle sessionHandle = null;
        OperationHandle operationHandle = null;
        Map<String, String> sessionConf = startSession(namespace);
        String database = getHiveDatabase(namespace.getNamespace());
        try {
            sessionHandle = openHiveSession(sessionConf);
            // Switch database to the one being passed in.
            setCurrentDatabase(database);
            // synchronously execute all but the last statement
            for (int i = 0; i < statements.length - 1; i++) {
                String statement = statements[i];
                LOG.trace("Executing statement synchronously: {}", statement);
                operationHandle = executeSync(sessionHandle, statement);
                QueryStatus status = doFetchStatus(operationHandle);
                if (QueryStatus.OpStatus.ERROR == status.getStatus()) {
                    throw new HiveSQLException(status.getErrorMessage(), status.getSqlState());
                }
                if (QueryStatus.OpStatus.FINISHED != status.getStatus()) {
                    throw new ExploreException("Expected operation status FINISHED for statement '{}' but received " + status.getStatus());
                }
            }
            String statement = statements[statements.length - 1];
            operationHandle = executeAsync(sessionHandle, statement);
            QueryHandle handle = saveReadWriteOperation(operationHandle, sessionHandle, sessionConf, statement, database);
            LOG.trace("Executing statement: {} with handle {}", statement, handle);
            return handle;
        } catch (Throwable e) {
            closeInternal(getQueryHandle(sessionConf), new ReadWriteOperationInfo(sessionHandle, operationHandle, sessionConf, "", database));
            throw e;
        }
    } catch (HiveSQLException e) {
        throw getSqlException(e);
    } catch (Throwable e) {
        throw new ExploreException(e);
    }
}
Also used : HiveSQLException(org.apache.hive.service.cli.HiveSQLException) SessionHandle(org.apache.hive.service.cli.SessionHandle) QueryHandle(co.cask.cdap.proto.QueryHandle) OperationHandle(org.apache.hive.service.cli.OperationHandle) QueryStatus(co.cask.cdap.proto.QueryStatus) ExploreException(co.cask.cdap.explore.service.ExploreException)

Example 12 with QueryStatus

use of co.cask.cdap.proto.QueryStatus in project cdap by caskdata.

the class Hive12ExploreService method doFetchStatus.

@Override
protected QueryStatus doFetchStatus(OperationHandle operationHandle) throws HiveSQLException, ExploreException, HandleNotFoundException {
    try {
        // In Hive 12, CLIService.getOperationStatus returns OperationState.
        // In Hive 13, CLIService.getOperationStatus returns OperationStatus.
        // Since we use Hive 13 for dev, we need the following workaround to get Hive 12 working.
        Class<? extends CLIService> cliServiceClass = getCliService().getClass();
        Method m = cliServiceClass.getMethod("getOperationStatus", OperationHandle.class);
        OperationState operationState = (OperationState) m.invoke(getCliService(), operationHandle);
        return new QueryStatus(QueryStatus.OpStatus.valueOf(operationState.toString()), operationHandle.hasResultSet());
    } catch (InvocationTargetException | NoSuchMethodException | IllegalAccessException e) {
        throw Throwables.propagate(e);
    }
}
Also used : Method(java.lang.reflect.Method) QueryStatus(co.cask.cdap.proto.QueryStatus) OperationState(org.apache.hive.service.cli.OperationState) InvocationTargetException(java.lang.reflect.InvocationTargetException)

Example 13 with QueryStatus

use of co.cask.cdap.proto.QueryStatus in project cdap by caskdata.

the class BaseHiveExploreServiceTest method waitForCompletionStatus.

protected static QueryStatus waitForCompletionStatus(QueryHandle handle, long sleepTime, TimeUnit timeUnit, int maxTries) throws ExploreException, HandleNotFoundException, InterruptedException, SQLException {
    QueryStatus status;
    int tries = 0;
    do {
        timeUnit.sleep(sleepTime);
        status = exploreService.getStatus(handle);
        if (++tries > maxTries) {
            break;
        }
    } while (!status.getStatus().isDone());
    return status;
}
Also used : QueryStatus(co.cask.cdap.proto.QueryStatus)

Example 14 with QueryStatus

use of co.cask.cdap.proto.QueryStatus in project cdap by caskdata.

the class HiveExploreServiceTestRun method previewResultsTest.

@Test
public void previewResultsTest() throws Exception {
    DatasetId myTable2 = NAMESPACE_ID.dataset("my_table_2");
    DatasetId myTable3 = NAMESPACE_ID.dataset("my_table_3");
    DatasetId myTable4 = NAMESPACE_ID.dataset("my_table_4");
    DatasetId myTable5 = NAMESPACE_ID.dataset("my_table_5");
    DatasetId myTable6 = NAMESPACE_ID.dataset("my_table_6");
    datasetFramework.addInstance("keyStructValueTable", myTable2, DatasetProperties.EMPTY);
    datasetFramework.addInstance("keyStructValueTable", myTable3, DatasetProperties.EMPTY);
    datasetFramework.addInstance("keyStructValueTable", myTable4, DatasetProperties.EMPTY);
    datasetFramework.addInstance("keyStructValueTable", myTable5, DatasetProperties.EMPTY);
    datasetFramework.addInstance("keyStructValueTable", myTable6, DatasetProperties.EMPTY);
    try {
        QueryHandle handle = exploreService.execute(NAMESPACE_ID, "show tables");
        QueryStatus status = waitForCompletionStatus(handle, 200, TimeUnit.MILLISECONDS, 50);
        Assert.assertEquals(QueryStatus.OpStatus.FINISHED, status.getStatus());
        List<QueryResult> firstPreview = exploreService.previewResults(handle);
        Assert.assertEquals(ImmutableList.of(new QueryResult(ImmutableList.<Object>of(MY_TABLE_NAME)), new QueryResult(ImmutableList.<Object>of(getDatasetHiveName(myTable2))), new QueryResult(ImmutableList.<Object>of(getDatasetHiveName(myTable3))), new QueryResult(ImmutableList.<Object>of(getDatasetHiveName(myTable4))), new QueryResult(ImmutableList.<Object>of(getDatasetHiveName(myTable5)))), firstPreview);
        // test that preview results do not change even when the query cursor is updated by nextResults
        List<QueryResult> endResults = exploreService.nextResults(handle, 100);
        Assert.assertEquals(ImmutableList.of(new QueryResult(ImmutableList.<Object>of(getDatasetHiveName(myTable6)))), endResults);
        List<QueryResult> secondPreview = exploreService.previewResults(handle);
        Assert.assertEquals(firstPreview, secondPreview);
        Assert.assertEquals(ImmutableList.<QueryResult>of(), exploreService.nextResults(handle, 100));
        try {
            // All results are fetched, query should be inactive now
            exploreService.previewResults(handle);
            Assert.fail("HandleNotFoundException expected - query should be inactive.");
        } catch (HandleNotFoundException e) {
            Assert.assertTrue(e.isInactive());
        // Expected exception
        }
        // now test preview on a query that doesn't return any results
        handle = exploreService.execute(NAMESPACE_ID, "select * from " + getDatasetHiveName(myTable2));
        status = waitForCompletionStatus(handle, 200, TimeUnit.MILLISECONDS, 50);
        Assert.assertEquals(QueryStatus.OpStatus.FINISHED, status.getStatus());
        Assert.assertTrue(exploreService.previewResults(handle).isEmpty());
        // calling preview again should return the same thing. it should not throw an exception
        Assert.assertTrue(exploreService.previewResults(handle).isEmpty());
    } finally {
        datasetFramework.deleteInstance(myTable2);
        datasetFramework.deleteInstance(myTable3);
        datasetFramework.deleteInstance(myTable4);
        datasetFramework.deleteInstance(myTable5);
        datasetFramework.deleteInstance(myTable6);
    }
}
Also used : QueryResult(co.cask.cdap.proto.QueryResult) QueryHandle(co.cask.cdap.proto.QueryHandle) QueryStatus(co.cask.cdap.proto.QueryStatus) DatasetId(co.cask.cdap.proto.id.DatasetId) Test(org.junit.Test)

Example 15 with QueryStatus

use of co.cask.cdap.proto.QueryStatus in project cdap by caskdata.

the class ExploreQueryExecutorHttpHandler method getQueryStatus.

@GET
@Path("data/explore/queries/{id}/status")
public void getQueryStatus(HttpRequest request, HttpResponder responder, @PathParam("id") String id) throws ExploreException {
    try {
        final QueryHandle handle = QueryHandle.fromId(id);
        QueryStatus status;
        if (!handle.equals(QueryHandle.NO_OP)) {
            status = doAs(handle, new Callable<QueryStatus>() {

                @Override
                public QueryStatus call() throws Exception {
                    return exploreService.getStatus(handle);
                }
            });
        } else {
            status = QueryStatus.NO_OP;
        }
        responder.sendJson(HttpResponseStatus.OK, GSON.toJson(status));
    } catch (IllegalArgumentException e) {
        LOG.debug("Got exception:", e);
        responder.sendString(HttpResponseStatus.BAD_REQUEST, e.getMessage());
    } catch (SQLException e) {
        LOG.debug("Got exception:", e);
        responder.sendString(HttpResponseStatus.BAD_REQUEST, String.format("[SQLState %s] %s", e.getSQLState(), e.getMessage()));
    } catch (HandleNotFoundException e) {
        responder.sendStatus(HttpResponseStatus.NOT_FOUND);
    } catch (ExploreException | RuntimeException e) {
        LOG.debug("Got exception:", e);
        throw e;
    }
}
Also used : HandleNotFoundException(co.cask.cdap.explore.service.HandleNotFoundException) SQLException(java.sql.SQLException) QueryHandle(co.cask.cdap.proto.QueryHandle) QueryStatus(co.cask.cdap.proto.QueryStatus) Callable(java.util.concurrent.Callable) ExploreException(co.cask.cdap.explore.service.ExploreException) Path(javax.ws.rs.Path) GET(javax.ws.rs.GET)

Aggregations

QueryStatus (co.cask.cdap.proto.QueryStatus)18 QueryHandle (co.cask.cdap.proto.QueryHandle)8 HiveSQLException (org.apache.hive.service.cli.HiveSQLException)7 ExploreException (co.cask.cdap.explore.service.ExploreException)4 HandleNotFoundException (co.cask.cdap.explore.service.HandleNotFoundException)3 ColumnDesc (co.cask.cdap.proto.ColumnDesc)3 OperationStatus (org.apache.hive.service.cli.OperationStatus)3 Test (org.junit.Test)3 QueryResult (co.cask.cdap.proto.QueryResult)2 InvocationTargetException (java.lang.reflect.InvocationTargetException)2 SQLException (java.sql.SQLException)2 ServiceUnavailableException (co.cask.cdap.common.ServiceUnavailableException)1 UnauthenticatedException (co.cask.cdap.common.UnauthenticatedException)1 ExploreExecutionResult (co.cask.cdap.explore.client.ExploreExecutionResult)1 QueryInfo (co.cask.cdap.proto.QueryInfo)1 DatasetId (co.cask.cdap.proto.id.DatasetId)1 NamespaceId (co.cask.cdap.proto.id.NamespaceId)1 IOException (java.io.IOException)1 Method (java.lang.reflect.Method)1 ArrayList (java.util.ArrayList)1