use of co.cask.cdap.proto.QueryStatus in project cdap by caskdata.
the class BaseHiveExploreService method execute.
@Override
public QueryHandle execute(NamespaceId namespace, String[] statements) throws ExploreException, SQLException {
Preconditions.checkArgument(statements.length > 0, "There must be at least one statement");
startAndWait();
try {
SessionHandle sessionHandle = null;
OperationHandle operationHandle = null;
Map<String, String> sessionConf = startSession(namespace);
String database = getHiveDatabase(namespace.getNamespace());
try {
sessionHandle = openHiveSession(sessionConf);
// Switch database to the one being passed in.
setCurrentDatabase(database);
// synchronously execute all but the last statement
for (int i = 0; i < statements.length - 1; i++) {
String statement = statements[i];
LOG.trace("Executing statement synchronously: {}", statement);
operationHandle = executeSync(sessionHandle, statement);
QueryStatus status = doFetchStatus(operationHandle);
if (QueryStatus.OpStatus.ERROR == status.getStatus()) {
throw new HiveSQLException(status.getErrorMessage(), status.getSqlState());
}
if (QueryStatus.OpStatus.FINISHED != status.getStatus()) {
throw new ExploreException("Expected operation status FINISHED for statement '{}' but received " + status.getStatus());
}
}
String statement = statements[statements.length - 1];
operationHandle = executeAsync(sessionHandle, statement);
QueryHandle handle = saveReadWriteOperation(operationHandle, sessionHandle, sessionConf, statement, database);
LOG.trace("Executing statement: {} with handle {}", statement, handle);
return handle;
} catch (Throwable e) {
closeInternal(getQueryHandle(sessionConf), new ReadWriteOperationInfo(sessionHandle, operationHandle, sessionConf, "", database));
throw e;
}
} catch (HiveSQLException e) {
throw getSqlException(e);
} catch (Throwable e) {
throw new ExploreException(e);
}
}
use of co.cask.cdap.proto.QueryStatus in project cdap by caskdata.
the class Hive12ExploreService method doFetchStatus.
@Override
protected QueryStatus doFetchStatus(OperationHandle operationHandle) throws HiveSQLException, ExploreException, HandleNotFoundException {
try {
// In Hive 12, CLIService.getOperationStatus returns OperationState.
// In Hive 13, CLIService.getOperationStatus returns OperationStatus.
// Since we use Hive 13 for dev, we need the following workaround to get Hive 12 working.
Class<? extends CLIService> cliServiceClass = getCliService().getClass();
Method m = cliServiceClass.getMethod("getOperationStatus", OperationHandle.class);
OperationState operationState = (OperationState) m.invoke(getCliService(), operationHandle);
return new QueryStatus(QueryStatus.OpStatus.valueOf(operationState.toString()), operationHandle.hasResultSet());
} catch (InvocationTargetException | NoSuchMethodException | IllegalAccessException e) {
throw Throwables.propagate(e);
}
}
use of co.cask.cdap.proto.QueryStatus in project cdap by caskdata.
the class BaseHiveExploreServiceTest method waitForCompletionStatus.
protected static QueryStatus waitForCompletionStatus(QueryHandle handle, long sleepTime, TimeUnit timeUnit, int maxTries) throws ExploreException, HandleNotFoundException, InterruptedException, SQLException {
QueryStatus status;
int tries = 0;
do {
timeUnit.sleep(sleepTime);
status = exploreService.getStatus(handle);
if (++tries > maxTries) {
break;
}
} while (!status.getStatus().isDone());
return status;
}
use of co.cask.cdap.proto.QueryStatus in project cdap by caskdata.
the class HiveExploreServiceTestRun method previewResultsTest.
@Test
public void previewResultsTest() throws Exception {
DatasetId myTable2 = NAMESPACE_ID.dataset("my_table_2");
DatasetId myTable3 = NAMESPACE_ID.dataset("my_table_3");
DatasetId myTable4 = NAMESPACE_ID.dataset("my_table_4");
DatasetId myTable5 = NAMESPACE_ID.dataset("my_table_5");
DatasetId myTable6 = NAMESPACE_ID.dataset("my_table_6");
datasetFramework.addInstance("keyStructValueTable", myTable2, DatasetProperties.EMPTY);
datasetFramework.addInstance("keyStructValueTable", myTable3, DatasetProperties.EMPTY);
datasetFramework.addInstance("keyStructValueTable", myTable4, DatasetProperties.EMPTY);
datasetFramework.addInstance("keyStructValueTable", myTable5, DatasetProperties.EMPTY);
datasetFramework.addInstance("keyStructValueTable", myTable6, DatasetProperties.EMPTY);
try {
QueryHandle handle = exploreService.execute(NAMESPACE_ID, "show tables");
QueryStatus status = waitForCompletionStatus(handle, 200, TimeUnit.MILLISECONDS, 50);
Assert.assertEquals(QueryStatus.OpStatus.FINISHED, status.getStatus());
List<QueryResult> firstPreview = exploreService.previewResults(handle);
Assert.assertEquals(ImmutableList.of(new QueryResult(ImmutableList.<Object>of(MY_TABLE_NAME)), new QueryResult(ImmutableList.<Object>of(getDatasetHiveName(myTable2))), new QueryResult(ImmutableList.<Object>of(getDatasetHiveName(myTable3))), new QueryResult(ImmutableList.<Object>of(getDatasetHiveName(myTable4))), new QueryResult(ImmutableList.<Object>of(getDatasetHiveName(myTable5)))), firstPreview);
// test that preview results do not change even when the query cursor is updated by nextResults
List<QueryResult> endResults = exploreService.nextResults(handle, 100);
Assert.assertEquals(ImmutableList.of(new QueryResult(ImmutableList.<Object>of(getDatasetHiveName(myTable6)))), endResults);
List<QueryResult> secondPreview = exploreService.previewResults(handle);
Assert.assertEquals(firstPreview, secondPreview);
Assert.assertEquals(ImmutableList.<QueryResult>of(), exploreService.nextResults(handle, 100));
try {
// All results are fetched, query should be inactive now
exploreService.previewResults(handle);
Assert.fail("HandleNotFoundException expected - query should be inactive.");
} catch (HandleNotFoundException e) {
Assert.assertTrue(e.isInactive());
// Expected exception
}
// now test preview on a query that doesn't return any results
handle = exploreService.execute(NAMESPACE_ID, "select * from " + getDatasetHiveName(myTable2));
status = waitForCompletionStatus(handle, 200, TimeUnit.MILLISECONDS, 50);
Assert.assertEquals(QueryStatus.OpStatus.FINISHED, status.getStatus());
Assert.assertTrue(exploreService.previewResults(handle).isEmpty());
// calling preview again should return the same thing. it should not throw an exception
Assert.assertTrue(exploreService.previewResults(handle).isEmpty());
} finally {
datasetFramework.deleteInstance(myTable2);
datasetFramework.deleteInstance(myTable3);
datasetFramework.deleteInstance(myTable4);
datasetFramework.deleteInstance(myTable5);
datasetFramework.deleteInstance(myTable6);
}
}
use of co.cask.cdap.proto.QueryStatus in project cdap by caskdata.
the class ExploreQueryExecutorHttpHandler method getQueryStatus.
@GET
@Path("data/explore/queries/{id}/status")
public void getQueryStatus(HttpRequest request, HttpResponder responder, @PathParam("id") String id) throws ExploreException {
try {
final QueryHandle handle = QueryHandle.fromId(id);
QueryStatus status;
if (!handle.equals(QueryHandle.NO_OP)) {
status = doAs(handle, new Callable<QueryStatus>() {
@Override
public QueryStatus call() throws Exception {
return exploreService.getStatus(handle);
}
});
} else {
status = QueryStatus.NO_OP;
}
responder.sendJson(HttpResponseStatus.OK, GSON.toJson(status));
} 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);
} catch (ExploreException | RuntimeException e) {
LOG.debug("Got exception:", e);
throw e;
}
}
Aggregations