Search in sources :

Example 21 with GPOMutable

use of org.apache.apex.malhar.lib.appdata.gpo.GPOMutable in project apex-malhar by apache.

the class AbstractTopBottomAggregator method aggregate.

/**
 * The result keep a list of object for each aggregate value
 * The value of resultAggregate should keep a list of inputEventKey(the value can be get from cache or load) or a map
 * from inputEventKey to the value instead of just a list of aggregate value. As the value could be changed in
 * current window, and this change should be applied.
 *
 * precondition: resultAggregate.eventKey matches with inputSubEventKeys
 * notes: this algorithm only support TOP for positive values and BOTTOM for negative values
 */
@Override
public void aggregate(Aggregate resultAggregate, Set<EventKey> inputSubEventKeys, Map<EventKey, Aggregate> inputAggregatesRepo) {
    // there are problem for composite's value field descriptor, just ignore now.
    GPOMutable resultGpo = resultAggregate.getAggregates();
    final List<String> compositeFieldList = resultAggregate.getEventKey().getKey().getFieldDescriptor().getFieldList();
    // Map<EventKey, Aggregate> existedSubEventKeyToAggregate = Maps.newHashMap();
    for (String valueField : resultGpo.getFieldDescriptor().getFieldList()) {
        // the resultGpo keep a list of sub aggregates
        updateAggregate(resultAggregate, valueField, inputSubEventKeys, inputAggregatesRepo);
        // compare the existed sub aggregates with the new input aggregates to update the list
        for (EventKey eventKey : inputSubEventKeys) {
            aggregate(compositeFieldList, resultGpo, eventKey, inputAggregatesRepo.get(eventKey).getAggregates());
        }
    }
}
Also used : GPOMutable(org.apache.apex.malhar.lib.appdata.gpo.GPOMutable) EventKey(org.apache.apex.malhar.lib.dimensions.DimensionsEvent.EventKey)

Example 22 with GPOMutable

use of org.apache.apex.malhar.lib.appdata.gpo.GPOMutable 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 23 with GPOMutable

use of org.apache.apex.malhar.lib.appdata.gpo.GPOMutable in project apex-malhar by apache.

the class AggregatorCount method getGroup.

@Override
public Aggregate getGroup(InputEvent src, int aggregatorIndex) {
    src.used = true;
    GPOMutable aggregates = new GPOMutable(context.aggregateDescriptor);
    GPOMutable keys = new GPOMutable(context.keyDescriptor);
    GPOUtils.indirectCopy(keys, src.getKeys(), context.indexSubsetKeys);
    EventKey eventKey = createEventKey(src, context, aggregatorIndex);
    long[] longFields = aggregates.getFieldsLong();
    for (int index = 0; index < longFields.length; index++) {
        longFields[index] = 0;
    }
    return new Aggregate(eventKey, aggregates);
}
Also used : GPOMutable(org.apache.apex.malhar.lib.appdata.gpo.GPOMutable) EventKey(org.apache.apex.malhar.lib.dimensions.DimensionsEvent.EventKey) Aggregate(org.apache.apex.malhar.lib.dimensions.DimensionsEvent.Aggregate)

Example 24 with GPOMutable

use of org.apache.apex.malhar.lib.appdata.gpo.GPOMutable in project apex-malhar by apache.

the class AppDataSnapshotServerPOJO method convert.

@Override
public GPOMutable convert(Object inputEvent) {
    firstTuple(inputEvent);
    GPOMutable convertedResult = new GPOMutable(schema.getValuesDescriptor());
    GPOUtils.copyPOJOToGPO(convertedResult, getters, inputEvent);
    return convertedResult;
}
Also used : GPOMutable(org.apache.apex.malhar.lib.appdata.gpo.GPOMutable)

Example 25 with GPOMutable

use of org.apache.apex.malhar.lib.appdata.gpo.GPOMutable in project apex-malhar by apache.

the class DataResultSnapshotSerializer method serializeHelper.

private String serializeHelper(Message result, ResultFormatter resultFormatter) throws Exception {
    DataResultSnapshot gResult = (DataResultSnapshot) result;
    JSONObject jo = new JSONObject();
    jo.put(Result.FIELD_ID, gResult.getId());
    jo.put(Result.FIELD_TYPE, gResult.getType());
    JSONArray ja = new JSONArray();
    for (GPOMutable value : gResult.getValues()) {
        JSONObject dataValue = GPOUtils.serializeJSONObject(value, ((DataQuerySnapshot) gResult.getQuery()).getFields(), resultFormatter);
        ja.put(dataValue);
    }
    jo.put(DataResultSnapshot.FIELD_DATA, ja);
    if (!gResult.isOneTime()) {
        jo.put(Result.FIELD_COUNTDOWN, gResult.getCountdown());
    }
    return jo.toString();
}
Also used : DataResultSnapshot(org.apache.apex.malhar.lib.appdata.schemas.DataResultSnapshot) JSONObject(org.codehaus.jettison.json.JSONObject) GPOMutable(org.apache.apex.malhar.lib.appdata.gpo.GPOMutable) JSONArray(org.codehaus.jettison.json.JSONArray)

Aggregations

GPOMutable (org.apache.apex.malhar.lib.appdata.gpo.GPOMutable)25 EventKey (org.apache.apex.malhar.lib.dimensions.DimensionsEvent.EventKey)6 FieldsDescriptor (org.apache.apex.malhar.lib.appdata.schemas.FieldsDescriptor)4 Aggregate (org.apache.apex.malhar.lib.dimensions.DimensionsEvent.Aggregate)4 SerdeListGPOMutable (org.apache.apex.malhar.lib.appdata.gpo.SerdeListGPOMutable)3 JSONObject (org.codehaus.jettison.json.JSONObject)3 Test (org.junit.Test)3 List (java.util.List)2 DataResultSnapshotSerializer (org.apache.apex.malhar.lib.appdata.query.serde.DataResultSnapshotSerializer)2 Type (org.apache.apex.malhar.lib.appdata.schemas.Type)2 JSONArray (org.codehaus.jettison.json.JSONArray)2 PreparedStatement (java.sql.PreparedStatement)1 SQLException (java.sql.SQLException)1 CustomTimeBucket (org.apache.apex.malhar.lib.appdata.schemas.CustomTimeBucket)1 DataResultSnapshot (org.apache.apex.malhar.lib.appdata.schemas.DataResultSnapshot)1 Fields (org.apache.apex.malhar.lib.appdata.schemas.Fields)1