Search in sources :

Example 11 with HandleNotFoundException

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

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 12 with HandleNotFoundException

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

Example 13 with HandleNotFoundException

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

the class ExploreStatement method execute.

/**
 * Executes a query and wait until it is finished, but does not close the session.
 */
@Override
public boolean execute(String sql) throws SQLException {
    if (isClosed) {
        throw new SQLException("Can't execute after statement has been closed");
    }
    if (resultSet != null) {
        // As requested by the Statement interface javadoc, "All execution methods in the Statement interface
        // implicitly close a statement's current ResultSet object if an open one exists"
        resultSet.close();
        resultSet = null;
    }
    futureResults = exploreClient.submit(namespace, sql);
    try {
        resultSet = new ExploreResultSet(futureResults.get(), this, maxRows);
        // Here we have a result, it may contain rows or may be empty, but it exists.
        return true;
    } catch (InterruptedException e) {
        LOG.error("Caught exception", e);
        Thread.currentThread().interrupt();
        return false;
    } catch (ExecutionException e) {
        Throwable t = Throwables.getRootCause(e);
        if (t instanceof HandleNotFoundException) {
            LOG.error("Error executing query", e);
            throw new SQLException("Unknown state");
        }
        LOG.error("Caught exception", e);
        throw new SQLException(Throwables.getRootCause(e));
    } catch (CancellationException e) {
        // If futureResults has been cancelled
        return false;
    }
}
Also used : HandleNotFoundException(io.cdap.cdap.explore.service.HandleNotFoundException) SQLException(java.sql.SQLException) CancellationException(java.util.concurrent.CancellationException) ExecutionException(java.util.concurrent.ExecutionException)

Example 14 with HandleNotFoundException

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

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 15 with HandleNotFoundException

use of io.cdap.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(io.cdap.cdap.cli.util.table.Table) SQLException(java.sql.SQLException) RowMaker(io.cdap.cdap.cli.util.RowMaker) ColumnDesc(io.cdap.cdap.proto.ColumnDesc) QueryStatus(io.cdap.cdap.proto.QueryStatus) HandleNotFoundException(io.cdap.cdap.explore.service.HandleNotFoundException) QueryResult(io.cdap.cdap.proto.QueryResult) CancellationException(java.util.concurrent.CancellationException) ExecutionException(java.util.concurrent.ExecutionException) ExploreExecutionResult(io.cdap.cdap.explore.client.ExploreExecutionResult) TimeoutException(java.util.concurrent.TimeoutException)

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