Search in sources :

Example 1 with ExploreException

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);
    }
}
Also used : TException(org.apache.thrift.TException) ImmutableList(com.google.common.collect.ImmutableList) TableNameInfo(io.cdap.cdap.proto.TableNameInfo) ExploreException(io.cdap.cdap.explore.service.ExploreException)

Example 2 with ExploreException

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;
    }
}
Also used : HiveSQLException(org.apache.hive.service.cli.HiveSQLException) SessionHandle(org.apache.hive.service.cli.SessionHandle) QueryHandle(io.cdap.cdap.proto.QueryHandle) OperationHandle(org.apache.hive.service.cli.OperationHandle) NamespaceNotFoundException(io.cdap.cdap.common.NamespaceNotFoundException) SQLException(java.sql.SQLException) TableNotFoundException(io.cdap.cdap.explore.service.TableNotFoundException) TException(org.apache.thrift.TException) IOException(java.io.IOException) HandleNotFoundException(io.cdap.cdap.explore.service.HandleNotFoundException) NoSuchObjectException(org.apache.hadoop.hive.metastore.api.NoSuchObjectException) TransactionFailureException(org.apache.tephra.TransactionFailureException) MetaException(org.apache.hadoop.hive.metastore.api.MetaException) ExploreException(io.cdap.cdap.explore.service.ExploreException) FileNotFoundException(java.io.FileNotFoundException) HiveSQLException(org.apache.hive.service.cli.HiveSQLException) ExploreException(io.cdap.cdap.explore.service.ExploreException)

Example 3 with ExploreException

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);
    }
}
Also used : HiveSQLException(org.apache.hive.service.cli.HiveSQLException) SessionHandle(org.apache.hive.service.cli.SessionHandle) QueryHandle(io.cdap.cdap.proto.QueryHandle) OperationHandle(org.apache.hive.service.cli.OperationHandle) QueryStatus(io.cdap.cdap.proto.QueryStatus) ExploreException(io.cdap.cdap.explore.service.ExploreException)

Example 4 with ExploreException

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);
    }
}
Also used : HiveSQLException(org.apache.hive.service.cli.HiveSQLException) SessionHandle(org.apache.hive.service.cli.SessionHandle) QueryHandle(io.cdap.cdap.proto.QueryHandle) OperationHandle(org.apache.hive.service.cli.OperationHandle) ExploreException(io.cdap.cdap.explore.service.ExploreException)

Example 5 with ExploreException

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;
    }
}
Also used : HandleNotFoundException(io.cdap.cdap.explore.service.HandleNotFoundException) SQLException(java.sql.SQLException) QueryHandle(io.cdap.cdap.proto.QueryHandle) QueryStatus(io.cdap.cdap.proto.QueryStatus) Callable(java.util.concurrent.Callable) ExploreException(io.cdap.cdap.explore.service.ExploreException) Path(javax.ws.rs.Path) GET(javax.ws.rs.GET)

Aggregations

ExploreException (io.cdap.cdap.explore.service.ExploreException)43 QueryHandle (io.cdap.cdap.proto.QueryHandle)16 SQLException (java.sql.SQLException)14 HiveSQLException (org.apache.hive.service.cli.HiveSQLException)14 IOException (java.io.IOException)12 OperationHandle (org.apache.hive.service.cli.OperationHandle)12 SessionHandle (org.apache.hive.service.cli.SessionHandle)12 HttpResponse (io.cdap.common.http.HttpResponse)11 Path (javax.ws.rs.Path)7 HandleNotFoundException (io.cdap.cdap.explore.service.HandleNotFoundException)6 UnsupportedTypeException (io.cdap.cdap.api.data.schema.UnsupportedTypeException)5 JsonSyntaxException (com.google.gson.JsonSyntaxException)4 DatasetManagementException (io.cdap.cdap.api.dataset.DatasetManagementException)4 BadRequestException (io.cdap.cdap.common.BadRequestException)4 TableNotFoundException (io.cdap.cdap.explore.service.TableNotFoundException)4 DatasetId (io.cdap.cdap.proto.id.DatasetId)4 UnauthorizedException (io.cdap.cdap.security.spi.authorization.UnauthorizedException)4 POST (javax.ws.rs.POST)4 TException (org.apache.thrift.TException)4 PartitionKey (io.cdap.cdap.api.dataset.lib.PartitionKey)3