Search in sources :

Example 1 with PartitionsSaveResponseDto

use of com.netflix.metacat.common.dto.PartitionsSaveResponseDto in project metacat by Netflix.

the class PartitionV1Resource method savePartitions.

/**
     * {@inheritDoc}
     */
@Override
public PartitionsSaveResponseDto savePartitions(final String catalogName, final String databaseName, final String tableName, final String viewName, final PartitionsSaveRequestDto partitionsSaveRequestDto) {
    final QualifiedName name = requestWrapper.qualifyName(() -> QualifiedName.ofView(catalogName, databaseName, tableName, viewName));
    return requestWrapper.processRequest(name, "saveMViewPartition", () -> {
        final PartitionsSaveResponseDto result;
        if (partitionsSaveRequestDto == null || partitionsSaveRequestDto.getPartitions() == null || partitionsSaveRequestDto.getPartitions().isEmpty()) {
            result = new PartitionsSaveResponseDto();
        } else {
            result = mViewService.savePartitions(name, partitionsSaveRequestDto, true);
            // This metadata is actually for the view, if it is present update that
            if (partitionsSaveRequestDto.getDefinitionMetadata() != null || partitionsSaveRequestDto.getDataMetadata() != null) {
                final TableDto dto = v1.getMView(catalogName, databaseName, tableName, viewName);
                dto.setDefinitionMetadata(partitionsSaveRequestDto.getDefinitionMetadata());
                dto.setDataMetadata(partitionsSaveRequestDto.getDataMetadata());
                v1.updateMView(catalogName, databaseName, tableName, viewName, dto);
            }
        }
        return result;
    });
}
Also used : QualifiedName(com.netflix.metacat.common.QualifiedName) TableDto(com.netflix.metacat.common.dto.TableDto) PartitionsSaveResponseDto(com.netflix.metacat.common.dto.PartitionsSaveResponseDto)

Example 2 with PartitionsSaveResponseDto

use of com.netflix.metacat.common.dto.PartitionsSaveResponseDto in project metacat by Netflix.

the class MViewServiceImpl method savePartitions.

@Override
public PartitionsSaveResponseDto savePartitions(@Nonnull final QualifiedName name, final PartitionsSaveRequestDto dto, final boolean merge) {
    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, partitionNames, null, null, false, false, false);
        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) ConnectorContext(com.netflix.metacat.common.server.connectors.ConnectorContext) NotFoundException(com.netflix.metacat.common.server.connectors.exception.NotFoundException) Nonnull(javax.annotation.Nonnull) MetacatDeleteMViewPartitionPostEvent(com.netflix.metacat.common.server.events.MetacatDeleteMViewPartitionPostEvent) NameDateDto(com.netflix.metacat.common.NameDateDto) 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) Optional(java.util.Optional) ConnectorManager(com.netflix.metacat.main.manager.ConnectorManager) 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) 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 3 with PartitionsSaveResponseDto

use of com.netflix.metacat.common.dto.PartitionsSaveResponseDto in project metacat by Netflix.

the class PartitionServiceImpl method save.

@Override
public PartitionsSaveResponseDto save(@Nonnull final QualifiedName name, final PartitionsSaveRequestDto dto) {
    PartitionsSaveResponseDto result = new PartitionsSaveResponseDto();
    final MetacatRequestContext metacatRequestContext = MetacatContextManager.getContext();
    final ConnectorContext connectorContext = converterUtil.toConnectorContext(metacatRequestContext);
    final ConnectorPartitionService service = connectorManager.getPartitionService(name.getCatalogName());
    final List<PartitionDto> partitionDtos = dto.getPartitions();
    // If no partitions are passed, then return
    if (partitionDtos == null || partitionDtos.isEmpty()) {
        return result;
    }
    final List<String> partitionIdsForDeletes = dto.getPartitionIdsForDeletes();
    registry.gauge(this.partitionAddedCountId.withTags(new HashMap<>(name.parts())), partitionDtos.size());
    if (!tableService.exists(name)) {
        throw new TableNotFoundException(name);
    }
    List<HasMetadata> deletePartitions = Lists.newArrayList();
    if (partitionIdsForDeletes != null && !partitionIdsForDeletes.isEmpty()) {
        eventBus.postSync(new MetacatDeleteTablePartitionPreEvent(name, metacatRequestContext, this, dto));
        registry.gauge(this.partitionDeletedCountId.withTags(new HashMap<>(name.parts())), partitionIdsForDeletes.size());
        final GetPartitionsRequestDto requestDto = new GetPartitionsRequestDto();
        requestDto.setIncludePartitionDetails(false);
        requestDto.setPartitionNames(partitionIdsForDeletes);
        final List<PartitionInfo> deletePartitionInfos = service.getPartitions(connectorContext, name, converterUtil.toPartitionListRequest(requestDto, null, null));
        if (deletePartitionInfos != null) {
            deletePartitions = deletePartitionInfos.stream().map(converterUtil::toPartitionDto).collect(Collectors.toList());
        }
    }
    //
    // Save all the new and updated partitions
    //
    eventBus.postSync(new MetacatSaveTablePartitionPreEvent(name, metacatRequestContext, this, dto));
    log.info("Saving partitions({}) for {}", partitionDtos.size(), name);
    result = converterUtil.toPartitionsSaveResponseDto(service.savePartitions(connectorContext, name, converterUtil.toPartitionsSaveRequest(dto)));
    // Save metadata
    log.info("Saving user metadata for partitions for {}", name);
    // delete metadata
    if (!deletePartitions.isEmpty()) {
        log.info("Deleting user metadata for partitions with names {} for {}", partitionIdsForDeletes, name);
        deleteMetadatas(metacatRequestContext.getUserName(), deletePartitions);
    }
    userMetadataService.saveMetadatas(metacatRequestContext.getUserName(), partitionDtos, true);
    eventBus.postAsync(new MetacatSaveTablePartitionPostEvent(name, metacatRequestContext, this, partitionDtos, result));
    if (partitionIdsForDeletes != null && !partitionIdsForDeletes.isEmpty()) {
        eventBus.postAsync(new MetacatDeleteTablePartitionPostEvent(name, metacatRequestContext, this, partitionIdsForDeletes));
    }
    return result;
}
Also used : MetacatRequestContext(com.netflix.metacat.common.MetacatRequestContext) HasMetadata(com.netflix.metacat.common.dto.HasMetadata) HashMap(java.util.HashMap) MetacatSaveTablePartitionPreEvent(com.netflix.metacat.common.server.events.MetacatSaveTablePartitionPreEvent) GetPartitionsRequestDto(com.netflix.metacat.common.dto.GetPartitionsRequestDto) MetacatDeleteTablePartitionPreEvent(com.netflix.metacat.common.server.events.MetacatDeleteTablePartitionPreEvent) MetacatDeleteTablePartitionPostEvent(com.netflix.metacat.common.server.events.MetacatDeleteTablePartitionPostEvent) TableNotFoundException(com.netflix.metacat.common.server.connectors.exception.TableNotFoundException) MetacatSaveTablePartitionPostEvent(com.netflix.metacat.common.server.events.MetacatSaveTablePartitionPostEvent) ConnectorContext(com.netflix.metacat.common.server.connectors.ConnectorContext) PartitionDto(com.netflix.metacat.common.dto.PartitionDto) ConnectorPartitionService(com.netflix.metacat.common.server.connectors.ConnectorPartitionService) PartitionInfo(com.netflix.metacat.common.server.connectors.model.PartitionInfo) PartitionsSaveResponseDto(com.netflix.metacat.common.dto.PartitionsSaveResponseDto)

Example 4 with PartitionsSaveResponseDto

use of com.netflix.metacat.common.dto.PartitionsSaveResponseDto in project metacat by Netflix.

the class MetacatServiceHelper method postPostUpdateEvent.

/**
     * Calls the right method of the event bus for the given qualified name.
     *
     * @param name                  name
     * @param metacatRequestContext context
     * @param oldDTo                dto
     * @param currentDto            dto
     */
public void postPostUpdateEvent(final QualifiedName name, final MetacatRequestContext metacatRequestContext, final BaseDto oldDTo, final BaseDto currentDto) {
    if (name.isPartitionDefinition()) {
        final List<PartitionDto> dtos = Lists.newArrayList();
        if (currentDto != null) {
            dtos.add((PartitionDto) currentDto);
        }
        // This request neither added nor updated partitions
        final PartitionsSaveResponseDto partitionsSaveResponseDto = new PartitionsSaveResponseDto();
        this.eventBus.postAsync(new MetacatSaveTablePartitionPostEvent(name, metacatRequestContext, this, dtos, partitionsSaveResponseDto));
    } else if (name.isTableDefinition()) {
        final MetacatUpdateTablePostEvent event = new MetacatUpdateTablePostEvent(name, metacatRequestContext, this, (TableDto) oldDTo, (TableDto) currentDto);
        this.eventBus.postAsync(event);
    } else if (name.isDatabaseDefinition()) {
        this.eventBus.postAsync(new MetacatUpdateDatabasePostEvent(name, metacatRequestContext, this));
    } else {
        throw new IllegalArgumentException(String.format("Invalid name %s", name));
    }
}
Also used : MetacatUpdateTablePostEvent(com.netflix.metacat.common.server.events.MetacatUpdateTablePostEvent) MetacatSaveTablePartitionPostEvent(com.netflix.metacat.common.server.events.MetacatSaveTablePartitionPostEvent) MetacatUpdateDatabasePostEvent(com.netflix.metacat.common.server.events.MetacatUpdateDatabasePostEvent) PartitionDto(com.netflix.metacat.common.dto.PartitionDto) TableDto(com.netflix.metacat.common.dto.TableDto) PartitionsSaveResponseDto(com.netflix.metacat.common.dto.PartitionsSaveResponseDto)

Aggregations

PartitionsSaveResponseDto (com.netflix.metacat.common.dto.PartitionsSaveResponseDto)4 PartitionDto (com.netflix.metacat.common.dto.PartitionDto)3 TableDto (com.netflix.metacat.common.dto.TableDto)3 MetacatRequestContext (com.netflix.metacat.common.MetacatRequestContext)2 QualifiedName (com.netflix.metacat.common.QualifiedName)2 ConnectorContext (com.netflix.metacat.common.server.connectors.ConnectorContext)2 TableNotFoundException (com.netflix.metacat.common.server.connectors.exception.TableNotFoundException)2 MetacatSaveTablePartitionPostEvent (com.netflix.metacat.common.server.events.MetacatSaveTablePartitionPostEvent)2 ObjectNode (com.fasterxml.jackson.databind.node.ObjectNode)1 MoreObjects (com.google.common.base.MoreObjects)1 Lists (com.google.common.collect.Lists)1 NameDateDto (com.netflix.metacat.common.NameDateDto)1 GetPartitionsRequestDto (com.netflix.metacat.common.dto.GetPartitionsRequestDto)1 HasMetadata (com.netflix.metacat.common.dto.HasMetadata)1 Pageable (com.netflix.metacat.common.dto.Pageable)1 PartitionsSaveRequestDto (com.netflix.metacat.common.dto.PartitionsSaveRequestDto)1 Sort (com.netflix.metacat.common.dto.Sort)1 StorageDto (com.netflix.metacat.common.dto.StorageDto)1 ConnectorPartitionService (com.netflix.metacat.common.server.connectors.ConnectorPartitionService)1 ConnectorTableService (com.netflix.metacat.common.server.connectors.ConnectorTableService)1