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;
}
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);
}
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);
}
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);
}
Aggregations