Search in sources :

Example 6 with HandleNotFoundException

use of co.cask.cdap.explore.service.HandleNotFoundException in project cdap by caskdata.

the class ExploreQueryExecutorHttpHandler method getQueryNextResults.

@POST
@Path("data/explore/queries/{id}/next")
public void getQueryNextResults(FullHttpRequest request, HttpResponder responder, @PathParam("id") String id) throws IOException, ExploreException {
    // NOTE: this call is a POST because it is not idempotent: cursor of results is moved
    try {
        final QueryHandle handle = QueryHandle.fromId(id);
        List<QueryResult> results;
        if (handle.equals(QueryHandle.NO_OP)) {
            results = Lists.newArrayList();
        } else {
            Map<String, String> args = decodeArguments(request);
            final int size = args.containsKey("size") ? Integer.valueOf(args.get("size")) : 100;
            results = doAs(handle, new Callable<List<QueryResult>>() {

                @Override
                public List<QueryResult> call() throws Exception {
                    return exploreService.nextResults(handle, size);
                }
            });
        }
        responder.sendJson(HttpResponseStatus.OK, GSON.toJson(results));
    } 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(co.cask.cdap.explore.service.HandleNotFoundException) QueryResult(co.cask.cdap.proto.QueryResult) SQLException(java.sql.SQLException) QueryHandle(co.cask.cdap.proto.QueryHandle) Callable(java.util.concurrent.Callable) Path(javax.ws.rs.Path) POST(javax.ws.rs.POST)

Example 7 with HandleNotFoundException

use of co.cask.cdap.explore.service.HandleNotFoundException in project cdap by caskdata.

the class ExploreQueryExecutorHttpHandler method downloadQueryResults.

@POST
@Path("data/explore/queries/{id}/download")
public void downloadQueryResults(HttpRequest request, final HttpResponder responder, @PathParam("id") final String id) throws ExploreException, IOException, SQLException, HandleNotFoundException {
    // NOTE: this call is a POST because it is not idempotent: cursor of results is moved
    final QueryHandle handle = QueryHandle.fromId(id);
    doAs(handle, new Callable<Void>() {

        @Override
        public Void call() throws Exception {
            doDownloadQueryResults(responder, handle);
            return null;
        }
    });
}
Also used : QueryHandle(co.cask.cdap.proto.QueryHandle) HandleNotFoundException(co.cask.cdap.explore.service.HandleNotFoundException) SQLException(java.sql.SQLException) IOException(java.io.IOException) ExploreException(co.cask.cdap.explore.service.ExploreException) Path(javax.ws.rs.Path) POST(javax.ws.rs.POST)

Example 8 with HandleNotFoundException

use of co.cask.cdap.explore.service.HandleNotFoundException in project cdap by caskdata.

the class ExecuteQueryCommand method perform.

@Override
public void perform(Arguments arguments, PrintStream output) throws Exception {
    String query = arguments.get(ArgumentName.QUERY.toString());
    long timeOutMins = arguments.getLongOptional(ArgumentName.TIMEOUT.toString(), DEFAULT_TIMEOUT_MIN);
    ListenableFuture<ExploreExecutionResult> future = queryClient.execute(cliConfig.getCurrentNamespace(), query);
    try {
        ExploreExecutionResult executionResult = future.get(timeOutMins, TimeUnit.MINUTES);
        if (!executionResult.canContainResults()) {
            output.println("SQL statement does not output any result.");
            executionResult.close();
            return;
        }
        final List<ColumnDesc> schema = executionResult.getResultSchema();
        String[] header = new String[schema.size()];
        for (int i = 0; i < header.length; i++) {
            ColumnDesc column = schema.get(i);
            // Hive columns start at 1
            int index = column.getPosition() - 1;
            header[index] = column.getName() + ": " + column.getType();
        }
        List<QueryResult> rows = Lists.newArrayList(executionResult);
        executionResult.close();
        QueryStatus.OpStatus opStatus = executionResult.getStatus().getStatus();
        if (opStatus != QueryStatus.OpStatus.FINISHED) {
            throw new SQLException(String.format("Query '%s' execution did not finish successfully. " + "Got final state - %s", query, opStatus));
        }
        Table table = Table.builder().setHeader(header).setRows(rows, new RowMaker<QueryResult>() {

            @Override
            public List<?> makeRow(QueryResult object) {
                return object.getColumns();
            }
        }).build();
        cliConfig.getTableRenderer().render(cliConfig, output, table);
        output.printf("Fetched %d rows", rows.size()).println();
    } catch (InterruptedException e) {
        Thread.currentThread().interrupt();
    } catch (ExecutionException e) {
        Throwable t = Throwables.getRootCause(e);
        if (t instanceof HandleNotFoundException) {
            throw Throwables.propagate(t);
        }
        throw new SQLException(Throwables.getRootCause(e));
    } catch (CancellationException e) {
        throw new RuntimeException("Query has been cancelled on ListenableFuture object.");
    } catch (TimeoutException e) {
        output.println("Couldn't obtain results after " + timeOutMins + "mins.");
    }
}
Also used : Table(co.cask.cdap.cli.util.table.Table) SQLException(java.sql.SQLException) RowMaker(co.cask.cdap.cli.util.RowMaker) ColumnDesc(co.cask.cdap.proto.ColumnDesc) QueryStatus(co.cask.cdap.proto.QueryStatus) HandleNotFoundException(co.cask.cdap.explore.service.HandleNotFoundException) QueryResult(co.cask.cdap.proto.QueryResult) CancellationException(java.util.concurrent.CancellationException) ExecutionException(java.util.concurrent.ExecutionException) ExploreExecutionResult(co.cask.cdap.explore.client.ExploreExecutionResult) TimeoutException(java.util.concurrent.TimeoutException)

Example 9 with HandleNotFoundException

use of co.cask.cdap.explore.service.HandleNotFoundException in project cdap by caskdata.

the class ExploreQueryExecutorHttpHandler method getQueryNextResults.

@POST
@Path("data/explore/queries/{id}/next")
public void getQueryNextResults(HttpRequest request, HttpResponder responder, @PathParam("id") String id) throws IOException, ExploreException {
    // NOTE: this call is a POST because it is not idempotent: cursor of results is moved
    try {
        final QueryHandle handle = QueryHandle.fromId(id);
        List<QueryResult> results;
        if (handle.equals(QueryHandle.NO_OP)) {
            results = Lists.newArrayList();
        } else {
            Map<String, String> args = decodeArguments(request);
            final int size = args.containsKey("size") ? Integer.valueOf(args.get("size")) : 100;
            results = doAs(handle, new Callable<List<QueryResult>>() {

                @Override
                public List<QueryResult> call() throws Exception {
                    return exploreService.nextResults(handle, size);
                }
            });
        }
        responder.sendJson(HttpResponseStatus.OK, results);
    } 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(co.cask.cdap.explore.service.HandleNotFoundException) QueryResult(co.cask.cdap.proto.QueryResult) SQLException(java.sql.SQLException) QueryHandle(co.cask.cdap.proto.QueryHandle) Callable(java.util.concurrent.Callable) Path(javax.ws.rs.Path) POST(javax.ws.rs.POST)

Example 10 with HandleNotFoundException

use of co.cask.cdap.explore.service.HandleNotFoundException in project cdap by caskdata.

the class ExploreQueryExecutorHttpHandler method closeQuery.

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

                @Override
                public Void call() throws Exception {
                    exploreService.close(handle);
                    return null;
                }
            });
        }
        responder.sendStatus(HttpResponseStatus.OK);
    } catch (IllegalArgumentException e) {
        LOG.debug("Got exception:", e);
        responder.sendString(HttpResponseStatus.BAD_REQUEST, e.getMessage());
    } catch (HandleNotFoundException e) {
        responder.sendStatus(HttpResponseStatus.NOT_FOUND);
    }
}
Also used : HandleNotFoundException(co.cask.cdap.explore.service.HandleNotFoundException) QueryHandle(co.cask.cdap.proto.QueryHandle) HandleNotFoundException(co.cask.cdap.explore.service.HandleNotFoundException) SQLException(java.sql.SQLException) IOException(java.io.IOException) ExploreException(co.cask.cdap.explore.service.ExploreException) Path(javax.ws.rs.Path) DELETE(javax.ws.rs.DELETE)

Aggregations

HandleNotFoundException (co.cask.cdap.explore.service.HandleNotFoundException)12 SQLException (java.sql.SQLException)10 QueryHandle (co.cask.cdap.proto.QueryHandle)8 Path (javax.ws.rs.Path)7 ExploreException (co.cask.cdap.explore.service.ExploreException)5 QueryResult (co.cask.cdap.proto.QueryResult)5 Callable (java.util.concurrent.Callable)5 IOException (java.io.IOException)4 POST (javax.ws.rs.POST)4 QueryStatus (co.cask.cdap.proto.QueryStatus)3 ColumnDesc (co.cask.cdap.proto.ColumnDesc)2 FileNotFoundException (java.io.FileNotFoundException)2 CancellationException (java.util.concurrent.CancellationException)2 ExecutionException (java.util.concurrent.ExecutionException)2 Lock (java.util.concurrent.locks.Lock)2 GET (javax.ws.rs.GET)2 RowMaker (co.cask.cdap.cli.util.RowMaker)1 Table (co.cask.cdap.cli.util.table.Table)1 NamespaceNotFoundException (co.cask.cdap.common.NamespaceNotFoundException)1 ExploreExecutionResult (co.cask.cdap.explore.client.ExploreExecutionResult)1