Search in sources :

Example 26 with ExploreException

use of io.cdap.cdap.explore.service.ExploreException in project cdap by cdapio.

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();
    }
}
Also used : 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) Lock(java.util.concurrent.locks.Lock)

Example 27 with ExploreException

use of io.cdap.cdap.explore.service.ExploreException in project cdap by cdapio.

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();
    }
}
Also used : HandleNotFoundException(io.cdap.cdap.explore.service.HandleNotFoundException) QueryResult(io.cdap.cdap.proto.QueryResult) TypeToken(com.google.common.reflect.TypeToken) FileWriter(java.io.FileWriter) FileNotFoundException(java.io.FileNotFoundException) Reader(java.io.Reader) IOException(java.io.IOException) File(java.io.File) Lock(java.util.concurrent.locks.Lock) ExploreException(io.cdap.cdap.explore.service.ExploreException)

Example 28 with ExploreException

use of io.cdap.cdap.explore.service.ExploreException in project cdap by cdapio.

the class BaseHiveExploreService method getSchemas.

@Override
public QueryHandle getSchemas(String catalog, String schemaPattern) throws ExploreException, SQLException {
    startAndWait();
    try {
        SessionHandle sessionHandle = null;
        OperationHandle operationHandle = null;
        Map<String, String> sessionConf = startSession();
        String database = getHiveDatabase(schemaPattern);
        try {
            sessionHandle = openHiveSession(sessionConf);
            operationHandle = cliService.getSchemas(sessionHandle, catalog, database);
            QueryHandle handle = saveReadOnlyOperation(operationHandle, sessionHandle, sessionConf, "", database);
            LOG.trace("Retrieving schemas: catalog {}, schema {}", catalog, database);
            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);
    }
}
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 29 with ExploreException

use of io.cdap.cdap.explore.service.ExploreException in project cdap by cdapio.

the class BaseHiveExploreService method getMetaStoreClient.

private IMetaStoreClient getMetaStoreClient() throws ExploreException {
    if (metastoreClientLocal.get() == null) {
        try {
            IMetaStoreClient client = new HiveMetaStoreClient(createHiveConf());
            Supplier<IMetaStoreClient> supplier = Suppliers.ofInstance(client);
            metastoreClientLocal.set(supplier);
            // We use GC of the supplier as a signal for us to know that a thread is gone
            // The supplier is set into the thread local, which will get GC'ed when the thread is gone.
            // Since we use a weak reference key to the supplier that points to the client
            // (in the metastoreClientReferences map), it won't block GC of the supplier instance.
            // We can use the weak reference, which is retrieved through polling the ReferenceQueue,
            // to get back the client and call close() on it.
            metastoreClientReferences.put(new WeakReference<>(supplier, metastoreClientReferenceQueue), client);
        } catch (MetaException e) {
            throw new ExploreException("Error initializing Hive Metastore client", e);
        }
    }
    return metastoreClientLocal.get().get();
}
Also used : HiveMetaStoreClient(org.apache.hadoop.hive.metastore.HiveMetaStoreClient) IMetaStoreClient(org.apache.hadoop.hive.metastore.IMetaStoreClient) MetaException(org.apache.hadoop.hive.metastore.api.MetaException) ExploreException(io.cdap.cdap.explore.service.ExploreException)

Example 30 with ExploreException

use of io.cdap.cdap.explore.service.ExploreException in project cdap by cdapio.

the class BaseHiveExploreService method createNamespace.

@Override
public QueryHandle createNamespace(NamespaceMeta namespaceMeta) throws ExploreException, SQLException {
    startAndWait();
    try {
        // This check prevents the extra warn log.
        if (NamespaceId.DEFAULT.equals(namespaceMeta.getNamespaceId())) {
            return QueryHandle.NO_OP;
        }
        Map<String, String> sessionConf = startSession();
        SessionHandle sessionHandle = null;
        OperationHandle operationHandle = null;
        try {
            sessionHandle = openHiveSession(sessionConf);
            QueryHandle handle;
            if (Strings.isNullOrEmpty(namespaceMeta.getConfig().getHiveDatabase())) {
                // if no custom hive database was provided get the hive database according to cdap format and create it
                // if one does not exists since cdap is responsible for managing the lifecycle of such databases
                String database = createHiveDBName(namespaceMeta.getName());
                // "IF NOT EXISTS" so that this operation is idempotent.
                String statement = String.format("CREATE DATABASE IF NOT EXISTS %s", database);
                operationHandle = executeAsync(sessionHandle, statement);
                handle = saveReadOnlyOperation(operationHandle, sessionHandle, sessionConf, statement, database);
                LOG.info("Creating database {} with handle {}", database, handle);
            } else {
                // a custom database name was provided so check its existence
                // there is no way to check if a hive database exists or not other than trying to use it and see whether
                // it fails or not. So, run a USE databaseName command and see if it throws exception
                // Other way can be to list all database and check if the database exists or not but we are doing USE to
                // make sure that user can acutally use the database once we have impersonation.
                String statement = String.format("USE %s", namespaceMeta.getConfig().getHiveDatabase());
                // if the database does not exists the below line will throw exception from hive
                try {
                    operationHandle = executeAsync(sessionHandle, statement);
                } catch (HiveSQLException e) {
                    // then we will get an exception from Hive with error code 10072 which represent database was not found
                    if (e.toTStatus().getErrorCode() == ErrorMsg.DATABASE_NOT_EXISTS.getErrorCode()) {
                        // TODO: Add username here
                        throw new ExploreException(String.format("A custom Hive Database %s was provided for namespace %s " + "which does not exists. Please create the database in hive " + "for the user and try creating the namespace again.", namespaceMeta.getConfig().getHiveDatabase(), namespaceMeta.getName()), e);
                    } else {
                        // some other exception was generated while checking the existense of the database
                        throw new ExploreException(String.format("Failed to check existence of given custom hive database " + "%s for namespace %s", namespaceMeta.getConfig().getHiveDatabase(), namespaceMeta.getName()), e);
                    }
                }
                // if we didn't got an exception on the line above we know that the database exists
                handle = saveReadOnlyOperation(operationHandle, sessionHandle, sessionConf, statement, namespaceMeta.getConfig().getHiveDatabase());
                LOG.debug("Custom database {} existence verified with handle {}", namespaceMeta.getConfig().getHiveDatabase(), handle);
            }
            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)

Aggregations

ExploreException (io.cdap.cdap.explore.service.ExploreException)86 QueryHandle (io.cdap.cdap.proto.QueryHandle)32 SQLException (java.sql.SQLException)28 HiveSQLException (org.apache.hive.service.cli.HiveSQLException)28 IOException (java.io.IOException)24 OperationHandle (org.apache.hive.service.cli.OperationHandle)24 SessionHandle (org.apache.hive.service.cli.SessionHandle)24 HttpResponse (io.cdap.common.http.HttpResponse)22 Path (javax.ws.rs.Path)14 HandleNotFoundException (io.cdap.cdap.explore.service.HandleNotFoundException)12 UnsupportedTypeException (io.cdap.cdap.api.data.schema.UnsupportedTypeException)10 POST (javax.ws.rs.POST)10 JsonSyntaxException (com.google.gson.JsonSyntaxException)8 DatasetManagementException (io.cdap.cdap.api.dataset.DatasetManagementException)8 BadRequestException (io.cdap.cdap.common.BadRequestException)8 TableNotFoundException (io.cdap.cdap.explore.service.TableNotFoundException)8 DatasetId (io.cdap.cdap.proto.id.DatasetId)8 UnauthorizedException (io.cdap.cdap.security.spi.authorization.UnauthorizedException)8 TException (org.apache.thrift.TException)8 PartitionKey (io.cdap.cdap.api.dataset.lib.PartitionKey)6