Search in sources :

Example 41 with QueryHandle

use of co.cask.cdap.proto.QueryHandle in project cdap by caskdata.

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(co.cask.cdap.proto.QueryHandle) QueryStatus(co.cask.cdap.proto.QueryStatus) HandleNotFoundException(co.cask.cdap.explore.service.HandleNotFoundException) SQLException(java.sql.SQLException) ServiceUnavailableException(co.cask.cdap.common.ServiceUnavailableException) IOException(java.io.IOException) UnauthenticatedException(co.cask.cdap.common.UnauthenticatedException) ExploreException(co.cask.cdap.explore.service.ExploreException)

Aggregations

QueryHandle (co.cask.cdap.proto.QueryHandle)41 ExploreException (co.cask.cdap.explore.service.ExploreException)27 SQLException (java.sql.SQLException)21 Path (javax.ws.rs.Path)14 IOException (java.io.IOException)12 POST (javax.ws.rs.POST)11 HiveSQLException (org.apache.hive.service.cli.HiveSQLException)11 OperationHandle (org.apache.hive.service.cli.OperationHandle)11 SessionHandle (org.apache.hive.service.cli.SessionHandle)11 UnsupportedTypeException (co.cask.cdap.api.data.schema.UnsupportedTypeException)10 HandleNotFoundException (co.cask.cdap.explore.service.HandleNotFoundException)10 JsonObject (com.google.gson.JsonObject)10 DatasetManagementException (co.cask.cdap.api.dataset.DatasetManagementException)8 BadRequestException (co.cask.cdap.common.BadRequestException)8 QueryStatus (co.cask.cdap.proto.QueryStatus)8 JsonSyntaxException (com.google.gson.JsonSyntaxException)8 Callable (java.util.concurrent.Callable)7 AuditPolicy (co.cask.cdap.common.security.AuditPolicy)6 InputStreamReader (java.io.InputStreamReader)5 Reader (java.io.Reader)5