use of io.cdap.cdap.proto.QueryHandle 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);
}
}
use of io.cdap.cdap.proto.QueryHandle 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);
}
}
use of io.cdap.cdap.proto.QueryHandle in project cdap by caskdata.
the class ExploreExecutorHttpHandler method doPartitionOperation.
private void doPartitionOperation(FullHttpRequest request, HttpResponder responder, DatasetId datasetId, PartitionOperation partitionOperation) {
try (SystemDatasetInstantiator datasetInstantiator = datasetInstantiatorFactory.create()) {
Dataset dataset;
try {
dataset = datasetInstantiator.getDataset(datasetId);
} catch (Exception e) {
LOG.error("Exception instantiating dataset {}.", datasetId, e);
responder.sendString(HttpResponseStatus.INTERNAL_SERVER_ERROR, "Exception instantiating dataset " + datasetId);
return;
}
try {
if (!(dataset instanceof PartitionedFileSet)) {
responder.sendString(HttpResponseStatus.BAD_REQUEST, "not a partitioned dataset.");
return;
}
Partitioning partitioning = ((PartitionedFileSet) dataset).getPartitioning();
Reader reader = new InputStreamReader(new ByteBufInputStream(request.content()));
Map<String, String> properties = GSON.fromJson(reader, new TypeToken<Map<String, String>>() {
}.getType());
PartitionKey partitionKey;
try {
partitionKey = PartitionedFileSetArguments.getOutputPartitionKey(properties, partitioning);
} catch (Exception e) {
responder.sendString(HttpResponseStatus.BAD_REQUEST, "invalid partition key: " + e.getMessage());
return;
}
if (partitionKey == null) {
responder.sendString(HttpResponseStatus.BAD_REQUEST, "no partition key was given.");
return;
}
QueryHandle handle = partitionOperation.submitOperation(partitionKey, properties);
if (handle == null) {
return;
}
JsonObject json = new JsonObject();
json.addProperty("handle", handle.getHandle());
responder.sendJson(HttpResponseStatus.OK, json.toString());
} finally {
Closeables.closeQuietly(dataset);
}
} 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 caskdata.
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 caskdata.
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()));
}
}
Aggregations