Search in sources :

Example 31 with ExportParameters

use of org.talend.dataprep.api.export.ExportParameters in project data-prep by Talend.

the class OptimizedExportStrategy method execute.

@Override
public StreamingResponseBody execute(ExportParameters parameters) {
    final String formatName = parameters.getExportType();
    final ExportFormat format = getFormat(formatName);
    // 
    ExportUtils.setExportHeaders(// 
    parameters.getExportName(), // 
    parameters.getArguments().get(ExportFormat.PREFIX + CSVFormat.ParametersCSV.ENCODING), format);
    return outputStream -> performOptimizedTransform(parameters, outputStream);
}
Also used : ExportFormat(org.talend.dataprep.format.export.ExportFormat) StringUtils(org.apache.commons.lang.StringUtils) TDPException(org.talend.dataprep.exception.TDPException) TransformationErrorCodes(org.talend.dataprep.exception.error.TransformationErrorCodes) LoggerFactory(org.slf4j.LoggerFactory) Autowired(org.springframework.beans.factory.annotation.Autowired) Configuration(org.talend.dataprep.transformation.api.transformer.configuration.Configuration) PreparationMessage(org.talend.dataprep.api.preparation.PreparationMessage) ArrayList(java.util.ArrayList) CacheKeyGenerator(org.talend.dataprep.cache.CacheKeyGenerator) TransformationMetadataCacheKey(org.talend.dataprep.cache.TransformationMetadataCacheKey) DataSetMetadata(org.talend.dataprep.api.dataset.DataSetMetadata) TeeOutputStream(org.apache.commons.io.output.TeeOutputStream) ExportUtils(org.talend.dataprep.transformation.service.ExportUtils) DataSet(org.talend.dataprep.api.dataset.DataSet) OutputStream(java.io.OutputStream) Preparation(org.talend.dataprep.api.preparation.Preparation) ExportParameters(org.talend.dataprep.api.export.ExportParameters) Logger(org.slf4j.Logger) BaseExportStrategy(org.talend.dataprep.transformation.service.BaseExportStrategy) JsonParser(com.fasterxml.jackson.core.JsonParser) UTF_8(java.nio.charset.StandardCharsets.UTF_8) StreamingResponseBody(org.springframework.web.servlet.mvc.method.annotation.StreamingResponseBody) IOException(java.io.IOException) Step(org.talend.dataprep.api.preparation.Step) InputStreamReader(java.io.InputStreamReader) Collectors(java.util.stream.Collectors) ContentCache(org.talend.dataprep.cache.ContentCache) CSVFormat(org.talend.dataprep.transformation.format.CSVFormat) List(java.util.List) Component(org.springframework.stereotype.Component) TransformationCacheKey(org.talend.dataprep.cache.TransformationCacheKey) InputStream(java.io.InputStream) ExportFormat(org.talend.dataprep.format.export.ExportFormat)

Example 32 with ExportParameters

use of org.talend.dataprep.api.export.ExportParameters in project data-prep by Talend.

the class PreparationExportStrategy method performPreparation.

public void performPreparation(final ExportParameters parameters, final OutputStream outputStream) {
    final String stepId = parameters.getStepId();
    final String preparationId = parameters.getPreparationId();
    final String formatName = parameters.getExportType();
    final PreparationMessage preparation = getPreparation(preparationId, stepId);
    final String dataSetId = preparation.getDataSetId();
    final ExportFormat format = getFormat(parameters.getExportType());
    // get the dataset content (in an auto-closable block to make sure it is properly closed)
    boolean releasedIdentity = false;
    // Allow get dataset and get dataset metadata access whatever share status is
    securityProxy.asTechnicalUser();
    final DataSetGet dataSetGet = applicationContext.getBean(DataSetGet.class, dataSetId, false, true);
    final DataSetGetMetadata dataSetGetMetadata = applicationContext.getBean(DataSetGetMetadata.class, dataSetId);
    try (InputStream datasetContent = dataSetGet.execute()) {
        try (JsonParser parser = mapper.getFactory().createParser(new InputStreamReader(datasetContent, UTF_8))) {
            // head is not allowed as step id
            final String version = getCleanStepId(preparation, stepId);
            // Create dataset
            final DataSet dataSet = mapper.readerFor(DataSet.class).readValue(parser);
            dataSet.setMetadata(dataSetGetMetadata.execute());
            // All good, can already release identity
            securityProxy.releaseIdentity();
            releasedIdentity = true;
            // get the actions to apply (no preparation ==> dataset export ==> no actions)
            final String actions = getActions(preparationId, version);
            final TransformationCacheKey key = // 
            cacheKeyGenerator.generateContentKey(// 
            dataSetId, // 
            preparationId, // 
            version, // 
            formatName, // 
            parameters.getFrom(), // 
            parameters.getArguments(), // 
            parameters.getFilter());
            LOGGER.debug("Cache key: " + key.getKey());
            LOGGER.debug("Cache key details: " + key.toString());
            try (final TeeOutputStream tee = new TeeOutputStream(outputStream, contentCache.put(key, ContentCache.TimeToLive.DEFAULT))) {
                final Configuration configuration = // 
                Configuration.builder().args(// 
                parameters.getArguments()).outFilter(// 
                rm -> filterService.build(parameters.getFilter(), rm)).sourceType(parameters.getFrom()).format(// 
                format.getName()).actions(// 
                actions).preparation(// 
                preparation).stepId(// 
                version).volume(// 
                Configuration.Volume.SMALL).output(// 
                tee).limit(// 
                limit).build();
                factory.get(configuration).buildExecutable(dataSet, configuration).execute();
                tee.flush();
            } catch (Throwable e) {
                // NOSONAR
                contentCache.evict(key);
                throw e;
            }
        }
    } catch (TDPException e) {
        throw e;
    } catch (Exception e) {
        throw new TDPException(TransformationErrorCodes.UNABLE_TO_TRANSFORM_DATASET, e);
    } finally {
        if (!releasedIdentity) {
            // Release identity in case of error.
            securityProxy.releaseIdentity();
        }
    }
}
Also used : ExportFormat(org.talend.dataprep.format.export.ExportFormat) StringUtils(org.apache.commons.lang.StringUtils) DataSetGet(org.talend.dataprep.command.dataset.DataSetGet) TDPException(org.talend.dataprep.exception.TDPException) TransformationErrorCodes(org.talend.dataprep.exception.error.TransformationErrorCodes) LoggerFactory(org.slf4j.LoggerFactory) Autowired(org.springframework.beans.factory.annotation.Autowired) Configuration(org.talend.dataprep.transformation.api.transformer.configuration.Configuration) PreparationMessage(org.talend.dataprep.api.preparation.PreparationMessage) CacheKeyGenerator(org.talend.dataprep.cache.CacheKeyGenerator) TeeOutputStream(org.apache.commons.io.output.TeeOutputStream) ExportUtils(org.talend.dataprep.transformation.service.ExportUtils) DataSet(org.talend.dataprep.api.dataset.DataSet) OutputStream(java.io.OutputStream) ExportParameters(org.talend.dataprep.api.export.ExportParameters) Logger(org.slf4j.Logger) HEAD(org.talend.dataprep.api.export.ExportParameters.SourceType.HEAD) BaseExportStrategy(org.talend.dataprep.transformation.service.BaseExportStrategy) JsonParser(com.fasterxml.jackson.core.JsonParser) UTF_8(java.nio.charset.StandardCharsets.UTF_8) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) StreamingResponseBody(org.springframework.web.servlet.mvc.method.annotation.StreamingResponseBody) InputStreamReader(java.io.InputStreamReader) ContentCache(org.talend.dataprep.cache.ContentCache) SecurityProxy(org.talend.dataprep.security.SecurityProxy) CSVFormat(org.talend.dataprep.transformation.format.CSVFormat) Component(org.springframework.stereotype.Component) TransformationCacheKey(org.talend.dataprep.cache.TransformationCacheKey) DataSetGetMetadata(org.talend.dataprep.command.dataset.DataSetGetMetadata) InputStream(java.io.InputStream) DataSetGet(org.talend.dataprep.command.dataset.DataSetGet) TeeOutputStream(org.apache.commons.io.output.TeeOutputStream) InputStreamReader(java.io.InputStreamReader) Configuration(org.talend.dataprep.transformation.api.transformer.configuration.Configuration) DataSet(org.talend.dataprep.api.dataset.DataSet) InputStream(java.io.InputStream) ExportFormat(org.talend.dataprep.format.export.ExportFormat) DataSetGetMetadata(org.talend.dataprep.command.dataset.DataSetGetMetadata) TDPException(org.talend.dataprep.exception.TDPException) TransformationCacheKey(org.talend.dataprep.cache.TransformationCacheKey) TDPException(org.talend.dataprep.exception.TDPException) PreparationMessage(org.talend.dataprep.api.preparation.PreparationMessage) JsonParser(com.fasterxml.jackson.core.JsonParser)

Example 33 with ExportParameters

use of org.talend.dataprep.api.export.ExportParameters in project data-prep by Talend.

the class SetStepRowMetadata method setStepRowMetadata.

/**
 * Update the given preparation's steps with row metadata.
 *
 * @param preparation the preparation.
 * @param currentProcessingNumber the number of the current preparation.
 * @param total the total number of preparation.
 */
private void setStepRowMetadata(PersistentPreparation preparation, AtomicLong currentProcessingNumber, long total) {
    LOGGER.info("[{}/{}] preparation #{} migration starting...", currentProcessingNumber.addAndGet(1), total, preparation.getId());
    // Check if preparation is accessible to end user
    final Folder folder = folderRepository.locateEntry(preparation.id(), PREPARATION);
    if (folder == null) {
        LOGGER.warn("Preparation {} does not belong to a folder, skip migration (not accessible to user).", preparation.getName());
        return;
    }
    // Run preparation
    final ExportParameters exportParameters = new ExportParameters();
    exportParameters.setPreparationId(preparation.getId());
    exportParameters.setDatasetId(preparation.getDataSetId());
    exportParameters.setExportType("JSON");
    // just process the preparation, the transformation service will automatically update the steps with row metadata
    try (NullOutputStream outputStream = new NullOutputStream()) {
        service.execute(exportParameters).writeTo(outputStream);
    } catch (Throwable e) {
        LOGGER.warn("Error processing preparation {} (#{}), semantic categories are not properly stored and may change if you change them using the data quality command line", preparation.getName(), preparation.getId());
        LOGGER.debug("Here is the stacktrace", e);
    }
    LOGGER.info("[{}/{}] preparation #{} done", currentProcessingNumber.get(), total, preparation.getId());
}
Also used : ExportParameters(org.talend.dataprep.api.export.ExportParameters) Folder(org.talend.dataprep.api.folder.Folder) NullOutputStream(org.apache.commons.io.output.NullOutputStream)

Example 34 with ExportParameters

use of org.talend.dataprep.api.export.ExportParameters in project data-prep by Talend.

the class PreparationExportStrategyTest method shouldUsedHeadPreparation.

@Test
public void shouldUsedHeadPreparation() throws IOException {
    // Given
    final ExportParameters parameters = new ExportParameters();
    parameters.setExportType("JSON");
    parameters.setPreparationId("prep-1234");
    parameters.setStepId("head");
    final Preparation preparation = new Preparation();
    preparation.setId("prep-1234");
    preparation.setHeadId("head");
    configurePreparation(preparation, "prep-1234", "head");
    // When
    final StreamingResponseBody body = strategy.execute(parameters);
    body.writeTo(new NullOutputStream());
    // Then
    final ArgumentCaptor<Configuration> captor = ArgumentCaptor.forClass(Configuration.class);
    verify(transformer).buildExecutable(any(), captor.capture());
    assertEquals("prep-1234", captor.getValue().getPreparationId());
    assertEquals("head", captor.getValue().getPreparation().getHeadId());
}
Also used : StreamingResponseBody(org.springframework.web.servlet.mvc.method.annotation.StreamingResponseBody) Configuration(org.talend.dataprep.transformation.api.transformer.configuration.Configuration) ExportParameters(org.talend.dataprep.api.export.ExportParameters) Preparation(org.talend.dataprep.api.preparation.Preparation) NullOutputStream(org.apache.commons.io.output.NullOutputStream) Test(org.junit.Test)

Example 35 with ExportParameters

use of org.talend.dataprep.api.export.ExportParameters in project data-prep by Talend.

the class PreparationExportStrategyTest method shouldAcceptParameters.

@Test
public void shouldAcceptParameters() {
    // Then
    final ExportParameters parameters = new ExportParameters();
    parameters.setContent(null);
    parameters.setFrom(null);
    parameters.setFrom(ExportParameters.SourceType.HEAD);
    parameters.setPreparationId("prep-1234");
    parameters.setDatasetId("");
    assertTrue(strategy.accept(parameters));
}
Also used : ExportParameters(org.talend.dataprep.api.export.ExportParameters) Test(org.junit.Test)

Aggregations

ExportParameters (org.talend.dataprep.api.export.ExportParameters)40 Test (org.junit.Test)23 StreamingResponseBody (org.springframework.web.servlet.mvc.method.annotation.StreamingResponseBody)11 TDPException (org.talend.dataprep.exception.TDPException)11 Preparation (org.talend.dataprep.api.preparation.Preparation)10 DataSet (org.talend.dataprep.api.dataset.DataSet)9 TransformationCacheKey (org.talend.dataprep.cache.TransformationCacheKey)9 OutputStream (java.io.OutputStream)8 Configuration (org.talend.dataprep.transformation.api.transformer.configuration.Configuration)8 TransformationServiceBaseTest (org.talend.dataprep.transformation.service.TransformationServiceBaseTest)8 JsonParser (com.fasterxml.jackson.core.JsonParser)7 InputStream (java.io.InputStream)7 StringUtils (org.apache.commons.lang.StringUtils)7 Component (org.springframework.stereotype.Component)7 ServiceBaseTest (org.talend.ServiceBaseTest)7 ExportFormat (org.talend.dataprep.format.export.ExportFormat)7 CSVFormat (org.talend.dataprep.transformation.format.CSVFormat)7 BaseExportStrategy (org.talend.dataprep.transformation.service.BaseExportStrategy)7 ExportUtils (org.talend.dataprep.transformation.service.ExportUtils)7 InputStreamReader (java.io.InputStreamReader)6