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