Search in sources :

Example 1 with Aggregator

use of org.talend.dataprep.transformation.aggregation.operation.Aggregator in project data-prep by Talend.

the class AggregationService method aggregate.

/**
 * Process an aggregation.
 *
 * @param parameters the aggregation parameters.
 * @param dataset the dataset input.
 * @return the aggregation result.
 */
public AggregationResult aggregate(AggregationParameters parameters, DataSet dataset) {
    // check the parameters
    if (parameters.getOperations().isEmpty() || parameters.getGroupBy().isEmpty()) {
        throw new TDPException(CommonErrorCodes.BAD_AGGREGATION_PARAMETERS);
    }
    AggregationResult result = new AggregationResult(parameters.getOperations().get(0).getOperator());
    // get the aggregator
    Aggregator aggregator = factory.get(parameters);
    // Build optional filter
    final DataSetMetadata metadata = dataset.getMetadata();
    final RowMetadata rowMetadata = metadata != null ? metadata.getRowMetadata() : new RowMetadata();
    final Predicate<DataSetRow> filter = filterService.build(parameters.getFilter(), rowMetadata);
    // process the dataset
    dataset.getRecords().filter(filter).forEach(row -> aggregator.accept(row, result));
    // Normalize result (perform clean / optimization now that all input was processed).
    aggregator.normalize(result);
    return result;
}
Also used : TDPException(org.talend.dataprep.exception.TDPException) AggregationResult(org.talend.dataprep.transformation.aggregation.api.AggregationResult) Aggregator(org.talend.dataprep.transformation.aggregation.operation.Aggregator) RowMetadata(org.talend.dataprep.api.dataset.RowMetadata) DataSetMetadata(org.talend.dataprep.api.dataset.DataSetMetadata) DataSetRow(org.talend.dataprep.api.dataset.row.DataSetRow)

Aggregations

DataSetMetadata (org.talend.dataprep.api.dataset.DataSetMetadata)1 RowMetadata (org.talend.dataprep.api.dataset.RowMetadata)1 DataSetRow (org.talend.dataprep.api.dataset.row.DataSetRow)1 TDPException (org.talend.dataprep.exception.TDPException)1 AggregationResult (org.talend.dataprep.transformation.aggregation.api.AggregationResult)1 Aggregator (org.talend.dataprep.transformation.aggregation.operation.Aggregator)1