Search in sources :

Example 21 with FieldsDescriptor

use of org.apache.apex.malhar.lib.appdata.schemas.FieldsDescriptor in project apex-malhar by apache.

the class AggregatorAverage method aggregate.

@Override
public GPOMutable aggregate(GPOMutable... aggregates) {
    Preconditions.checkArgument(aggregates.length == getChildAggregators().size(), "The number of arguments " + aggregates.length + " should be the same as the number of child aggregators " + getChildAggregators().size());
    GPOMutable sumAggregation = aggregates[SUM_INDEX];
    GPOMutable countAggregation = aggregates[COUNT_INDEX];
    FieldsDescriptor fieldsDescriptor = sumAggregation.getFieldDescriptor();
    Fields fields = fieldsDescriptor.getFields();
    GPOMutable result = new GPOMutable(AggregatorUtils.getOutputFieldsDescriptor(fields, this));
    long count = countAggregation.getFieldsLong()[0];
    for (String field : fields.getFields()) {
        Type type = sumAggregation.getFieldDescriptor().getType(field);
        switch(type) {
            case BYTE:
                {
                    double val = ((double) sumAggregation.getFieldByte(field)) / ((double) count);
                    result.setField(field, val);
                    break;
                }
            case SHORT:
                {
                    double val = ((double) sumAggregation.getFieldShort(field)) / ((double) count);
                    result.setField(field, val);
                    break;
                }
            case INTEGER:
                {
                    double val = ((double) sumAggregation.getFieldInt(field)) / ((double) count);
                    result.setField(field, val);
                    break;
                }
            case LONG:
                {
                    double val = ((double) sumAggregation.getFieldLong(field)) / ((double) count);
                    result.setField(field, val);
                    break;
                }
            case FLOAT:
                {
                    double val = sumAggregation.getFieldFloat(field) / ((double) count);
                    result.setField(field, val);
                    break;
                }
            case DOUBLE:
                {
                    double val = sumAggregation.getFieldDouble(field) / ((double) count);
                    result.setField(field, val);
                    break;
                }
            default:
                {
                    throw new UnsupportedOperationException("The type " + type + " is not supported.");
                }
        }
    }
    return result;
}
Also used : Type(org.apache.apex.malhar.lib.appdata.schemas.Type) Fields(org.apache.apex.malhar.lib.appdata.schemas.Fields) GPOMutable(org.apache.apex.malhar.lib.appdata.gpo.GPOMutable) FieldsDescriptor(org.apache.apex.malhar.lib.appdata.schemas.FieldsDescriptor)

Example 22 with FieldsDescriptor

use of org.apache.apex.malhar.lib.appdata.schemas.FieldsDescriptor in project apex-malhar by apache.

the class AggregatorUtils method getOutputFieldsDescriptor.

public static FieldsDescriptor getOutputFieldsDescriptor(FieldsDescriptor inputFieldsDescriptor, CompositeAggregator compositeAggregator) {
    Map<String, Type> fieldToType = Maps.newHashMap();
    Map<String, Serde> fieldToSerde = Maps.newHashMap();
    for (Map.Entry<String, Type> entry : inputFieldsDescriptor.getFieldToType().entrySet()) {
        String fieldName = entry.getKey();
        Type outputType = compositeAggregator.getOutputType();
        fieldToType.put(fieldName, outputType);
        fieldToSerde.put(fieldName, SerdeMapPrimitive.INSTANCE);
    }
    return new FieldsDescriptor(fieldToType, fieldToSerde);
}
Also used : Serde(org.apache.apex.malhar.lib.appdata.gpo.Serde) Type(org.apache.apex.malhar.lib.appdata.schemas.Type) FieldsDescriptor(org.apache.apex.malhar.lib.appdata.schemas.FieldsDescriptor) Map(java.util.Map)

Example 23 with FieldsDescriptor

use of org.apache.apex.malhar.lib.appdata.schemas.FieldsDescriptor in project apex-malhar by apache.

the class AggregatorUtils method getOutputFieldsDescriptor.

/**
 * This is a helper method which takes a {@link FieldsDescriptor} object, which defines the types of the fields
 * that the {@link IncrementalAggregator} receives as input. It then uses the given {@link IncrementalAggregator}
 * and {@link FieldsDescriptor} object to compute the {@link FieldsDescriptor} object for the aggregation produced
 * byte the given
 * {@link IncrementalAggregator} when it receives an input corresponding to the given input {@link FieldsDescriptor}.
 *
 * @param inputFieldsDescriptor This is a {@link FieldsDescriptor} object which defines the names and types of input
 *                              data recieved by an aggregator.
 * @param incrementalAggregator This is the
 * {@link IncrementalAggregator} for which an output {@link FieldsDescriptor} needs
 *                              to be computed.
 * @return The output {@link FieldsDescriptor} for this aggregator when it receives input data with the same schema as
 * the specified input {@link FieldsDescriptor}.
 */
public static FieldsDescriptor getOutputFieldsDescriptor(FieldsDescriptor inputFieldsDescriptor, IncrementalAggregator incrementalAggregator) {
    Map<String, Type> fieldToType = Maps.newHashMap();
    for (Map.Entry<String, Type> entry : inputFieldsDescriptor.getFieldToType().entrySet()) {
        String fieldName = entry.getKey();
        Type fieldType = entry.getValue();
        Type outputType = incrementalAggregator.getOutputType(fieldType);
        fieldToType.put(fieldName, outputType);
    }
    return new FieldsDescriptor(fieldToType);
}
Also used : Type(org.apache.apex.malhar.lib.appdata.schemas.Type) FieldsDescriptor(org.apache.apex.malhar.lib.appdata.schemas.FieldsDescriptor) Map(java.util.Map)

Example 24 with FieldsDescriptor

use of org.apache.apex.malhar.lib.appdata.schemas.FieldsDescriptor in project apex-malhar by apache.

the class AggregatorUtils method getOutputFieldsDescriptor.

/**
 * This is a utility method which creates an output {@link FieldsDescriptor} using the field names
 * from the given {@link FieldsDescriptor} and the output type of the given {@link OTFAggregator}.
 *
 * @param inputFieldsDescriptor The {@link FieldsDescriptor} from which to derive the field names used
 *                              for the output fields descriptor.
 * @param otfAggregator         The {@link OTFAggregator} to use for creating the output {@link FieldsDescriptor}.
 * @return The output {@link FieldsDescriptor}.
 */
public static FieldsDescriptor getOutputFieldsDescriptor(FieldsDescriptor inputFieldsDescriptor, OTFAggregator otfAggregator) {
    Map<String, Type> fieldToType = Maps.newHashMap();
    for (Map.Entry<String, Type> entry : inputFieldsDescriptor.getFieldToType().entrySet()) {
        String fieldName = entry.getKey();
        Type outputType = otfAggregator.getOutputType();
        fieldToType.put(fieldName, outputType);
    }
    return new FieldsDescriptor(fieldToType);
}
Also used : Type(org.apache.apex.malhar.lib.appdata.schemas.Type) FieldsDescriptor(org.apache.apex.malhar.lib.appdata.schemas.FieldsDescriptor) Map(java.util.Map)

Aggregations

FieldsDescriptor (org.apache.apex.malhar.lib.appdata.schemas.FieldsDescriptor)24 Type (org.apache.apex.malhar.lib.appdata.schemas.Type)19 Test (org.junit.Test)10 Map (java.util.Map)5 GPOMutable (org.apache.apex.malhar.lib.appdata.gpo.GPOMutable)4 MutableInt (org.apache.commons.lang3.mutable.MutableInt)4 JSONObject (org.codehaus.jettison.json.JSONObject)3 PreparedStatement (java.sql.PreparedStatement)2 SQLException (java.sql.SQLException)2 ArrayList (java.util.ArrayList)2 List (java.util.List)2 EventKey (org.apache.apex.malhar.lib.dimensions.DimensionsEvent.EventKey)2 Int2ObjectMap (it.unimi.dsi.fastutil.ints.Int2ObjectMap)1 Set (java.util.Set)1 Serde (org.apache.apex.malhar.lib.appdata.gpo.Serde)1 DimensionalConfigurationSchema (org.apache.apex.malhar.lib.appdata.schemas.DimensionalConfigurationSchema)1 Fields (org.apache.apex.malhar.lib.appdata.schemas.Fields)1