Search in sources :

Example 6 with ExploreException

use of co.cask.cdap.explore.service.ExploreException in project cdap by caskdata.

the class BaseHiveExploreService method getFunctions.

@Override
public QueryHandle getFunctions(String catalog, String schemaPattern, String functionNamePattern) 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.getFunctions(sessionHandle, catalog, database, functionNamePattern);
            QueryHandle handle = saveReadOnlyOperation(operationHandle, sessionHandle, sessionConf, "", database);
            LOG.trace("Retrieving functions: catalog {}, schema {}, function {}", catalog, database, functionNamePattern);
            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(co.cask.cdap.proto.QueryHandle) OperationHandle(org.apache.hive.service.cli.OperationHandle) ExploreException(co.cask.cdap.explore.service.ExploreException)

Example 7 with ExploreException

use of co.cask.cdap.explore.service.ExploreException in project cdap by caskdata.

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(co.cask.cdap.explore.service.HandleNotFoundException) QueryResult(co.cask.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(co.cask.cdap.explore.service.ExploreException)

Example 8 with ExploreException

use of co.cask.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(co.cask.cdap.proto.QueryHandle) OperationHandle(org.apache.hive.service.cli.OperationHandle) ExploreException(co.cask.cdap.explore.service.ExploreException)

Example 9 with ExploreException

use of co.cask.cdap.explore.service.ExploreException in project cdap by caskdata.

the class DistributedStorageProviderNamespaceAdmin method create.

@Override
public void create(NamespaceMeta namespaceMeta) throws IOException, ExploreException, SQLException {
    // create filesystem directory
    super.create(namespaceMeta);
    // skip namespace creation in HBase for default namespace
    if (NamespaceId.DEFAULT.equals(namespaceMeta.getNamespaceId())) {
        return;
    }
    // create HBase namespace and set group C(reate) permission if a group is configured
    String hbaseNamespace = tableUtil.getHBaseNamespace(namespaceMeta);
    if (Strings.isNullOrEmpty(namespaceMeta.getConfig().getHbaseNamespace())) {
        try (HBaseDDLExecutor executor = hBaseDDLExecutorFactory.get()) {
            boolean created = executor.createNamespaceIfNotExists(hbaseNamespace);
            if (namespaceMeta.getConfig().getGroupName() != null) {
                try {
                    executor.grantPermissions(hbaseNamespace, null, ImmutableMap.of("@" + namespaceMeta.getConfig().getGroupName(), "C"));
                } catch (IOException | RuntimeException e) {
                    // don't leave a partial state behind, as this fails the create(), the namespace should be removed
                    if (created) {
                        try {
                            executor.deleteNamespaceIfExists(hbaseNamespace);
                        } catch (Throwable t) {
                            e.addSuppressed(t);
                        }
                    }
                    throw e;
                }
            }
        } catch (Throwable t) {
            try {
                // if we failed to create a namespace in hbase then do clean up for above creations
                super.delete(namespaceMeta.getNamespaceId());
            } catch (Exception e) {
                t.addSuppressed(e);
            }
            throw t;
        }
    }
    try (HBaseAdmin admin = new HBaseAdmin(hConf)) {
        if (!tableUtil.hasNamespace(admin, hbaseNamespace)) {
            throw new IOException(String.format("HBase namespace '%s' specified for new namespace '%s' does not" + " exist. Please specify an existing HBase namespace.", hbaseNamespace, namespaceMeta.getName()));
        }
    }
}
Also used : HBaseDDLExecutor(co.cask.cdap.spi.hbase.HBaseDDLExecutor) HBaseAdmin(org.apache.hadoop.hbase.client.HBaseAdmin) IOException(java.io.IOException) IOException(java.io.IOException) SQLException(java.sql.SQLException) ExploreException(co.cask.cdap.explore.service.ExploreException)

Example 10 with ExploreException

use of co.cask.cdap.explore.service.ExploreException in project cdap by caskdata.

the class ExploreHttpClient method getSchemas.

@Override
public QueryHandle getSchemas(@Nullable String catalog, @Nullable String schemaPattern) throws ExploreException, SQLException {
    String body = GSON.toJson(new SchemasArgs(catalog, schemaPattern));
    String resource = String.format("namespaces/%s/data/explore/jdbc/schemas", schemaPattern);
    HttpResponse response = doPost(resource, body, null);
    if (response.getResponseCode() == HttpURLConnection.HTTP_OK) {
        return QueryHandle.fromId(parseResponseAsMap(response, "handle"));
    }
    throw new ExploreException("Cannot get the schemas. Reason: " + response);
}
Also used : HttpResponse(co.cask.common.http.HttpResponse) SchemasArgs(co.cask.cdap.explore.utils.SchemasArgs) ExploreException(co.cask.cdap.explore.service.ExploreException)

Aggregations

ExploreException (co.cask.cdap.explore.service.ExploreException)41 QueryHandle (co.cask.cdap.proto.QueryHandle)16 HiveSQLException (org.apache.hive.service.cli.HiveSQLException)14 HttpResponse (co.cask.common.http.HttpResponse)12 OperationHandle (org.apache.hive.service.cli.OperationHandle)12 SessionHandle (org.apache.hive.service.cli.SessionHandle)12 SQLException (java.sql.SQLException)11 IOException (java.io.IOException)9 HandleNotFoundException (co.cask.cdap.explore.service.HandleNotFoundException)6 TableNotFoundException (co.cask.cdap.explore.service.TableNotFoundException)4 Path (javax.ws.rs.Path)4 QueryStatus (co.cask.cdap.proto.QueryStatus)3 FileNotFoundException (java.io.FileNotFoundException)3 TException (org.apache.thrift.TException)3 UnsupportedTypeException (co.cask.cdap.api.data.schema.UnsupportedTypeException)2 NamespaceNotFoundException (co.cask.cdap.common.NamespaceNotFoundException)2 HBaseDDLExecutor (co.cask.cdap.spi.hbase.HBaseDDLExecutor)2 ImmutableList (com.google.common.collect.ImmutableList)2 JsonObject (com.google.gson.JsonObject)2 HashMap (java.util.HashMap)2