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