Search in sources :

Example 11 with MetacatRequestContext

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

the class DatabaseServiceImpl method get.

/**
 * {@inheritDoc}
 */
@Override
public DatabaseDto get(final QualifiedName name, final GetDatabaseServiceParameters getDatabaseServiceParameters) {
    validate(name);
    final MetacatRequestContext metacatRequestContext = MetacatContextManager.getContext();
    final MetacatCatalogConfig config = connectorManager.getCatalogConfig(name);
    final ConnectorDatabaseService service = connectorManager.getDatabaseService(name);
    final ConnectorTableService tableService = connectorManager.getTableService(name);
    final ConnectorRequestContext connectorRequestContext = converterUtil.toConnectorContext(metacatRequestContext);
    final DatabaseDto dto = converterUtil.toDatabaseDto(service.get(connectorRequestContext, name));
    dto.setType(config.getType());
    if (getDatabaseServiceParameters.isIncludeTableNames()) {
        final List<QualifiedName> tableNames = tableService.listNames(connectorRequestContext, name, null, null, null);
        List<QualifiedName> viewNames = Collections.emptyList();
        if (config.isIncludeViewsWithTables()) {
            // TODO JdbcMetadata returns ImmutableList.of() for views.  We should change it to fetch views.
            try {
                viewNames = service.listViewNames(connectorRequestContext, name);
            } catch (UnsupportedOperationException ignored) {
            }
        }
        // Check to see if schema exists
        if (tableNames.isEmpty() && viewNames.isEmpty() && !exists(name)) {
            throw new DatabaseNotFoundException(name);
        }
        dto.setTables(Stream.concat(tableNames.stream(), viewNames.stream()).map(QualifiedName::getTableName).sorted(String.CASE_INSENSITIVE_ORDER).collect(Collectors.toList()));
    }
    if (getDatabaseServiceParameters.isIncludeUserMetadata()) {
        log.info("Populate user metadata for schema {}", name);
        userMetadataService.populateMetadata(dto, getDatabaseServiceParameters.isDisableOnReadMetadataIntercetor());
    }
    return dto;
}
Also used : MetacatRequestContext(com.netflix.metacat.common.MetacatRequestContext) ConnectorDatabaseService(com.netflix.metacat.common.server.connectors.ConnectorDatabaseService) ConnectorTableService(com.netflix.metacat.common.server.connectors.ConnectorTableService) MetacatCatalogConfig(com.netflix.metacat.common.server.spi.MetacatCatalogConfig) DatabaseDto(com.netflix.metacat.common.dto.DatabaseDto) QualifiedName(com.netflix.metacat.common.QualifiedName) DatabaseNotFoundException(com.netflix.metacat.common.server.connectors.exception.DatabaseNotFoundException) ConnectorRequestContext(com.netflix.metacat.common.server.connectors.ConnectorRequestContext)

Example 12 with MetacatRequestContext

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

the class MViewServiceImpl method savePartitions.

/**
 * {@inheritDoc}
 */
@Override
public PartitionsSaveResponseDto savePartitions(final QualifiedName name, final PartitionsSaveRequestDto dto, final boolean merge) {
    final PartitionsSaveResponseDto result;
    final List<PartitionDto> partitionDtos = dto.getPartitions();
    if (partitionDtos == null || partitionDtos.isEmpty()) {
        return new PartitionsSaveResponseDto();
    }
    final QualifiedName viewQName = QualifiedName.ofTable(name.getCatalogName(), VIEW_DB_NAME, createViewName(name));
    partitionDtos.forEach(partitionDto -> partitionDto.setName(QualifiedName.ofPartition(viewQName.getCatalogName(), viewQName.getDatabaseName(), viewQName.getTableName(), partitionDto.getName().getPartitionName())));
    final MetacatRequestContext metacatRequestContext = MetacatContextManager.getContext();
    eventBus.postSync(new MetacatSaveMViewPartitionPreEvent(name, metacatRequestContext, this, dto));
    final List<String> partitionIdsForDeletes = dto.getPartitionIdsForDeletes();
    if (partitionIdsForDeletes != null && !partitionIdsForDeletes.isEmpty()) {
        eventBus.postSync(new MetacatDeleteMViewPartitionPreEvent(name, metacatRequestContext, this, dto));
    }
    if (merge) {
        final List<String> partitionNames = partitionDtos.stream().map(partitionDto -> partitionDto.getName().getPartitionName()).collect(Collectors.toList());
        final List<PartitionDto> existingPartitions = partitionService.list(viewQName, null, null, false, false, new GetPartitionsRequestDto(null, partitionNames, false, true));
        final Map<String, PartitionDto> existingPartitionsMap = existingPartitions.stream().collect(Collectors.toMap(partitionDto -> partitionDto.getName().getPartitionName(), Function.identity()));
        final List<PartitionDto> mergedPartitions = partitionDtos.stream().map(partitionDto -> {
            final String partitionName = partitionDto.getName().getPartitionName();
            final PartitionDto existingPartition = existingPartitionsMap.get(partitionName);
            return mergePartition(partitionDto, existingPartition);
        }).collect(Collectors.toList());
        dto.setPartitions(mergedPartitions);
        result = partitionService.save(viewQName, dto);
    } else {
        result = partitionService.save(viewQName, dto);
    }
    eventBus.postAsync(new MetacatSaveMViewPartitionPostEvent(name, metacatRequestContext, this, dto.getPartitions()));
    if (partitionIdsForDeletes != null && !partitionIdsForDeletes.isEmpty()) {
        eventBus.postAsync(new MetacatDeleteMViewPartitionPostEvent(name, metacatRequestContext, this, partitionIdsForDeletes));
    }
    return result;
}
Also used : MViewService(com.netflix.metacat.main.services.MViewService) MetacatCreateMViewPreEvent(com.netflix.metacat.common.server.events.MetacatCreateMViewPreEvent) UserMetadataService(com.netflix.metacat.common.server.usermetadata.UserMetadataService) TableDto(com.netflix.metacat.common.dto.TableDto) MetacatDeleteMViewPreEvent(com.netflix.metacat.common.server.events.MetacatDeleteMViewPreEvent) MetacatEventBus(com.netflix.metacat.common.server.events.MetacatEventBus) MetacatSaveMViewPartitionPreEvent(com.netflix.metacat.common.server.events.MetacatSaveMViewPartitionPreEvent) ConverterUtil(com.netflix.metacat.common.server.converter.ConverterUtil) MetacatContextManager(com.netflix.metacat.common.server.util.MetacatContextManager) MetacatDeleteMViewPostEvent(com.netflix.metacat.common.server.events.MetacatDeleteMViewPostEvent) PartitionService(com.netflix.metacat.main.services.PartitionService) MetacatUpdateMViewPostEvent(com.netflix.metacat.common.server.events.MetacatUpdateMViewPostEvent) StorageDto(com.netflix.metacat.common.dto.StorageDto) Function(java.util.function.Function) ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) PartitionsSaveResponseDto(com.netflix.metacat.common.dto.PartitionsSaveResponseDto) ConnectorTableService(com.netflix.metacat.common.server.connectors.ConnectorTableService) Lists(com.google.common.collect.Lists) TableService(com.netflix.metacat.main.services.TableService) Map(java.util.Map) MetacatCreateMViewPostEvent(com.netflix.metacat.common.server.events.MetacatCreateMViewPostEvent) MetacatRequestContext(com.netflix.metacat.common.MetacatRequestContext) PartitionsSaveRequestDto(com.netflix.metacat.common.dto.PartitionsSaveRequestDto) MetacatDeleteMViewPartitionPreEvent(com.netflix.metacat.common.server.events.MetacatDeleteMViewPartitionPreEvent) NotFoundException(com.netflix.metacat.common.server.connectors.exception.NotFoundException) ConnectorRequestContext(com.netflix.metacat.common.server.connectors.ConnectorRequestContext) Nullable(javax.annotation.Nullable) MetacatDeleteMViewPartitionPostEvent(com.netflix.metacat.common.server.events.MetacatDeleteMViewPartitionPostEvent) NameDateDto(com.netflix.metacat.common.NameDateDto) GetPartitionsRequestDto(com.netflix.metacat.common.dto.GetPartitionsRequestDto) MetacatUpdateMViewPreEvent(com.netflix.metacat.common.server.events.MetacatUpdateMViewPreEvent) Pageable(com.netflix.metacat.common.dto.Pageable) MoreObjects(com.google.common.base.MoreObjects) MetacatSaveMViewPartitionPostEvent(com.netflix.metacat.common.server.events.MetacatSaveMViewPartitionPostEvent) QualifiedName(com.netflix.metacat.common.QualifiedName) TableNotFoundException(com.netflix.metacat.common.server.connectors.exception.TableNotFoundException) Collectors(java.util.stream.Collectors) Slf4j(lombok.extern.slf4j.Slf4j) List(java.util.List) PartitionDto(com.netflix.metacat.common.dto.PartitionDto) GetMetadataInterceptorParameters(com.netflix.metacat.common.server.usermetadata.GetMetadataInterceptorParameters) Optional(java.util.Optional) ConnectorManager(com.netflix.metacat.main.manager.ConnectorManager) GetTableServiceParameters(com.netflix.metacat.main.services.GetTableServiceParameters) Sort(com.netflix.metacat.common.dto.Sort) MetacatRequestContext(com.netflix.metacat.common.MetacatRequestContext) MetacatDeleteMViewPartitionPreEvent(com.netflix.metacat.common.server.events.MetacatDeleteMViewPartitionPreEvent) QualifiedName(com.netflix.metacat.common.QualifiedName) MetacatSaveMViewPartitionPreEvent(com.netflix.metacat.common.server.events.MetacatSaveMViewPartitionPreEvent) GetPartitionsRequestDto(com.netflix.metacat.common.dto.GetPartitionsRequestDto) MetacatDeleteMViewPartitionPostEvent(com.netflix.metacat.common.server.events.MetacatDeleteMViewPartitionPostEvent) PartitionDto(com.netflix.metacat.common.dto.PartitionDto) MetacatSaveMViewPartitionPostEvent(com.netflix.metacat.common.server.events.MetacatSaveMViewPartitionPostEvent) PartitionsSaveResponseDto(com.netflix.metacat.common.dto.PartitionsSaveResponseDto)

Example 13 with MetacatRequestContext

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

the class MViewServiceImpl method updateAndReturn.

/**
 * {@inheritDoc}
 */
@Override
public TableDto updateAndReturn(final QualifiedName name, final TableDto tableDto) {
    final MetacatRequestContext metacatRequestContext = MetacatContextManager.getContext();
    eventBus.postSync(new MetacatUpdateMViewPreEvent(name, metacatRequestContext, this, tableDto));
    final QualifiedName viewQName = QualifiedName.ofTable(name.getCatalogName(), VIEW_DB_NAME, createViewName(name));
    log.info("Updating view {}.", viewQName);
    tableService.update(viewQName, tableDto);
    final TableDto updatedDto = getOpt(name, GetTableServiceParameters.builder().includeInfo(true).includeDefinitionMetadata(false).includeDataMetadata(false).disableOnReadMetadataIntercetor(false).build()).orElseThrow(() -> new IllegalStateException("should exist"));
    eventBus.postAsync(new MetacatUpdateMViewPostEvent(name, metacatRequestContext, this, updatedDto));
    return updatedDto;
}
Also used : MetacatRequestContext(com.netflix.metacat.common.MetacatRequestContext) MetacatUpdateMViewPreEvent(com.netflix.metacat.common.server.events.MetacatUpdateMViewPreEvent) QualifiedName(com.netflix.metacat.common.QualifiedName) TableDto(com.netflix.metacat.common.dto.TableDto) MetacatUpdateMViewPostEvent(com.netflix.metacat.common.server.events.MetacatUpdateMViewPostEvent)

Example 14 with MetacatRequestContext

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

the class MViewServiceImpl method deleteAndReturn.

/**
 * {@inheritDoc}
 */
@Override
public TableDto deleteAndReturn(final QualifiedName name) {
    final MetacatRequestContext metacatRequestContext = MetacatContextManager.getContext();
    eventBus.postSync(new MetacatDeleteMViewPreEvent(name, metacatRequestContext, this));
    final QualifiedName viewQName = QualifiedName.ofTable(name.getCatalogName(), VIEW_DB_NAME, createViewName(name));
    log.info("Deleting view {}.", viewQName);
    final TableDto deletedDto = tableService.deleteAndReturn(viewQName, true);
    eventBus.postAsync(new MetacatDeleteMViewPostEvent(name, metacatRequestContext, this, deletedDto));
    return deletedDto;
}
Also used : MetacatRequestContext(com.netflix.metacat.common.MetacatRequestContext) MetacatDeleteMViewPreEvent(com.netflix.metacat.common.server.events.MetacatDeleteMViewPreEvent) QualifiedName(com.netflix.metacat.common.QualifiedName) MetacatDeleteMViewPostEvent(com.netflix.metacat.common.server.events.MetacatDeleteMViewPostEvent) TableDto(com.netflix.metacat.common.dto.TableDto)

Example 15 with MetacatRequestContext

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

the class PartitionServiceImpl method save.

/**
 * {@inheritDoc}
 */
@Override
public PartitionsSaveResponseDto save(final QualifiedName name, final PartitionsSaveRequestDto dto) {
    final MetacatRequestContext metacatRequestContext = MetacatContextManager.getContext();
    final ConnectorPartitionService service = connectorManager.getPartitionService(name);
    final List<PartitionDto> partitionDtos = dto.getPartitions();
    // If no partitions are passed, then return
    if (partitionDtos == null || partitionDtos.isEmpty()) {
        return new PartitionsSaveResponseDto();
    }
    if (!tableService.exists(name)) {
        throw new TableNotFoundException(name);
    }
    // optimization for metadata only updates (e.g. squirrel) , assuming only validate partitions are requested
    if (dto.getSaveMetadataOnly()) {
        return savePartitionMetadataOnly(metacatRequestContext, dto, name, partitionDtos);
    } else {
        return updatePartitions(service, metacatRequestContext, dto, name, partitionDtos);
    }
}
Also used : TableNotFoundException(com.netflix.metacat.common.server.connectors.exception.TableNotFoundException) MetacatRequestContext(com.netflix.metacat.common.MetacatRequestContext) PartitionDto(com.netflix.metacat.common.dto.PartitionDto) ConnectorPartitionService(com.netflix.metacat.common.server.connectors.ConnectorPartitionService) PartitionsSaveResponseDto(com.netflix.metacat.common.dto.PartitionsSaveResponseDto)

Aggregations

MetacatRequestContext (com.netflix.metacat.common.MetacatRequestContext)50 QualifiedName (com.netflix.metacat.common.QualifiedName)22 TableDto (com.netflix.metacat.common.dto.TableDto)20 ConnectorRequestContext (com.netflix.metacat.common.server.connectors.ConnectorRequestContext)20 TableNotFoundException (com.netflix.metacat.common.server.connectors.exception.TableNotFoundException)16 List (java.util.List)12 PartitionDto (com.netflix.metacat.common.dto.PartitionDto)11 ConnectorTableService (com.netflix.metacat.common.server.connectors.ConnectorTableService)11 ConnectorPartitionService (com.netflix.metacat.common.server.connectors.ConnectorPartitionService)10 Collectors (java.util.stream.Collectors)10 Slf4j (lombok.extern.slf4j.Slf4j)10 ObjectNode (com.fasterxml.jackson.databind.node.ObjectNode)9 Lists (com.google.common.collect.Lists)8 DatabaseDto (com.netflix.metacat.common.dto.DatabaseDto)8 GetPartitionsRequestDto (com.netflix.metacat.common.dto.GetPartitionsRequestDto)8 NotFoundException (com.netflix.metacat.common.server.connectors.exception.NotFoundException)8 Metrics (com.netflix.metacat.common.server.monitoring.Metrics)8 UserMetadataService (com.netflix.metacat.common.server.usermetadata.UserMetadataService)8 MetacatContextManager (com.netflix.metacat.common.server.util.MetacatContextManager)8 Registry (com.netflix.spectator.api.Registry)8