Search in sources :

Example 16 with QueryResult

use of io.cdap.cdap.proto.QueryResult in project cdap by caskdata.

the class BaseHiveExploreService method nextResults.

@Override
public List<QueryResult> nextResults(QueryHandle handle, int size) throws ExploreException, HandleNotFoundException, SQLException {
    startAndWait();
    InactiveOperationInfo inactiveOperationInfo = inactiveHandleCache.getIfPresent(handle);
    if (inactiveOperationInfo != null) {
        // Operation has been made inactive, so all results should have been fetched already - return empty list.
        LOG.trace("Returning empty result for inactive handle {}", handle);
        return ImmutableList.of();
    }
    try {
        List<QueryResult> results = fetchNextResults(handle, size);
        QueryStatus status = getStatus(handle);
        if (results.isEmpty() && status.getStatus() == QueryStatus.OpStatus.FINISHED) {
            // Since operation has fetched all the results, handle can be timed out aggressively.
            timeoutAggressively(handle, getResultSchema(handle), status);
        }
        return results;
    } catch (HiveSQLException e) {
        throw getSqlException(e);
    }
}
Also used : QueryResult(io.cdap.cdap.proto.QueryResult) HiveSQLException(org.apache.hive.service.cli.HiveSQLException) QueryStatus(io.cdap.cdap.proto.QueryStatus)

Example 17 with QueryResult

use of io.cdap.cdap.proto.QueryResult in project cdap by caskdata.

the class ExploreStatementTest method executeTest.

@Test
public void executeTest() throws Exception {
    List<ColumnDesc> columnDescriptions = Lists.newArrayList(new ColumnDesc("column1", "STRING", 1, ""));
    List<QueryResult> queryResults = Lists.newArrayList();
    ExploreClient exploreClient = new MockExploreClient(ImmutableMap.of("mock_query_1", columnDescriptions, "mock_query_2", columnDescriptions, "mock_query_3", columnDescriptions, "mock_query_4", columnDescriptions), ImmutableMap.of("mock_query_1", queryResults, "mock_query_2", queryResults, "mock_query_3", queryResults, "mock_query_4", queryResults));
    // Make sure an empty query still has a ResultSet associated to it
    ExploreStatement statement = new ExploreStatement(null, exploreClient, "ns1");
    Assert.assertTrue(statement.execute("mock_query_1"));
    ResultSet rs = statement.getResultSet();
    Assert.assertNotNull(rs);
    Assert.assertFalse(rs.isClosed());
    Assert.assertFalse(rs.next());
    rs = statement.executeQuery("mock_query_2");
    Assert.assertNotNull(rs);
    Assert.assertFalse(rs.isClosed());
    Assert.assertFalse(rs.next());
    // Make sure subsequent calls to an execute method close the previous results
    ResultSet rs2 = statement.executeQuery("mock_query_3");
    Assert.assertTrue(rs.isClosed());
    Assert.assertNotNull(rs2);
    Assert.assertFalse(rs2.isClosed());
    Assert.assertFalse(rs2.next());
    Assert.assertTrue(statement.execute("mock_query_4"));
    Assert.assertTrue(rs2.isClosed());
}
Also used : ExploreClient(io.cdap.cdap.explore.client.ExploreClient) MockExploreClient(io.cdap.cdap.explore.client.MockExploreClient) QueryResult(io.cdap.cdap.proto.QueryResult) MockExploreClient(io.cdap.cdap.explore.client.MockExploreClient) ResultSet(java.sql.ResultSet) ColumnDesc(io.cdap.cdap.proto.ColumnDesc) Test(org.junit.Test)

Example 18 with QueryResult

use of io.cdap.cdap.proto.QueryResult 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);
    }
}
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 19 with QueryResult

use of io.cdap.cdap.proto.QueryResult 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);
    }
}
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 20 with QueryResult

use of io.cdap.cdap.proto.QueryResult in project cdap by caskdata.

the class Hive12ExploreService method doFetchNextResults.

@SuppressWarnings("unchecked")
@Override
protected List<QueryResult> doFetchNextResults(OperationHandle handle, FetchOrientation fetchOrientation, int size) throws Exception {
    Class cliServiceClass = Class.forName("org.apache.hive.service.cli.CLIService");
    Method fetchResultsMethod = cliServiceClass.getMethod("fetchResults", OperationHandle.class, FetchOrientation.class, Long.TYPE);
    Object rowSet = fetchResultsMethod.invoke(getCliService(), handle, fetchOrientation, size);
    ImmutableList.Builder<QueryResult> rowsBuilder = ImmutableList.builder();
    Class rowSetClass = Class.forName("org.apache.hive.service.cli.RowSet");
    Method toTRowSetMethod = rowSetClass.getMethod("toTRowSet");
    TRowSet tRowSet = (TRowSet) toTRowSetMethod.invoke(rowSet);
    for (TRow tRow : tRowSet.getRows()) {
        List<Object> cols = Lists.newArrayList();
        for (TColumnValue tColumnValue : tRow.getColVals()) {
            cols.add(HiveUtilities.tColumnToObject(tColumnValue));
        }
        rowsBuilder.add(new QueryResult(cols));
    }
    return rowsBuilder.build();
}
Also used : TRowSet(org.apache.hive.service.cli.thrift.TRowSet) TRow(org.apache.hive.service.cli.thrift.TRow) QueryResult(io.cdap.cdap.proto.QueryResult) ImmutableList(com.google.common.collect.ImmutableList) Method(java.lang.reflect.Method) TColumnValue(org.apache.hive.service.cli.thrift.TColumnValue)

Aggregations

QueryResult (io.cdap.cdap.proto.QueryResult)74 ColumnDesc (io.cdap.cdap.proto.ColumnDesc)46 DatasetId (io.cdap.cdap.proto.id.DatasetId)36 Test (org.junit.Test)36 TimePartitionedFileSet (io.cdap.cdap.api.dataset.lib.TimePartitionedFileSet)18 ExploreExecutionResult (io.cdap.cdap.explore.client.ExploreExecutionResult)18 Location (org.apache.twill.filesystem.Location)16 PartitionedFileSet (io.cdap.cdap.api.dataset.lib.PartitionedFileSet)14 ImmutableList (com.google.common.collect.ImmutableList)12 FileSet (io.cdap.cdap.api.dataset.lib.FileSet)12 SQLException (java.sql.SQLException)10 Schema (io.cdap.cdap.api.data.schema.Schema)8 Table (io.cdap.cdap.api.dataset.table.Table)8 HandleNotFoundException (io.cdap.cdap.explore.service.HandleNotFoundException)8 PartitionedFileSetProperties (io.cdap.cdap.api.dataset.lib.PartitionedFileSetProperties)6 ExploreClient (io.cdap.cdap.explore.client.ExploreClient)6 MockExploreClient (io.cdap.cdap.explore.client.MockExploreClient)6 QueryHandle (io.cdap.cdap.proto.QueryHandle)6 QueryStatus (io.cdap.cdap.proto.QueryStatus)6 NamespaceId (io.cdap.cdap.proto.id.NamespaceId)6