Search in sources :

Example 6 with QueryStatus

use of io.cdap.cdap.proto.QueryStatus in project cdap by caskdata.

the class BaseHiveExploreService method getQueries.

@Override
public List<QueryInfo> getQueries(NamespaceId namespace) throws ExploreException, SQLException {
    startAndWait();
    List<QueryInfo> result = new ArrayList<>();
    String namespaceHiveDb = getHiveDatabase(namespace.getNamespace());
    for (Map.Entry<QueryHandle, OperationInfo> entry : activeHandleCache.asMap().entrySet()) {
        try {
            if (namespaceHiveDb.equals(entry.getValue().getHiveDatabase())) {
                // we use empty query statement for get tables, get schemas, we don't need to return it this method call.
                if (!entry.getValue().getStatement().isEmpty()) {
                    QueryStatus status = getStatus(entry.getKey());
                    result.add(new QueryInfo(entry.getValue().getTimestamp(), entry.getValue().getStatement(), entry.getKey(), status, true));
                }
            }
        } catch (HandleNotFoundException e) {
        // ignore the handle not found exception. this method returns all queries and handle, if the
        // handle is removed from the internal cache, then there is no point returning them from here.
        }
    }
    for (Map.Entry<QueryHandle, InactiveOperationInfo> entry : inactiveHandleCache.asMap().entrySet()) {
        InactiveOperationInfo inactiveOperationInfo = entry.getValue();
        if (namespaceHiveDb.equals(inactiveOperationInfo.getHiveDatabase())) {
            // we use empty query statement for get tables, get schemas, we don't need to return it this method call.
            if (!inactiveOperationInfo.getStatement().isEmpty()) {
                if (inactiveOperationInfo.getStatus() == null) {
                    LOG.error("Null status for query {}, handle {}", inactiveOperationInfo.getStatement(), entry.getKey());
                }
                result.add(new QueryInfo(inactiveOperationInfo.getTimestamp(), inactiveOperationInfo.getStatement(), entry.getKey(), inactiveOperationInfo.getStatus(), false));
            }
        }
    }
    Collections.sort(result);
    return result;
}
Also used : HandleNotFoundException(io.cdap.cdap.explore.service.HandleNotFoundException) ArrayList(java.util.ArrayList) QueryInfo(io.cdap.cdap.proto.QueryInfo) QueryHandle(io.cdap.cdap.proto.QueryHandle) Map(java.util.Map) HashMap(java.util.HashMap) QueryStatus(io.cdap.cdap.proto.QueryStatus)

Example 7 with QueryStatus

use of io.cdap.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(io.cdap.cdap.proto.QueryHandle) OperationHandle(org.apache.hive.service.cli.OperationHandle) QueryStatus(io.cdap.cdap.proto.QueryStatus) ExploreException(io.cdap.cdap.explore.service.ExploreException)

Example 8 with QueryStatus

use of io.cdap.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(io.cdap.cdap.explore.service.HandleNotFoundException) SQLException(java.sql.SQLException) QueryHandle(io.cdap.cdap.proto.QueryHandle) QueryStatus(io.cdap.cdap.proto.QueryStatus) Callable(java.util.concurrent.Callable) ExploreException(io.cdap.cdap.explore.service.ExploreException) Path(javax.ws.rs.Path) GET(javax.ws.rs.GET)

Example 9 with QueryStatus

use of io.cdap.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(io.cdap.cdap.proto.QueryStatus) OperationState(org.apache.hive.service.cli.OperationState) InvocationTargetException(java.lang.reflect.InvocationTargetException)

Example 10 with QueryStatus

use of io.cdap.cdap.proto.QueryStatus in project cdap by caskdata.

the class Hive14ExploreService method doFetchStatus.

@Override
protected QueryStatus doFetchStatus(OperationHandle operationHandle) throws HiveSQLException, ExploreException, HandleNotFoundException {
    OperationStatus operationStatus;
    CLIService cliService = getCliService();
    // Call the getOperationStatus method based on the number of arguments it expects.
    try {
        if (getOperationStatus.getParameterTypes().length == 2) {
            operationStatus = (OperationStatus) getOperationStatus.invoke(cliService, operationHandle, true);
        } else {
            operationStatus = (OperationStatus) getOperationStatus.invoke(cliService, operationHandle);
        }
    } catch (IndexOutOfBoundsException | IllegalAccessException | InvocationTargetException e) {
        throw new RuntimeException("Failed to get the status of the operation.", e);
    }
    @SuppressWarnings("ThrowableResultOfMethodCallIgnored") HiveSQLException hiveExn = operationStatus.getOperationException();
    if (hiveExn != null) {
        return new QueryStatus(hiveExn.getMessage(), hiveExn.getSQLState());
    }
    return new QueryStatus(QueryStatus.OpStatus.valueOf(operationStatus.getState().toString()), operationHandle.hasResultSet());
}
Also used : OperationStatus(org.apache.hive.service.cli.OperationStatus) HiveSQLException(org.apache.hive.service.cli.HiveSQLException) QueryStatus(io.cdap.cdap.proto.QueryStatus) CLIService(org.apache.hive.service.cli.CLIService) InvocationTargetException(java.lang.reflect.InvocationTargetException)

Aggregations

QueryStatus (io.cdap.cdap.proto.QueryStatus)36 QueryHandle (io.cdap.cdap.proto.QueryHandle)16 HiveSQLException (org.apache.hive.service.cli.HiveSQLException)14 ExploreException (io.cdap.cdap.explore.service.ExploreException)8 HandleNotFoundException (io.cdap.cdap.explore.service.HandleNotFoundException)6 ColumnDesc (io.cdap.cdap.proto.ColumnDesc)6 OperationStatus (org.apache.hive.service.cli.OperationStatus)6 Test (org.junit.Test)6 QueryResult (io.cdap.cdap.proto.QueryResult)4 InvocationTargetException (java.lang.reflect.InvocationTargetException)4 SQLException (java.sql.SQLException)4 ServiceUnavailableException (io.cdap.cdap.common.ServiceUnavailableException)2 ExploreExecutionResult (io.cdap.cdap.explore.client.ExploreExecutionResult)2 QueryInfo (io.cdap.cdap.proto.QueryInfo)2 DatasetId (io.cdap.cdap.proto.id.DatasetId)2 NamespaceId (io.cdap.cdap.proto.id.NamespaceId)2 UnauthenticatedException (io.cdap.cdap.security.spi.authentication.UnauthenticatedException)2 IOException (java.io.IOException)2 Method (java.lang.reflect.Method)2 ArrayList (java.util.ArrayList)2