Search in sources :

Example 6 with HandleNotFoundException

use of io.cdap.cdap.explore.service.HandleNotFoundException in project cdap by caskdata.

the class ExploreQueryExecutorHttpHandler method getQueryResultPreview.

@POST
@Path("data/explore/queries/{id}/preview")
public void getQueryResultPreview(HttpRequest request, HttpResponder responder, @PathParam("id") String id) throws 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 {
            results = doAs(handle, new Callable<List<QueryResult>>() {

                @Override
                public List<QueryResult> call() throws Exception {
                    return exploreService.previewResults(handle);
                }
            });
        }
        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) {
        if (e.isInactive()) {
            responder.sendString(HttpResponseStatus.CONFLICT, "Preview is unavailable for inactive queries.");
            return;
        }
        responder.sendStatus(HttpResponseStatus.NOT_FOUND);
    }
}
Also used : HandleNotFoundException(io.cdap.cdap.explore.service.HandleNotFoundException) QueryResult(io.cdap.cdap.proto.QueryResult) SQLException(java.sql.SQLException) QueryHandle(io.cdap.cdap.proto.QueryHandle) Callable(java.util.concurrent.Callable) Path(javax.ws.rs.Path) POST(javax.ws.rs.POST)

Example 7 with HandleNotFoundException

use of io.cdap.cdap.explore.service.HandleNotFoundException in project cdap by cdapio.

the class BaseHiveExploreService method fetchNextResults.

@SuppressWarnings("unchecked")
private List<QueryResult> fetchNextResults(QueryHandle handle, int size) throws HiveSQLException, ExploreException, HandleNotFoundException {
    startAndWait();
    Lock nextLock = getActiveOperationInfo(handle).getNextLock();
    nextLock.lock();
    try {
        // Fetch results from Hive
        LOG.trace("Getting results for handle {}", handle);
        OperationHandle operationHandle = getOperationHandle(handle);
        if (operationHandle.hasResultSet()) {
            return doFetchNextResults(operationHandle, FetchOrientation.FETCH_NEXT, size);
        } else {
            return Collections.emptyList();
        }
    } catch (Exception e) {
        throw Throwables.propagate(e);
    } finally {
        nextLock.unlock();
    }
}
Also used : OperationHandle(org.apache.hive.service.cli.OperationHandle) NamespaceNotFoundException(io.cdap.cdap.common.NamespaceNotFoundException) SQLException(java.sql.SQLException) TableNotFoundException(io.cdap.cdap.explore.service.TableNotFoundException) TException(org.apache.thrift.TException) IOException(java.io.IOException) HandleNotFoundException(io.cdap.cdap.explore.service.HandleNotFoundException) NoSuchObjectException(org.apache.hadoop.hive.metastore.api.NoSuchObjectException) TransactionFailureException(org.apache.tephra.TransactionFailureException) MetaException(org.apache.hadoop.hive.metastore.api.MetaException) ExploreException(io.cdap.cdap.explore.service.ExploreException) FileNotFoundException(java.io.FileNotFoundException) HiveSQLException(org.apache.hive.service.cli.HiveSQLException) Lock(java.util.concurrent.locks.Lock)

Example 8 with HandleNotFoundException

use of io.cdap.cdap.explore.service.HandleNotFoundException in project cdap by cdapio.

the class BaseHiveExploreService method previewResults.

@Override
public List<QueryResult> previewResults(QueryHandle handle) throws ExploreException, HandleNotFoundException, SQLException {
    startAndWait();
    if (inactiveHandleCache.getIfPresent(handle) != null) {
        throw new HandleNotFoundException("Query is inactive.", true);
    }
    OperationInfo operationInfo = getActiveOperationInfo(handle);
    Lock previewLock = operationInfo.getPreviewLock();
    previewLock.lock();
    try {
        File previewFile = operationInfo.getPreviewFile();
        if (previewFile != null) {
            try {
                Reader reader = com.google.common.io.Files.newReader(previewFile, Charsets.UTF_8);
                try {
                    return GSON.fromJson(reader, new TypeToken<List<QueryResult>>() {
                    }.getType());
                } finally {
                    Closeables.closeQuietly(reader);
                }
            } catch (FileNotFoundException e) {
                LOG.error("Could not retrieve preview result file {}", previewFile, e);
                throw new ExploreException(e);
            }
        }
        try {
            // Create preview results for query
            previewFile = new File(previewsDir, handle.getHandle());
            try (FileWriter fileWriter = new FileWriter(previewFile)) {
                List<QueryResult> results = fetchNextResults(handle, PREVIEW_COUNT);
                GSON.toJson(results, fileWriter);
                operationInfo.setPreviewFile(previewFile);
                return results;
            }
        } catch (IOException e) {
            LOG.error("Could not write preview results into file", e);
            throw new ExploreException(e);
        }
    } finally {
        previewLock.unlock();
    }
}
Also used : HandleNotFoundException(io.cdap.cdap.explore.service.HandleNotFoundException) QueryResult(io.cdap.cdap.proto.QueryResult) TypeToken(com.google.common.reflect.TypeToken) FileWriter(java.io.FileWriter) FileNotFoundException(java.io.FileNotFoundException) Reader(java.io.Reader) IOException(java.io.IOException) File(java.io.File) Lock(java.util.concurrent.locks.Lock) ExploreException(io.cdap.cdap.explore.service.ExploreException)

Example 9 with HandleNotFoundException

use of io.cdap.cdap.explore.service.HandleNotFoundException 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 10 with HandleNotFoundException

use of io.cdap.cdap.explore.service.HandleNotFoundException 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)

Aggregations

HandleNotFoundException (io.cdap.cdap.explore.service.HandleNotFoundException)22 SQLException (java.sql.SQLException)18 QueryHandle (io.cdap.cdap.proto.QueryHandle)14 Path (javax.ws.rs.Path)12 ExploreException (io.cdap.cdap.explore.service.ExploreException)10 QueryResult (io.cdap.cdap.proto.QueryResult)8 IOException (java.io.IOException)8 Callable (java.util.concurrent.Callable)8 QueryStatus (io.cdap.cdap.proto.QueryStatus)6 POST (javax.ws.rs.POST)6 ColumnDesc (io.cdap.cdap.proto.ColumnDesc)4 FileNotFoundException (java.io.FileNotFoundException)4 CancellationException (java.util.concurrent.CancellationException)4 ExecutionException (java.util.concurrent.ExecutionException)4 Lock (java.util.concurrent.locks.Lock)4 GET (javax.ws.rs.GET)4 TypeToken (com.google.common.reflect.TypeToken)2 RowMaker (io.cdap.cdap.cli.util.RowMaker)2 Table (io.cdap.cdap.cli.util.table.Table)2 NamespaceNotFoundException (io.cdap.cdap.common.NamespaceNotFoundException)2