use of co.cask.cdap.proto.QueryHandle in project cdap by caskdata.
the class BaseHiveExploreService method getCatalogs.
@Override
public QueryHandle getCatalogs() throws ExploreException, SQLException {
startAndWait();
try {
SessionHandle sessionHandle = null;
OperationHandle operationHandle = null;
Map<String, String> sessionConf = startSession();
try {
sessionHandle = openHiveSession(sessionConf);
operationHandle = cliService.getCatalogs(sessionHandle);
QueryHandle handle = saveReadOnlyOperation(operationHandle, sessionHandle, sessionConf, "", "");
LOG.trace("Retrieving catalogs");
return handle;
} catch (Throwable e) {
closeInternal(getQueryHandle(sessionConf), new ReadOnlyOperationInfo(sessionHandle, operationHandle, sessionConf, "", ""));
throw e;
}
} catch (HiveSQLException e) {
throw getSqlException(e);
} catch (Throwable e) {
throw new ExploreException(e);
}
}
use of co.cask.cdap.proto.QueryHandle in project cdap by caskdata.
the class BaseHiveExploreService method saveReadWriteOperation.
/**
* Saves information associated with a Hive operation that writes to a Dataset.
* @param operationHandle {@link OperationHandle} of the Hive operation running.
* @param sessionHandle {@link SessionHandle} for the Hive operation running.
* @param sessionConf configuration for the session running the Hive operation.
* @param statement SQL statement executed with the call.
* @return {@link QueryHandle} that represents the Hive operation being run.
*/
private QueryHandle saveReadWriteOperation(OperationHandle operationHandle, SessionHandle sessionHandle, Map<String, String> sessionConf, String statement, String hiveDatabase) {
QueryHandle handle = QueryHandle.fromId(sessionConf.get(Constants.Explore.QUERY_ID));
activeHandleCache.put(handle, new ReadWriteOperationInfo(sessionHandle, operationHandle, sessionConf, statement, hiveDatabase));
return handle;
}
use of co.cask.cdap.proto.QueryHandle 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.QueryHandle in project cdap by caskdata.
the class NamespacedExploreQueryExecutorHttpHandler method query.
@POST
@Path("data/explore/queries")
@AuditPolicy(AuditDetail.REQUEST_BODY)
public void query(HttpRequest request, HttpResponder responder, @PathParam("namespace-id") final String namespaceId) throws Exception {
try {
Map<String, String> args = decodeArguments(request);
final String query = args.get("query");
final Map<String, String> additionalSessionConf = new HashMap<>(args);
additionalSessionConf.remove("query");
LOG.trace("Received query: {}", query);
QueryHandle queryHandle = impersonator.doAs(new NamespaceId(namespaceId), new Callable<QueryHandle>() {
@Override
public QueryHandle call() throws Exception {
return exploreService.execute(new NamespaceId(namespaceId), query, additionalSessionConf);
}
}, ImpersonatedOpType.EXPLORE);
responder.sendJson(HttpResponseStatus.OK, queryHandle);
} 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()));
}
}
use of co.cask.cdap.proto.QueryHandle in project cdap by caskdata.
the class ExploreExecutorHttpHandler method disableStream.
@POST
@Path("streams/{stream}/tables/{table}/disable")
public void disableStream(HttpRequest request, HttpResponder responder, @PathParam("namespace-id") String namespace, @PathParam("stream") String streamName, @PathParam("table") final String tableName) {
final StreamId streamId = new StreamId(namespace, streamName);
try {
// throws io exception if there is no stream
streamAdmin.getConfig(streamId);
} catch (IOException e) {
LOG.debug("Could not find stream {} to disable explore on.", streamName, e);
responder.sendString(HttpResponseStatus.NOT_FOUND, "Could not find stream " + streamName);
return;
}
try {
QueryHandle handle = impersonator.doAs(new NamespaceId(namespace), new Callable<QueryHandle>() {
@Override
public QueryHandle call() throws Exception {
return exploreTableManager.disableStream(tableName, streamId);
}
});
JsonObject json = new JsonObject();
json.addProperty("handle", handle.getHandle());
responder.sendJson(HttpResponseStatus.OK, json);
} catch (Throwable t) {
LOG.error("Got exception disabling exploration for stream {}", streamId, t);
responder.sendString(HttpResponseStatus.INTERNAL_SERVER_ERROR, t.getMessage());
}
}
Aggregations