Search in sources :

Example 31 with QueryHandle

use of io.cdap.cdap.proto.QueryHandle in project cdap by cdapio.

the class ExploreExecutorHttpHandler method enableDataset.

private void enableDataset(HttpResponder responder, final DatasetId datasetId, final DatasetSpecification datasetSpec, final boolean truncating) {
    LOG.debug("Enabling explore for dataset instance {}", datasetId);
    try {
        QueryHandle handle = impersonator.doAs(datasetId, new Callable<QueryHandle>() {

            @Override
            public QueryHandle call() throws Exception {
                return exploreTableManager.enableDataset(datasetId, datasetSpec, truncating);
            }
        });
        JsonObject json = new JsonObject();
        json.addProperty("handle", handle.getHandle());
        responder.sendJson(HttpResponseStatus.OK, json.toString());
    } catch (IllegalArgumentException e) {
        responder.sendString(HttpResponseStatus.BAD_REQUEST, e.getMessage());
    } catch (ExploreException e) {
        responder.sendString(HttpResponseStatus.INTERNAL_SERVER_ERROR, "Error enabling explore on dataset " + datasetId);
    } catch (SQLException e) {
        responder.sendString(HttpResponseStatus.BAD_REQUEST, "SQL exception while trying to enable explore on dataset " + datasetId);
    } catch (UnsupportedTypeException e) {
        responder.sendString(HttpResponseStatus.BAD_REQUEST, "Schema for dataset " + datasetId + " is not supported for exploration: " + e.getMessage());
    } catch (Throwable e) {
        LOG.error("Got exception:", e);
        responder.sendString(HttpResponseStatus.INTERNAL_SERVER_ERROR, e.getMessage());
    }
}
Also used : SQLException(java.sql.SQLException) JsonObject(com.google.gson.JsonObject) UnsupportedTypeException(io.cdap.cdap.api.data.schema.UnsupportedTypeException) QueryHandle(io.cdap.cdap.proto.QueryHandle) ExploreException(io.cdap.cdap.explore.service.ExploreException) UnsupportedTypeException(io.cdap.cdap.api.data.schema.UnsupportedTypeException) UnauthorizedException(io.cdap.cdap.security.spi.authorization.UnauthorizedException) SQLException(java.sql.SQLException) JsonSyntaxException(com.google.gson.JsonSyntaxException) DatasetManagementException(io.cdap.cdap.api.dataset.DatasetManagementException) IOException(java.io.IOException) BadRequestException(io.cdap.cdap.common.BadRequestException) ExploreException(io.cdap.cdap.explore.service.ExploreException)

Example 32 with QueryHandle

use of io.cdap.cdap.proto.QueryHandle in project cdap by cdapio.

the class ExploreQueryExecutorHttpHandler method getQueryResultsSchema.

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

                @Override
                public List<ColumnDesc> call() throws Exception {
                    return exploreService.getResultSchema(handle);
                }
            });
        } else {
            schema = Lists.newArrayList();
        }
        responder.sendJson(HttpResponseStatus.OK, GSON.toJson(schema));
    } 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);
    }
}
Also used : HandleNotFoundException(io.cdap.cdap.explore.service.HandleNotFoundException) SQLException(java.sql.SQLException) QueryHandle(io.cdap.cdap.proto.QueryHandle) ColumnDesc(io.cdap.cdap.proto.ColumnDesc) Callable(java.util.concurrent.Callable) Path(javax.ws.rs.Path) GET(javax.ws.rs.GET)

Example 33 with QueryHandle

use of io.cdap.cdap.proto.QueryHandle in project cdap by cdapio.

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 34 with QueryHandle

use of io.cdap.cdap.proto.QueryHandle in project cdap by cdapio.

the class NamespacedExploreQueryExecutorHttpHandler method query.

@POST
@Path("data/explore/queries")
@AuditPolicy(AuditDetail.REQUEST_BODY)
public void query(FullHttpRequest request, HttpResponder responder, @PathParam("namespace-id") final String namespaceId) throws Exception {
    try {
        Map<String, String> args = decodeArguments(request);
        final String query = args.get("query");
        final Map<String, String> additionalSessionConf = new HashMap<>(args);
        additionalSessionConf.remove("query");
        LOG.trace("Received query: {}", query);
        QueryHandle queryHandle = impersonator.doAs(new NamespaceId(namespaceId), new Callable<QueryHandle>() {

            @Override
            public QueryHandle call() throws Exception {
                return exploreService.execute(new NamespaceId(namespaceId), query, additionalSessionConf);
            }
        }, ImpersonatedOpType.EXPLORE);
        responder.sendJson(HttpResponseStatus.OK, GSON.toJson(queryHandle));
    } 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()));
    }
}
Also used : HashMap(java.util.HashMap) SQLException(java.sql.SQLException) NamespaceId(io.cdap.cdap.proto.id.NamespaceId) QueryHandle(io.cdap.cdap.proto.QueryHandle) ExploreException(io.cdap.cdap.explore.service.ExploreException) SQLException(java.sql.SQLException) Path(javax.ws.rs.Path) AuditPolicy(io.cdap.cdap.common.security.AuditPolicy) POST(javax.ws.rs.POST)

Example 35 with QueryHandle

use of io.cdap.cdap.proto.QueryHandle in project cdap by cdapio.

the class AbstractExploreClient method getFutureResultsFromHandle.

/**
 * Create a {@link ListenableFuture} object by polling the Explore service using the
 * {@link ListenableFuture} containing a {@link QueryHandle}.
 */
private ListenableFuture<ExploreExecutionResult> getFutureResultsFromHandle(final ListenableFuture<QueryHandle> futureHandle) {
    final StatementExecutionFuture resultFuture = new StatementExecutionFuture(this, futureHandle);
    Futures.addCallback(futureHandle, new FutureCallback<QueryHandle>() {

        @Override
        public void onSuccess(final QueryHandle handle) {
            boolean mustCloseHandle;
            try {
                QueryStatus status = getStatus(handle);
                if (!status.getStatus().isDone()) {
                    final String userId = SecurityRequestContext.getUserId();
                    final String userIp = SecurityRequestContext.getUserIP();
                    executor.schedule(new Runnable() {

                        @Override
                        public void run() {
                            SecurityRequestContext.setUserId(userId);
                            SecurityRequestContext.setUserIP(userIp);
                            onSuccess(handle);
                        }
                    }, 300, TimeUnit.MILLISECONDS);
                    return;
                }
                if (QueryStatus.OpStatus.ERROR.equals(status.getStatus())) {
                    throw new SQLException(status.getErrorMessage(), status.getSqlState());
                }
                ExploreExecutionResult result = new ClientExploreExecutionResult(AbstractExploreClient.this, handle, status);
                mustCloseHandle = !resultFuture.set(result) || !status.hasResults();
            } catch (Exception e) {
                mustCloseHandle = true;
                resultFuture.setException(e);
            }
            if (mustCloseHandle) {
                try {
                    close(handle);
                } catch (Throwable t) {
                    LOG.warn("Failed to close handle {}", handle, t);
                }
            }
        }

        @Override
        public void onFailure(Throwable t) {
            resultFuture.setException(t);
        }
    }, executor);
    return resultFuture;
}
Also used : SQLException(java.sql.SQLException) QueryHandle(io.cdap.cdap.proto.QueryHandle) QueryStatus(io.cdap.cdap.proto.QueryStatus) ServiceUnavailableException(io.cdap.cdap.common.ServiceUnavailableException) ExploreException(io.cdap.cdap.explore.service.ExploreException) SQLException(java.sql.SQLException) IOException(java.io.IOException) UnauthenticatedException(io.cdap.cdap.security.spi.authentication.UnauthenticatedException) HandleNotFoundException(io.cdap.cdap.explore.service.HandleNotFoundException)

Aggregations

QueryHandle (io.cdap.cdap.proto.QueryHandle)70 ExploreException (io.cdap.cdap.explore.service.ExploreException)40 SQLException (java.sql.SQLException)26 HiveSQLException (org.apache.hive.service.cli.HiveSQLException)22 OperationHandle (org.apache.hive.service.cli.OperationHandle)22 SessionHandle (org.apache.hive.service.cli.SessionHandle)22 HandleNotFoundException (io.cdap.cdap.explore.service.HandleNotFoundException)18 QueryStatus (io.cdap.cdap.proto.QueryStatus)16 Path (javax.ws.rs.Path)16 IOException (java.io.IOException)14 Callable (java.util.concurrent.Callable)10 POST (javax.ws.rs.POST)10 Test (org.junit.Test)10 JsonObject (com.google.gson.JsonObject)8 UnsupportedTypeException (io.cdap.cdap.api.data.schema.UnsupportedTypeException)8 JsonSyntaxException (com.google.gson.JsonSyntaxException)6 DatasetManagementException (io.cdap.cdap.api.dataset.DatasetManagementException)6 BadRequestException (io.cdap.cdap.common.BadRequestException)6 ColumnDesc (io.cdap.cdap.proto.ColumnDesc)6 QueryResult (io.cdap.cdap.proto.QueryResult)6