use of io.cdap.cdap.explore.service.ExploreException in project cdap by caskdata.
the class BaseHiveExploreService method getTables.
@Override
public List<TableNameInfo> getTables(final String namespace) throws ExploreException {
startAndWait();
// TODO check if the database user is allowed to access if security is enabled
try {
String database = getHiveDatabase(namespace);
ImmutableList.Builder<TableNameInfo> builder = ImmutableList.builder();
List<String> tables = getMetaStoreClient().getAllTables(database);
for (String table : tables) {
builder.add(new TableNameInfo(database, table));
}
return builder.build();
} catch (TException e) {
throw new ExploreException("Error connecting to Hive metastore", e);
}
}
use of io.cdap.cdap.explore.service.ExploreException in project cdap by caskdata.
the class BaseHiveExploreService method deleteNamespace.
@Override
public QueryHandle deleteNamespace(NamespaceId namespace) throws ExploreException, SQLException {
startAndWait();
String customHiveDatabase;
try {
customHiveDatabase = namespaceQueryAdmin.get(namespace).getConfig().getHiveDatabase();
} catch (Exception e) {
throw new ExploreException(String.format("Failed to get namespace meta for the namespace %s", namespace));
}
if (Strings.isNullOrEmpty(customHiveDatabase)) {
// no custom hive database was given for this namespace so we need to delete it
try {
SessionHandle sessionHandle = null;
OperationHandle operationHandle = null;
Map<String, String> sessionConf = startSession();
String database = getHiveDatabase(namespace.getNamespace());
try {
sessionHandle = openHiveSession(sessionConf);
String statement = String.format("DROP DATABASE IF EXISTS %s", database);
operationHandle = executeAsync(sessionHandle, statement);
QueryHandle handle = saveReadOnlyOperation(operationHandle, sessionHandle, sessionConf, statement, database);
LOG.info("Deleting database {} with handle {}", database, handle);
return handle;
} catch (Throwable e) {
closeInternal(getQueryHandle(sessionConf), new ReadOnlyOperationInfo(sessionHandle, operationHandle, sessionConf, "", database));
throw e;
}
} catch (HiveSQLException e) {
throw getSqlException(e);
} catch (Throwable e) {
throw new ExploreException(e);
}
} else {
// a custom hive database was provided for this namespace do we don't need to delete it.
LOG.debug("Custom hive database {}. Skipping delete.", customHiveDatabase, namespace);
return QueryHandle.NO_OP;
}
}
use of io.cdap.cdap.explore.service.ExploreException 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 io.cdap.cdap.explore.service.ExploreException in project cdap by caskdata.
the class BaseHiveExploreService method getTypeInfo.
@Override
public QueryHandle getTypeInfo() throws ExploreException, SQLException {
startAndWait();
try {
SessionHandle sessionHandle = null;
OperationHandle operationHandle = null;
Map<String, String> sessionConf = startSession();
try {
sessionHandle = openHiveSession(sessionConf);
operationHandle = cliService.getTypeInfo(sessionHandle);
QueryHandle handle = saveReadOnlyOperation(operationHandle, sessionHandle, sessionConf, "", "");
LOG.trace("Retrieving type info");
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 io.cdap.cdap.explore.service.ExploreException 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