Search in sources :

Example 31 with ConnectorException

use of com.netflix.metacat.common.server.connectors.exception.ConnectorException in project metacat by Netflix.

the class HiveConnectorDatabaseService method list.

/**
 * {@inheritDoc}.
 */
@Override
public List<DatabaseInfo> list(final ConnectorRequestContext requestContext, final QualifiedName name, @Nullable final QualifiedName prefix, @Nullable final Sort sort, @Nullable final Pageable pageable) {
    try {
        final List<DatabaseInfo> databaseInfos = Lists.newArrayList();
        for (String databaseName : metacatHiveClient.getAllDatabases()) {
            final QualifiedName qualifiedName = QualifiedName.ofDatabase(name.getCatalogName(), databaseName);
            if (prefix != null && !qualifiedName.toString().startsWith(prefix.toString())) {
                continue;
            }
            databaseInfos.add(DatabaseInfo.builder().name(qualifiedName).build());
        }
        // supporting sort by name only
        if (sort != null) {
            ConnectorUtils.sort(databaseInfos, sort, Comparator.comparing(p -> p.getName().getDatabaseName()));
        }
        return ConnectorUtils.paginate(databaseInfos, pageable);
    } catch (MetaException exception) {
        throw new InvalidMetaException(name, exception);
    } catch (TException exception) {
        throw new ConnectorException(String.format("Failed list hive database %s", name), exception);
    }
}
Also used : ConnectorDatabaseService(com.netflix.metacat.common.server.connectors.ConnectorDatabaseService) MetaException(org.apache.hadoop.hive.metastore.api.MetaException) DatabaseAlreadyExistsException(com.netflix.metacat.common.server.connectors.exception.DatabaseAlreadyExistsException) Pageable(com.netflix.metacat.common.dto.Pageable) TException(org.apache.thrift.TException) QualifiedName(com.netflix.metacat.common.QualifiedName) InvalidObjectException(org.apache.hadoop.hive.metastore.api.InvalidObjectException) DatabaseNotFoundException(com.netflix.metacat.common.server.connectors.exception.DatabaseNotFoundException) AlreadyExistsException(org.apache.hadoop.hive.metastore.api.AlreadyExistsException) InvalidMetaException(com.netflix.metacat.common.server.connectors.exception.InvalidMetaException) DatabaseInfo(com.netflix.metacat.common.server.connectors.model.DatabaseInfo) List(java.util.List) Lists(com.google.common.collect.Lists) ConnectorException(com.netflix.metacat.common.server.connectors.exception.ConnectorException) MetacatNotSupportedException(com.netflix.metacat.common.exception.MetacatNotSupportedException) ConnectorUtils(com.netflix.metacat.common.server.connectors.ConnectorUtils) HiveConnectorInfoConverter(com.netflix.metacat.connector.hive.converters.HiveConnectorInfoConverter) Comparator(java.util.Comparator) Database(org.apache.hadoop.hive.metastore.api.Database) ConnectorRequestContext(com.netflix.metacat.common.server.connectors.ConnectorRequestContext) InvalidOperationException(org.apache.hadoop.hive.metastore.api.InvalidOperationException) NoSuchObjectException(org.apache.hadoop.hive.metastore.api.NoSuchObjectException) Nullable(javax.annotation.Nullable) Sort(com.netflix.metacat.common.dto.Sort) TException(org.apache.thrift.TException) DatabaseInfo(com.netflix.metacat.common.server.connectors.model.DatabaseInfo) 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 32 with ConnectorException

use of com.netflix.metacat.common.server.connectors.exception.ConnectorException in project metacat by Netflix.

the class JdbcConnectorTableService method get.

/**
 * {@inheritDoc}
 */
@Override
public TableInfo get(@Nonnull final ConnectorRequestContext context, @Nonnull final QualifiedName name) {
    log.debug("Beginning to get table metadata for qualified name {} for request {}", name, context);
    try (Connection connection = this.getConnection(name.getDatabaseName())) {
        final ImmutableList.Builder<FieldInfo> fields = ImmutableList.builder();
        try (ResultSet columns = this.getColumns(connection, name)) {
            while (columns.next()) {
                final String type = columns.getString("TYPE_NAME");
                final String size = columns.getString("COLUMN_SIZE");
                final String precision = columns.getString("DECIMAL_DIGITS");
                final String sourceType = this.buildSourceType(type, size, precision);
                final FieldInfo.FieldInfoBuilder fieldInfo = FieldInfo.builder().name(columns.getString("COLUMN_NAME")).sourceType(sourceType).type(this.typeConverter.toMetacatType(sourceType)).comment(columns.getString("REMARKS")).isNullable(columns.getString("IS_NULLABLE").equals("YES")).defaultValue(columns.getString("COLUMN_DEF"));
                if (size != null) {
                    fieldInfo.size(Integer.parseInt(size));
                }
                fields.add(fieldInfo.build());
            }
        }
        final List<FieldInfo> fieldInfos = fields.build();
        // If table does not exist, throw TableNotFoundException.
        if (fieldInfos.isEmpty() && !exists(context, name)) {
            throw new TableNotFoundException(name);
        }
        // Set table details
        final TableInfo result = TableInfo.builder().name(name).fields(fields.build()).build();
        setTableInfoDetails(connection, result);
        log.debug("Finished getting table metadata for qualified name {} for request {}", name, context);
        return result;
    } catch (final SQLException se) {
        throw new ConnectorException(se.getMessage(), se);
    }
}
Also used : TableNotFoundException(com.netflix.metacat.common.server.connectors.exception.TableNotFoundException) SQLException(java.sql.SQLException) ImmutableList(com.google.common.collect.ImmutableList) ConnectorException(com.netflix.metacat.common.server.connectors.exception.ConnectorException) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) TableInfo(com.netflix.metacat.common.server.connectors.model.TableInfo) FieldInfo(com.netflix.metacat.common.server.connectors.model.FieldInfo)

Example 33 with ConnectorException

use of com.netflix.metacat.common.server.connectors.exception.ConnectorException in project metacat by Netflix.

the class PolarisConnectorTableService method update.

/**
 * {@inheritDoc}.
 */
@Override
public void update(final ConnectorRequestContext requestContext, final TableInfo tableInfo) {
    final QualifiedName name = tableInfo.getName();
    final Config conf = connectorContext.getConfig();
    try {
        final Map<String, String> newTableMetadata = tableInfo.getMetadata();
        if (MapUtils.isEmpty(newTableMetadata)) {
            final String message = String.format("No parameters defined for iceberg table %s", name);
            log.warn(message);
            throw new InvalidMetaException(name, message, null);
        }
        final String prevLoc = newTableMetadata.get(DirectSqlTable.PARAM_PREVIOUS_METADATA_LOCATION);
        final String newLoc = newTableMetadata.get(DirectSqlTable.PARAM_METADATA_LOCATION);
        if (StringUtils.isBlank(prevLoc) || StringUtils.isBlank(newLoc)) {
            final String message = String.format("Invalid metadata for %s. Provided previous %s or new %s location is empty.", name, prevLoc, newLoc);
            log.error(message);
            throw new InvalidMetaException(name, message, null);
        }
        if (conf.isIcebergPreviousMetadataLocationCheckEnabled() && !icebergTableHandler.doesMetadataLocationExist(name, prevLoc)) {
            final String message = String.format("Provided previous metadata location: %s for table: %s does not exist.", name, prevLoc);
            log.error(message);
            throw new InvalidMetaException(name, message, null);
        }
        // optimistically attempt to update metadata location
        final boolean updated = polarisStoreService.updateTableMetadataLocation(name.getDatabaseName(), name.getTableName(), prevLoc, newLoc);
        // if succeeded then done, else try to figure out why and throw corresponding exception
        if (updated) {
            requestContext.setIgnoreErrorsAfterUpdate(true);
            return;
        }
        final PolarisTableEntity table = polarisStoreService.getTable(name.getDatabaseName(), name.getTableName()).orElseThrow(() -> new TableNotFoundException(name));
        final String existingLoc = table.getMetadataLocation();
        if (StringUtils.isBlank(existingLoc)) {
            final String message = String.format("Invalid metadata location for %s existing location is empty.", name);
            log.error(message);
            throw new TablePreconditionFailedException(name, message, existingLoc, prevLoc);
        }
        if (StringUtils.equalsIgnoreCase(existingLoc, newLoc)) {
            log.warn("Existing metadata location is the same as new. Existing: {}, New: {}", existingLoc, newLoc);
            return;
        }
        if (!Objects.equals(existingLoc, prevLoc)) {
            final String message = String.format("Invalid metadata location for %s expected: %s, provided: %s", name, existingLoc, prevLoc);
            log.error(message);
            throw new TablePreconditionFailedException(name, message, existingLoc, prevLoc);
        }
    } catch (TableNotFoundException | InvalidMetaException | TablePreconditionFailedException exception) {
        throw exception;
    } catch (DataIntegrityViolationException exception) {
        throw new InvalidMetaException(name, exception);
    } catch (Exception exception) {
        final String msg = String.format("Failed updating polaris table %s", tableInfo.getName());
        log.error(msg, exception);
        throw new ConnectorException(msg, exception);
    }
}
Also used : TablePreconditionFailedException(com.netflix.metacat.common.server.connectors.exception.TablePreconditionFailedException) Config(com.netflix.metacat.common.server.properties.Config) QualifiedName(com.netflix.metacat.common.QualifiedName) PolarisTableEntity(com.netflix.metacat.connector.polaris.store.entities.PolarisTableEntity) InvalidMetaException(com.netflix.metacat.common.server.connectors.exception.InvalidMetaException) TablePreconditionFailedException(com.netflix.metacat.common.server.connectors.exception.TablePreconditionFailedException) InvalidMetaException(com.netflix.metacat.common.server.connectors.exception.InvalidMetaException) ConnectorException(com.netflix.metacat.common.server.connectors.exception.ConnectorException) DataIntegrityViolationException(org.springframework.dao.DataIntegrityViolationException) TableNotFoundException(com.netflix.metacat.common.server.connectors.exception.TableNotFoundException) TableAlreadyExistsException(com.netflix.metacat.common.server.connectors.exception.TableAlreadyExistsException) DataIntegrityViolationException(org.springframework.dao.DataIntegrityViolationException) TableNotFoundException(com.netflix.metacat.common.server.connectors.exception.TableNotFoundException) ConnectorException(com.netflix.metacat.common.server.connectors.exception.ConnectorException)

Example 34 with ConnectorException

use of com.netflix.metacat.common.server.connectors.exception.ConnectorException in project metacat by Netflix.

the class PolarisConnectorTableService method listNames.

/**
 * {@inheritDoc}.
 */
@Override
public List<QualifiedName> listNames(final ConnectorRequestContext requestContext, final QualifiedName name, @Nullable final QualifiedName prefix, @Nullable final Sort sort, @Nullable final Pageable pageable) {
    try {
        final List<QualifiedName> qualifiedNames = Lists.newArrayList();
        final String tableFilter = (prefix != null && prefix.isTableDefinition()) ? prefix.getTableName() : "";
        for (String tableName : polarisStoreService.getTables(name.getDatabaseName(), tableFilter)) {
            final QualifiedName qualifiedName = QualifiedName.ofTable(name.getCatalogName(), name.getDatabaseName(), tableName);
            if (prefix != null && !qualifiedName.toString().startsWith(prefix.toString())) {
                continue;
            }
            qualifiedNames.add(qualifiedName);
        }
        if (sort != null) {
            ConnectorUtils.sort(qualifiedNames, sort, Comparator.comparing(QualifiedName::toString));
        }
        return ConnectorUtils.paginate(qualifiedNames, pageable);
    } catch (Exception exception) {
        final String msg = String.format("Failed polaris list table names %s using prefix %s", name, prefix);
        log.error(msg, exception);
        throw new ConnectorException(msg, exception);
    }
}
Also used : QualifiedName(com.netflix.metacat.common.QualifiedName) ConnectorException(com.netflix.metacat.common.server.connectors.exception.ConnectorException) TablePreconditionFailedException(com.netflix.metacat.common.server.connectors.exception.TablePreconditionFailedException) InvalidMetaException(com.netflix.metacat.common.server.connectors.exception.InvalidMetaException) ConnectorException(com.netflix.metacat.common.server.connectors.exception.ConnectorException) DataIntegrityViolationException(org.springframework.dao.DataIntegrityViolationException) TableNotFoundException(com.netflix.metacat.common.server.connectors.exception.TableNotFoundException) TableAlreadyExistsException(com.netflix.metacat.common.server.connectors.exception.TableAlreadyExistsException)

Example 35 with ConnectorException

use of com.netflix.metacat.common.server.connectors.exception.ConnectorException in project metacat by Netflix.

the class PolarisConnectorTableService method list.

/**
 * {@inheritDoc}.
 */
@Override
public List<TableInfo> list(final ConnectorRequestContext requestContext, final QualifiedName name, @Nullable final QualifiedName prefix, @Nullable final Sort sort, @Nullable final Pageable pageable) {
    try {
        final String tableFilter = (prefix != null && prefix.isTableDefinition()) ? prefix.getTableName() : "";
        final List<PolarisTableEntity> tbls = polarisStoreService.getTableEntities(name.getDatabaseName(), tableFilter);
        if (sort != null) {
            ConnectorUtils.sort(tbls, sort, Comparator.comparing(t -> t.getTblName()));
        }
        return ConnectorUtils.paginate(tbls, pageable).stream().map(t -> polarisTableMapper.toInfo(t)).collect(Collectors.toList());
    } catch (Exception exception) {
        final String msg = String.format("Failed polaris list tables %s using prefix %s", name, prefix);
        log.error(msg, exception);
        throw new ConnectorException(msg, exception);
    }
}
Also used : StringUtils(org.apache.commons.lang.StringUtils) Cacheable(org.springframework.cache.annotation.Cacheable) PolarisTableEntity(com.netflix.metacat.connector.polaris.store.entities.PolarisTableEntity) TablePreconditionFailedException(com.netflix.metacat.common.server.connectors.exception.TablePreconditionFailedException) DirectSqlTable(com.netflix.metacat.connector.hive.sql.DirectSqlTable) ConnectorTableService(com.netflix.metacat.common.server.connectors.ConnectorTableService) InvalidMetaException(com.netflix.metacat.common.server.connectors.exception.InvalidMetaException) Lists(com.google.common.collect.Lists) ConnectorException(com.netflix.metacat.common.server.connectors.exception.ConnectorException) DataIntegrityViolationException(org.springframework.dao.DataIntegrityViolationException) Map(java.util.Map) IcebergTableWrapper(com.netflix.metacat.connector.hive.iceberg.IcebergTableWrapper) ConnectorContext(com.netflix.metacat.common.server.connectors.ConnectorContext) HiveConnectorInfoConverter(com.netflix.metacat.connector.hive.converters.HiveConnectorInfoConverter) Config(com.netflix.metacat.common.server.properties.Config) ConnectorRequestContext(com.netflix.metacat.common.server.connectors.ConnectorRequestContext) Nullable(javax.annotation.Nullable) PolarisTableMapper(com.netflix.metacat.connector.polaris.mappers.PolarisTableMapper) MapUtils(org.apache.commons.collections.MapUtils) Pageable(com.netflix.metacat.common.dto.Pageable) QualifiedName(com.netflix.metacat.common.QualifiedName) TableNotFoundException(com.netflix.metacat.common.server.connectors.exception.TableNotFoundException) Collectors(java.util.stream.Collectors) Objects(java.util.Objects) Slf4j(lombok.extern.slf4j.Slf4j) List(java.util.List) IcebergTableHandler(com.netflix.metacat.connector.hive.iceberg.IcebergTableHandler) TableInfo(com.netflix.metacat.common.server.connectors.model.TableInfo) HiveTableUtil(com.netflix.metacat.connector.hive.util.HiveTableUtil) TableAlreadyExistsException(com.netflix.metacat.common.server.connectors.exception.TableAlreadyExistsException) ConnectorUtils(com.netflix.metacat.common.server.connectors.ConnectorUtils) Comparator(java.util.Comparator) PolarisStoreService(com.netflix.metacat.connector.polaris.store.PolarisStoreService) Sort(com.netflix.metacat.common.dto.Sort) ConnectorException(com.netflix.metacat.common.server.connectors.exception.ConnectorException) PolarisTableEntity(com.netflix.metacat.connector.polaris.store.entities.PolarisTableEntity) TablePreconditionFailedException(com.netflix.metacat.common.server.connectors.exception.TablePreconditionFailedException) InvalidMetaException(com.netflix.metacat.common.server.connectors.exception.InvalidMetaException) ConnectorException(com.netflix.metacat.common.server.connectors.exception.ConnectorException) DataIntegrityViolationException(org.springframework.dao.DataIntegrityViolationException) TableNotFoundException(com.netflix.metacat.common.server.connectors.exception.TableNotFoundException) TableAlreadyExistsException(com.netflix.metacat.common.server.connectors.exception.TableAlreadyExistsException)

Aggregations

ConnectorException (com.netflix.metacat.common.server.connectors.exception.ConnectorException)46 InvalidMetaException (com.netflix.metacat.common.server.connectors.exception.InvalidMetaException)35 QualifiedName (com.netflix.metacat.common.QualifiedName)26 TableNotFoundException (com.netflix.metacat.common.server.connectors.exception.TableNotFoundException)25 MetaException (org.apache.hadoop.hive.metastore.api.MetaException)24 TException (org.apache.thrift.TException)24 NoSuchObjectException (org.apache.hadoop.hive.metastore.api.NoSuchObjectException)20 Table (org.apache.hadoop.hive.metastore.api.Table)17 InvalidObjectException (org.apache.hadoop.hive.metastore.api.InvalidObjectException)16 Pageable (com.netflix.metacat.common.dto.Pageable)15 DatabaseNotFoundException (com.netflix.metacat.common.server.connectors.exception.DatabaseNotFoundException)15 TableAlreadyExistsException (com.netflix.metacat.common.server.connectors.exception.TableAlreadyExistsException)13 Sort (com.netflix.metacat.common.dto.Sort)12 List (java.util.List)12 Nullable (javax.annotation.Nullable)12 Lists (com.google.common.collect.Lists)11 ConnectorContext (com.netflix.metacat.common.server.connectors.ConnectorContext)11 TableInfo (com.netflix.metacat.common.server.connectors.model.TableInfo)11 ConnectorUtils (com.netflix.metacat.common.server.connectors.ConnectorUtils)10 HiveConnectorInfoConverter (com.netflix.metacat.connector.hive.converters.HiveConnectorInfoConverter)10