use of io.cdap.cdap.explore.service.HandleNotFoundException in project cdap by caskdata.
the class BaseHiveExploreService method previewResults.
@Override
public List<QueryResult> previewResults(QueryHandle handle) throws ExploreException, HandleNotFoundException, SQLException {
startAndWait();
if (inactiveHandleCache.getIfPresent(handle) != null) {
throw new HandleNotFoundException("Query is inactive.", true);
}
OperationInfo operationInfo = getActiveOperationInfo(handle);
Lock previewLock = operationInfo.getPreviewLock();
previewLock.lock();
try {
File previewFile = operationInfo.getPreviewFile();
if (previewFile != null) {
try {
Reader reader = com.google.common.io.Files.newReader(previewFile, Charsets.UTF_8);
try {
return GSON.fromJson(reader, new TypeToken<List<QueryResult>>() {
}.getType());
} finally {
Closeables.closeQuietly(reader);
}
} catch (FileNotFoundException e) {
LOG.error("Could not retrieve preview result file {}", previewFile, e);
throw new ExploreException(e);
}
}
try {
// Create preview results for query
previewFile = new File(previewsDir, handle.getHandle());
try (FileWriter fileWriter = new FileWriter(previewFile)) {
List<QueryResult> results = fetchNextResults(handle, PREVIEW_COUNT);
GSON.toJson(results, fileWriter);
operationInfo.setPreviewFile(previewFile);
return results;
}
} catch (IOException e) {
LOG.error("Could not write preview results into file", e);
throw new ExploreException(e);
}
} finally {
previewLock.unlock();
}
}
use of io.cdap.cdap.explore.service.HandleNotFoundException in project cdap by caskdata.
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);
}
}
use of io.cdap.cdap.explore.service.HandleNotFoundException in project cdap by caskdata.
the class ExploreStatement method execute.
/**
* Executes a query and wait until it is finished, but does not close the session.
*/
@Override
public boolean execute(String sql) throws SQLException {
if (isClosed) {
throw new SQLException("Can't execute after statement has been closed");
}
if (resultSet != null) {
// As requested by the Statement interface javadoc, "All execution methods in the Statement interface
// implicitly close a statement's current ResultSet object if an open one exists"
resultSet.close();
resultSet = null;
}
futureResults = exploreClient.submit(namespace, sql);
try {
resultSet = new ExploreResultSet(futureResults.get(), this, maxRows);
// Here we have a result, it may contain rows or may be empty, but it exists.
return true;
} catch (InterruptedException e) {
LOG.error("Caught exception", e);
Thread.currentThread().interrupt();
return false;
} catch (ExecutionException e) {
Throwable t = Throwables.getRootCause(e);
if (t instanceof HandleNotFoundException) {
LOG.error("Error executing query", e);
throw new SQLException("Unknown state");
}
LOG.error("Caught exception", e);
throw new SQLException(Throwables.getRootCause(e));
} catch (CancellationException e) {
// If futureResults has been cancelled
return false;
}
}
use of io.cdap.cdap.explore.service.HandleNotFoundException in project cdap by caskdata.
the class BaseHiveExploreService method fetchNextResults.
@SuppressWarnings("unchecked")
private List<QueryResult> fetchNextResults(QueryHandle handle, int size) throws HiveSQLException, ExploreException, HandleNotFoundException {
startAndWait();
Lock nextLock = getActiveOperationInfo(handle).getNextLock();
nextLock.lock();
try {
// Fetch results from Hive
LOG.trace("Getting results for handle {}", handle);
OperationHandle operationHandle = getOperationHandle(handle);
if (operationHandle.hasResultSet()) {
return doFetchNextResults(operationHandle, FetchOrientation.FETCH_NEXT, size);
} else {
return Collections.emptyList();
}
} catch (Exception e) {
throw Throwables.propagate(e);
} finally {
nextLock.unlock();
}
}
use of io.cdap.cdap.explore.service.HandleNotFoundException in project cdap by caskdata.
the class ExecuteQueryCommand method perform.
@Override
public void perform(Arguments arguments, PrintStream output) throws Exception {
String query = arguments.get(ArgumentName.QUERY.toString());
long timeOutMins = arguments.getLongOptional(ArgumentName.TIMEOUT.toString(), DEFAULT_TIMEOUT_MIN);
ListenableFuture<ExploreExecutionResult> future = queryClient.execute(cliConfig.getCurrentNamespace(), query);
try {
ExploreExecutionResult executionResult = future.get(timeOutMins, TimeUnit.MINUTES);
if (!executionResult.canContainResults()) {
output.println("SQL statement does not output any result.");
executionResult.close();
return;
}
final List<ColumnDesc> schema = executionResult.getResultSchema();
String[] header = new String[schema.size()];
for (int i = 0; i < header.length; i++) {
ColumnDesc column = schema.get(i);
// Hive columns start at 1
int index = column.getPosition() - 1;
header[index] = column.getName() + ": " + column.getType();
}
List<QueryResult> rows = Lists.newArrayList(executionResult);
executionResult.close();
QueryStatus.OpStatus opStatus = executionResult.getStatus().getStatus();
if (opStatus != QueryStatus.OpStatus.FINISHED) {
throw new SQLException(String.format("Query '%s' execution did not finish successfully. " + "Got final state - %s", query, opStatus));
}
Table table = Table.builder().setHeader(header).setRows(rows, new RowMaker<QueryResult>() {
@Override
public List<?> makeRow(QueryResult object) {
return object.getColumns();
}
}).build();
cliConfig.getTableRenderer().render(cliConfig, output, table);
output.printf("Fetched %d rows", rows.size()).println();
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
} catch (ExecutionException e) {
Throwable t = Throwables.getRootCause(e);
if (t instanceof HandleNotFoundException) {
throw Throwables.propagate(t);
}
throw new SQLException(Throwables.getRootCause(e));
} catch (CancellationException e) {
throw new RuntimeException("Query has been cancelled on ListenableFuture object.");
} catch (TimeoutException e) {
output.println("Couldn't obtain results after " + timeOutMins + "mins.");
}
}
Aggregations