Search in sources :

Example 1 with MetacatCreateMViewPreEvent

use of com.netflix.metacat.common.server.events.MetacatCreateMViewPreEvent in project metacat by Netflix.

the class MViewServiceImpl method createAndSnapshotPartitions.

/**
 * Creates the materialized view using the schema of the give table
 * Assumes that the "franklinviews" database name already exists in the given catalog.
 */
@Override
public TableDto createAndSnapshotPartitions(final QualifiedName name, final boolean snapshot, @Nullable final String filter) {
    final TableDto result;
    // Get the table
    log.info("Get the table {}", name);
    final MetacatRequestContext metacatRequestContext = MetacatContextManager.getContext();
    eventBus.post(new MetacatCreateMViewPreEvent(name, metacatRequestContext, this, snapshot, filter));
    final Optional<TableDto> oTable = tableService.get(name, GetTableServiceParameters.builder().includeDataMetadata(false).includeDefinitionMetadata(false).disableOnReadMetadataIntercetor(// turn off for optimization
    true).includeInfo(true).build());
    if (oTable.isPresent()) {
        final TableDto table = oTable.get();
        final String viewName = createViewName(name);
        final QualifiedName targetName = QualifiedName.ofTable(name.getCatalogName(), VIEW_DB_NAME, viewName);
        // Get the view table if it exists
        log.info("Check if the view table {} exists.", targetName);
        Optional<TableDto> oViewTable = Optional.empty();
        try {
            // read the original view back
            oViewTable = tableService.get(targetName, GetTableServiceParameters.builder().includeDataMetadata(false).includeDefinitionMetadata(false).disableOnReadMetadataIntercetor(false).includeInfo(true).build());
        } catch (NotFoundException ignored) {
        }
        if (!oViewTable.isPresent()) {
            log.info("Creating view {}.", targetName);
            // 
            if (MetacatServiceHelper.isIcebergTable(table)) {
                table.getFields().forEach(f -> f.setSource_type(null));
            }
            result = tableService.copy(table, targetName);
        } else {
            result = oViewTable.get();
        }
        if (snapshot) {
            snapshotPartitions(name, filter);
        }
        eventBus.post(new MetacatCreateMViewPostEvent(name, metacatRequestContext, this, result, snapshot, filter));
    } else {
        throw new TableNotFoundException(name);
    }
    return result;
}
Also used : TableNotFoundException(com.netflix.metacat.common.server.connectors.exception.TableNotFoundException) MetacatRequestContext(com.netflix.metacat.common.MetacatRequestContext) QualifiedName(com.netflix.metacat.common.QualifiedName) NotFoundException(com.netflix.metacat.common.server.connectors.exception.NotFoundException) TableNotFoundException(com.netflix.metacat.common.server.connectors.exception.TableNotFoundException) TableDto(com.netflix.metacat.common.dto.TableDto) MetacatCreateMViewPostEvent(com.netflix.metacat.common.server.events.MetacatCreateMViewPostEvent) MetacatCreateMViewPreEvent(com.netflix.metacat.common.server.events.MetacatCreateMViewPreEvent)

Aggregations

MetacatRequestContext (com.netflix.metacat.common.MetacatRequestContext)1 QualifiedName (com.netflix.metacat.common.QualifiedName)1 TableDto (com.netflix.metacat.common.dto.TableDto)1 NotFoundException (com.netflix.metacat.common.server.connectors.exception.NotFoundException)1 TableNotFoundException (com.netflix.metacat.common.server.connectors.exception.TableNotFoundException)1 MetacatCreateMViewPostEvent (com.netflix.metacat.common.server.events.MetacatCreateMViewPostEvent)1 MetacatCreateMViewPreEvent (com.netflix.metacat.common.server.events.MetacatCreateMViewPreEvent)1