Search in sources :

Example 61 with QueryHandle

use of io.cdap.cdap.proto.QueryHandle in project cdap by cdapio.

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 62 with QueryHandle

use of io.cdap.cdap.proto.QueryHandle in project cdap by cdapio.

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 63 with QueryHandle

use of io.cdap.cdap.proto.QueryHandle in project cdap by cdapio.

the class ExploreQueryExecutorHttpHandler method downloadQueryResults.

@POST
@Path("data/explore/queries/{id}/download")
public void downloadQueryResults(HttpRequest request, final HttpResponder responder, @PathParam("id") final String id) throws ExploreException, IOException, SQLException, HandleNotFoundException {
    // NOTE: this call is a POST because it is not idempotent: cursor of results is moved
    final QueryHandle handle = QueryHandle.fromId(id);
    doAs(handle, new Callable<Void>() {

        @Override
        public Void call() throws Exception {
            doDownloadQueryResults(responder, handle);
            return null;
        }
    });
}
Also used : 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) POST(javax.ws.rs.POST)

Example 64 with QueryHandle

use of io.cdap.cdap.proto.QueryHandle in project cdap by cdapio.

the class BaseHiveExploreServiceTest method deleteNamespace.

/**
 * Delete a namespace because app fabric is not started in explore tests.
 */
protected static void deleteNamespace(NamespaceId namespaceId) throws Exception {
    namespacePathLocator.get(namespaceId).delete(true);
    if (!NamespaceId.DEFAULT.equals(namespaceId)) {
        QueryHandle handle = exploreService.deleteNamespace(namespaceId);
        waitForCompletionStatus(handle, 50, TimeUnit.MILLISECONDS, 40);
    }
    namespaceAdmin.delete(namespaceId);
}
Also used : QueryHandle(io.cdap.cdap.proto.QueryHandle)

Example 65 with QueryHandle

use of io.cdap.cdap.proto.QueryHandle in project cdap by cdapio.

the class InMemoryExploreServiceTest method runCommand.

private static void runCommand(String namespace, String command, boolean expectedHasResult, List<ColumnDesc> expectedColumnDescs, List<QueryResult> expectedResults) throws Exception {
    QueryHandle handle = exploreService.execute(new NamespaceId(namespace), command);
    QueryStatus status = waitForCompletionStatus(handle);
    Assert.assertEquals(QueryStatus.OpStatus.FINISHED, status.getStatus());
    Assert.assertEquals(expectedHasResult, status.hasResults());
    Assert.assertEquals(expectedColumnDescs, exploreService.getResultSchema(handle));
    Assert.assertEquals(expectedResults.toString(), trimColumnValues(exploreService.nextResults(handle, 100)).toString());
    exploreService.close(handle);
}
Also used : NamespaceId(io.cdap.cdap.proto.id.NamespaceId) QueryHandle(io.cdap.cdap.proto.QueryHandle) QueryStatus(io.cdap.cdap.proto.QueryStatus)

Aggregations

QueryHandle (io.cdap.cdap.proto.QueryHandle)70 ExploreException (io.cdap.cdap.explore.service.ExploreException)40 SQLException (java.sql.SQLException)26 HiveSQLException (org.apache.hive.service.cli.HiveSQLException)22 OperationHandle (org.apache.hive.service.cli.OperationHandle)22 SessionHandle (org.apache.hive.service.cli.SessionHandle)22 HandleNotFoundException (io.cdap.cdap.explore.service.HandleNotFoundException)18 QueryStatus (io.cdap.cdap.proto.QueryStatus)16 Path (javax.ws.rs.Path)16 IOException (java.io.IOException)14 Callable (java.util.concurrent.Callable)10 POST (javax.ws.rs.POST)10 Test (org.junit.Test)10 JsonObject (com.google.gson.JsonObject)8 UnsupportedTypeException (io.cdap.cdap.api.data.schema.UnsupportedTypeException)8 JsonSyntaxException (com.google.gson.JsonSyntaxException)6 DatasetManagementException (io.cdap.cdap.api.dataset.DatasetManagementException)6 BadRequestException (io.cdap.cdap.common.BadRequestException)6 ColumnDesc (io.cdap.cdap.proto.ColumnDesc)6 QueryResult (io.cdap.cdap.proto.QueryResult)6