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());
}
}
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);
}
}
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;
}
}
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()));
}
}
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;
}
Aggregations