Search in sources :

Example 16 with QualifiedName

use of com.netflix.metacat.common.QualifiedName in project metacat by Netflix.

the class HiveConnectorDatabaseService method listNames.

/**
     * {@inheritDoc}.
     */
@Override
public List<QualifiedName> listNames(@Nonnull @NonNull final ConnectorContext requestContext, @Nonnull @NonNull final QualifiedName name, @Nullable final QualifiedName prefix, @Nullable final Sort sort, @Nullable final Pageable pageable) {
    try {
        final List<QualifiedName> qualifiedNames = Lists.newArrayList();
        final String databaseFilter = (prefix != null) ? prefix.getDatabaseName() : null;
        for (String databaseName : metacatHiveClient.getAllDatabases()) {
            final QualifiedName qualifiedName = QualifiedName.ofDatabase(name.getCatalogName(), databaseName);
            if (databaseFilter != null && !databaseName.startsWith(databaseFilter)) {
                continue;
            }
            qualifiedNames.add(qualifiedName);
        }
        //supporting sort by qualified name only
        if (sort != null) {
            ConnectorUtils.sort(qualifiedNames, sort, Comparator.comparing(QualifiedName::toString));
        }
        return ConnectorUtils.paginate(qualifiedNames, pageable);
    } catch (MetaException exception) {
        throw new InvalidMetaException(name, exception);
    } catch (TException exception) {
        throw new ConnectorException(String.format("Failed listName hive database %s", name), exception);
    }
}
Also used : TException(org.apache.thrift.TException) QualifiedName(com.netflix.metacat.common.QualifiedName) ConnectorException(com.netflix.metacat.common.server.connectors.exception.ConnectorException) InvalidMetaException(com.netflix.metacat.common.server.connectors.exception.InvalidMetaException) MetaException(org.apache.hadoop.hive.metastore.api.MetaException) InvalidMetaException(com.netflix.metacat.common.server.connectors.exception.InvalidMetaException)

Example 17 with QualifiedName

use of com.netflix.metacat.common.QualifiedName in project metacat by Netflix.

the class CassandraConnectorTableService method listNames.

/**
     * {@inheritDoc}
     */
@Override
public List<QualifiedName> listNames(@Nonnull @NonNull final ConnectorContext context, @Nonnull @NonNull final QualifiedName name, @Nullable final QualifiedName prefix, @Nullable final Sort sort, @Nullable final Pageable pageable) {
    final String catalog = name.getCatalogName();
    final String keyspace = name.getDatabaseName();
    log.debug("Attempting to list table names in Cassandra keyspace {} for request {}", keyspace, context);
    try {
        final KeyspaceMetadata keyspaceMetadata = this.getCluster().getMetadata().getKeyspace(keyspace);
        if (keyspaceMetadata == null) {
            throw new DatabaseNotFoundException(name);
        }
        // TODO: Should we include views?
        final List<QualifiedName> tableNames = Lists.newArrayList();
        for (final TableMetadata tableMetadata : keyspaceMetadata.getTables()) {
            final String tableName = tableMetadata.getName();
            if (prefix != null && !tableName.startsWith(prefix.getTableName())) {
                continue;
            }
            tableNames.add(QualifiedName.ofTable(catalog, keyspace, tableName));
        }
        // Sort
        if (sort != null) {
            final Comparator<QualifiedName> tableNameComparator = Comparator.comparing(QualifiedName::getTableName);
            ConnectorUtils.sort(tableNames, sort, tableNameComparator);
        }
        // Paging
        final List<QualifiedName> paged = ConnectorUtils.paginate(tableNames, pageable);
        log.debug("Listed {} table names in Cassandra keyspace {} for request {}", paged.size(), keyspace, context);
        return paged;
    } catch (final DriverException de) {
        log.error(de.getMessage(), de);
        throw this.getExceptionMapper().toConnectorException(de, name);
    }
}
Also used : TableMetadata(com.datastax.driver.core.TableMetadata) DatabaseNotFoundException(com.netflix.metacat.common.server.connectors.exception.DatabaseNotFoundException) QualifiedName(com.netflix.metacat.common.QualifiedName) DriverException(com.datastax.driver.core.exceptions.DriverException) KeyspaceMetadata(com.datastax.driver.core.KeyspaceMetadata)

Example 18 with QualifiedName

use of com.netflix.metacat.common.QualifiedName in project metacat by Netflix.

the class CassandraConnectorDatabaseService method listNames.

/**
     * {@inheritDoc}
     */
@Override
public List<QualifiedName> listNames(@Nonnull @NonNull final ConnectorContext context, @Nonnull @NonNull final QualifiedName name, @Nullable final QualifiedName prefix, @Nullable final Sort sort, @Nullable final Pageable pageable) {
    log.debug("Attempting to list keyspaces for request {}", context);
    try {
        final List<QualifiedName> names = Lists.newArrayList();
        for (final KeyspaceMetadata keyspace : this.getCluster().getMetadata().getKeyspaces()) {
            final String keyspaceName = keyspace.getName();
            if (prefix != null && !keyspaceName.startsWith(prefix.getDatabaseName())) {
                continue;
            }
            names.add(QualifiedName.ofDatabase(name.getCatalogName(), keyspaceName));
        }
        if (sort != null) {
            // We can only really sort by the database name at this level so ignore SortBy field
            final Comparator<QualifiedName> comparator = Comparator.comparing(QualifiedName::getDatabaseName);
            ConnectorUtils.sort(names, sort, comparator);
        }
        final List<QualifiedName> results = ConnectorUtils.paginate(names, pageable);
        log.debug("Finished listing keyspaces for request {}", context);
        return results;
    } catch (final DriverException de) {
        log.error(de.getMessage(), de);
        throw this.getExceptionMapper().toConnectorException(de, name);
    }
}
Also used : QualifiedName(com.netflix.metacat.common.QualifiedName) DriverException(com.datastax.driver.core.exceptions.DriverException) KeyspaceMetadata(com.datastax.driver.core.KeyspaceMetadata)

Example 19 with QualifiedName

use of com.netflix.metacat.common.QualifiedName in project metacat by Netflix.

the class HiveConnectorTableService method create.

/**
     * Create a table.
     *
     * @param requestContext The request context
     * @param tableInfo      The resource metadata
     */
@Override
public void create(@Nonnull @NonNull final ConnectorContext requestContext, @Nonnull @NonNull final TableInfo tableInfo) {
    final QualifiedName tableName = tableInfo.getName();
    try {
        final Table table = hiveMetacatConverters.fromTableInfo(tableInfo);
        updateTable(requestContext, table, tableInfo);
        metacatHiveClient.createTable(table);
    } catch (AlreadyExistsException exception) {
        throw new TableAlreadyExistsException(tableName, exception);
    } catch (MetaException exception) {
        throw new InvalidMetaException(tableName, exception);
    } catch (NoSuchObjectException | InvalidObjectException exception) {
        throw new DatabaseNotFoundException(QualifiedName.ofDatabase(tableName.getCatalogName(), tableName.getDatabaseName()), exception);
    } catch (TException exception) {
        throw new ConnectorException(String.format("Failed create hive table %s", tableName), exception);
    }
}
Also used : TException(org.apache.thrift.TException) TableAlreadyExistsException(com.netflix.metacat.common.server.connectors.exception.TableAlreadyExistsException) Table(org.apache.hadoop.hive.metastore.api.Table) AlreadyExistsException(org.apache.hadoop.hive.metastore.api.AlreadyExistsException) TableAlreadyExistsException(com.netflix.metacat.common.server.connectors.exception.TableAlreadyExistsException) QualifiedName(com.netflix.metacat.common.QualifiedName) DatabaseNotFoundException(com.netflix.metacat.common.server.connectors.exception.DatabaseNotFoundException) ConnectorException(com.netflix.metacat.common.server.connectors.exception.ConnectorException) NoSuchObjectException(org.apache.hadoop.hive.metastore.api.NoSuchObjectException) InvalidObjectException(org.apache.hadoop.hive.metastore.api.InvalidObjectException) InvalidMetaException(com.netflix.metacat.common.server.connectors.exception.InvalidMetaException) MetaException(org.apache.hadoop.hive.metastore.api.MetaException) InvalidMetaException(com.netflix.metacat.common.server.connectors.exception.InvalidMetaException)

Example 20 with QualifiedName

use of com.netflix.metacat.common.QualifiedName in project metacat by Netflix.

the class MySqlTagService method list.

/**
     * Returns the list of <code>QualifiedName</code> of items that are tagged by the
     * given <code>includeTags</code> and do not contain the given <code>excludeTags</code>.
     *
     * @param includeTags  include items that contain tags
     * @param excludeTags  include items that do not contain tags
     * @param sourceName   catalog/source name
     * @param databaseName database name
     * @param tableName    table name
     * @return list of qualified names of the items
     */
@Override
public List<QualifiedName> list(final Set<String> includeTags, final Set<String> excludeTags, final String sourceName, final String databaseName, final String tableName) {
    Set<String> includedNames = Sets.newHashSet();
    final Set<String> excludedNames = Sets.newHashSet();
    final Connection connection = DBUtil.getReadConnection(getDataSource());
    try {
        final QueryRunner runner = new QueryRunner();
        final String wildCardName = QualifiedName.toWildCardString(sourceName, databaseName, tableName);
        //Includes
        String query = String.format(QUERY_SEARCH, "in ('" + Joiner.on("','").skipNulls().join(includeTags) + "')");
        final Object[] params = { includeTags.size() == 0 ? 1 : 0, wildCardName == null ? 1 : 0, wildCardName };
        includedNames.addAll(runner.query(connection, query, new ColumnListHandler<>("name"), params));
        if (!excludeTags.isEmpty()) {
            //Excludes
            query = String.format(QUERY_SEARCH, "in ('" + Joiner.on("','").skipNulls().join(excludeTags) + "')");
            final Object[] eParams = { excludeTags.size() == 0 ? 1 : 0, wildCardName == null ? 1 : 0, wildCardName };
            excludedNames.addAll(runner.query(connection, query, new ColumnListHandler<>("name"), eParams));
        }
    } catch (SQLException e) {
        final String message = String.format("Failed getting the list of qualified names for tags %s", includeTags);
        log.error(message, e);
        throw new UserMetadataServiceException(message, e);
    } finally {
        DBUtil.closeReadConnection(connection);
    }
    if (!excludeTags.isEmpty()) {
        includedNames = Sets.difference(includedNames, excludedNames);
    }
    return includedNames.stream().map(s -> QualifiedName.fromString(s, false)).collect(Collectors.toList());
}
Also used : Lookup(com.netflix.metacat.common.server.model.Lookup) UserMetadataService(com.netflix.metacat.common.server.usermetadata.UserMetadataService) Connection(java.sql.Connection) TagItem(com.netflix.metacat.common.server.model.TagItem) SQLException(java.sql.SQLException) TagService(com.netflix.metacat.common.server.usermetadata.TagService) ImmutableList(com.google.common.collect.ImmutableList) ScalarHandler(org.apache.commons.dbutils.handlers.ScalarHandler) Map(java.util.Map) UserMetadataServiceException(com.netflix.metacat.common.server.usermetadata.UserMetadataServiceException) DataSource(javax.sql.DataSource) LookupService(com.netflix.metacat.common.server.usermetadata.LookupService) Config(com.netflix.metacat.common.server.properties.Config) DataSourceManager(com.netflix.metacat.common.server.util.DataSourceManager) QueryRunner(org.apache.commons.dbutils.QueryRunner) Iterator(java.util.Iterator) MetacatJson(com.netflix.metacat.common.json.MetacatJson) BeanHandler(org.apache.commons.dbutils.handlers.BeanHandler) Set(java.util.Set) QualifiedName(com.netflix.metacat.common.QualifiedName) Maps(com.google.common.collect.Maps) Collectors(java.util.stream.Collectors) Sets(com.google.common.collect.Sets) Slf4j(lombok.extern.slf4j.Slf4j) List(java.util.List) DBUtil(com.netflix.metacat.common.server.util.DBUtil) Optional(java.util.Optional) Preconditions(com.google.common.base.Preconditions) ColumnListHandler(org.apache.commons.dbutils.handlers.ColumnListHandler) ResultSetHandler(org.apache.commons.dbutils.ResultSetHandler) Joiner(com.google.common.base.Joiner) UserMetadataServiceException(com.netflix.metacat.common.server.usermetadata.UserMetadataServiceException) SQLException(java.sql.SQLException) Connection(java.sql.Connection) ColumnListHandler(org.apache.commons.dbutils.handlers.ColumnListHandler) QueryRunner(org.apache.commons.dbutils.QueryRunner)

Aggregations

QualifiedName (com.netflix.metacat.common.QualifiedName)144 List (java.util.List)52 Lists (com.google.common.collect.Lists)44 Collectors (java.util.stream.Collectors)41 Map (java.util.Map)38 Slf4j (lombok.extern.slf4j.Slf4j)36 Strings (com.google.common.base.Strings)35 Nullable (javax.annotation.Nullable)33 Pageable (com.netflix.metacat.common.dto.Pageable)29 Sort (com.netflix.metacat.common.dto.Sort)29 Nonnull (javax.annotation.Nonnull)29 MetacatRequestContext (com.netflix.metacat.common.MetacatRequestContext)28 TableDto (com.netflix.metacat.common.dto.TableDto)27 Maps (com.google.common.collect.Maps)25 ConnectorException (com.netflix.metacat.common.server.connectors.exception.ConnectorException)25 DatabaseNotFoundException (com.netflix.metacat.common.server.connectors.exception.DatabaseNotFoundException)24 TableNotFoundException (com.netflix.metacat.common.server.connectors.exception.TableNotFoundException)24 Optional (java.util.Optional)22 Registry (com.netflix.spectator.api.Registry)21 Set (java.util.Set)21