Search in sources :

Example 1 with EventKey

use of org.apache.apex.malhar.lib.dimensions.DimensionsEvent.EventKey in project apex-malhar by apache.

the class AbstractIncrementalAggregator method createEventKey.

/**
 * Creates an {@link EventKey} from the given {@link InputEvent}.
 *
 * @param inputEvent      The {@link InputEvent} to extract an {@link EventKey} from.
 * @param context         The conversion context required to extract the {@link EventKey} from
 *                        the given {@link InputEvent}.
 * @param aggregatorIndex The aggregatorIndex to assign to this {@link InputEvent}.
 * @return The {@link EventKey} extracted from the given {@link InputEvent}.
 */
public static EventKey createEventKey(InputEvent inputEvent, DimensionsConversionContext context, int aggregatorIndex) {
    GPOMutable keys = new GPOMutable(context.keyDescriptor);
    GPOUtils.indirectCopy(keys, inputEvent.getKeys(), context.indexSubsetKeys);
    if (context.outputTimebucketIndex >= 0) {
        CustomTimeBucket timeBucket = context.dd.getCustomTimeBucket();
        keys.getFieldsInteger()[context.outputTimebucketIndex] = context.customTimeBucketRegistry.getTimeBucketId(timeBucket);
        keys.getFieldsLong()[context.outputTimestampIndex] = timeBucket.roundDown(inputEvent.getKeys().getFieldsLong()[context.inputTimestampIndex]);
    }
    EventKey eventKey = new EventKey(context.schemaID, context.dimensionsDescriptorID, context.aggregatorID, keys);
    return eventKey;
}
Also used : CustomTimeBucket(org.apache.apex.malhar.lib.appdata.schemas.CustomTimeBucket) GPOMutable(org.apache.apex.malhar.lib.appdata.gpo.GPOMutable) EventKey(org.apache.apex.malhar.lib.dimensions.DimensionsEvent.EventKey)

Example 2 with EventKey

use of org.apache.apex.malhar.lib.dimensions.DimensionsEvent.EventKey in project apex-malhar by apache.

the class DimensionsEventTest method eventKeyEqualsHashCodeTest.

@Test
public void eventKeyEqualsHashCodeTest() {
    Map<String, Type> fieldToTypeA = Maps.newHashMap();
    fieldToTypeA.put("a", Type.LONG);
    fieldToTypeA.put("b", Type.STRING);
    FieldsDescriptor fdA = new FieldsDescriptor(fieldToTypeA);
    GPOMutable gpoA = new GPOMutable(fdA);
    gpoA.setField("a", 1L);
    gpoA.setField("b", "hello");
    EventKey eventKeyA = new EventKey(1, 1, 1, gpoA);
    Map<String, Type> fieldToTypeB = Maps.newHashMap();
    fieldToTypeB.put("a", Type.LONG);
    fieldToTypeB.put("b", Type.STRING);
    FieldsDescriptor fdB = new FieldsDescriptor(fieldToTypeB);
    GPOMutable gpoB = new GPOMutable(fdB);
    gpoB.setField("a", 1L);
    gpoB.setField("b", "hello");
    EventKey eventKeyB = new EventKey(1, 1, 1, gpoB);
    Assert.assertEquals("The two hashcodes should equal", eventKeyA.hashCode(), eventKeyB.hashCode());
    Assert.assertEquals("The two event keys should equal", eventKeyA, eventKeyB);
}
Also used : Type(org.apache.apex.malhar.lib.appdata.schemas.Type) GPOMutable(org.apache.apex.malhar.lib.appdata.gpo.GPOMutable) FieldsDescriptor(org.apache.apex.malhar.lib.appdata.schemas.FieldsDescriptor) EventKey(org.apache.apex.malhar.lib.dimensions.DimensionsEvent.EventKey) Test(org.junit.Test)

Example 3 with EventKey

use of org.apache.apex.malhar.lib.dimensions.DimensionsEvent.EventKey in project apex-malhar by apache.

the class AbstractIncrementalAggregator method createAggregate.

/**
 * Creates an {@link Aggregate} from the given {@link InputEvent}.
 *
 * @param inputEvent      The {@link InputEvent} to unpack into an {@link Aggregate}.
 * @param context         The conversion context required to transform the {@link InputEvent} into
 *                        the correct {@link Aggregate}.
 * @param aggregatorIndex The aggregatorIndex assigned to this {@link Aggregate}.
 * @return The converted {@link Aggregate}.
 */
public static Aggregate createAggregate(InputEvent inputEvent, DimensionsConversionContext context, int aggregatorIndex) {
    GPOMutable aggregates = new GPOMutable(context.aggregateDescriptor);
    EventKey eventKey = createEventKey(inputEvent, context, aggregatorIndex);
    Aggregate aggregate = new Aggregate(eventKey, aggregates);
    aggregate.setAggregatorIndex(aggregatorIndex);
    return aggregate;
}
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 4 with EventKey

use of org.apache.apex.malhar.lib.dimensions.DimensionsEvent.EventKey in project apex-malhar by apache.

the class JDBCDimensionalOutputOperator method setStatementParameters.

/**
 * Sets the parameters on the {@link java.sql.PreparedStatement} based on the
 * values in the given {@link Aggregate}.
 *
 * @param aggregate
 *          The {@link Aggregate} whose values will be set on the
 *          corresponding {@link java.sql.PreparedStatement}.
 */
private void setStatementParameters(Aggregate aggregate) {
    EventKey eventKey = aggregate.getEventKey();
    int ddID = eventKey.getDimensionDescriptorID();
    int aggID = eventKey.getAggregatorID();
    LOG.info("Setting statement params {} {}", ddID, aggID);
    FieldsDescriptor keyFD = schema.getDimensionsDescriptorIDToKeyDescriptor().get(ddID);
    FieldsDescriptor aggFD = schema.getDimensionsDescriptorIDToAggregatorIDToOutputAggregatorDescriptor().get(ddID).get(aggID);
    GPOMutable key = eventKey.getKey();
    key.setFieldDescriptor(keyFD);
    GPOMutable value = aggregate.getAggregates();
    value.setFieldDescriptor(aggFD);
    int qCounter = 1;
    PreparedStatement ps = ddIDToAggIDToStatement.get(ddID).get(aggID);
    try {
        qCounter = setParams(ps, key, qCounter, true);
        setParams(ps, value, qCounter, false);
        ps.addBatch();
    } catch (SQLException ex) {
        throw new RuntimeException(ex);
    }
}
Also used : GPOMutable(org.apache.apex.malhar.lib.appdata.gpo.GPOMutable) SQLException(java.sql.SQLException) EventKey(org.apache.apex.malhar.lib.dimensions.DimensionsEvent.EventKey) FieldsDescriptor(org.apache.apex.malhar.lib.appdata.schemas.FieldsDescriptor) PreparedStatement(java.sql.PreparedStatement)

Example 5 with EventKey

use of org.apache.apex.malhar.lib.dimensions.DimensionsEvent.EventKey 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)

Aggregations

EventKey (org.apache.apex.malhar.lib.dimensions.DimensionsEvent.EventKey)7 GPOMutable (org.apache.apex.malhar.lib.appdata.gpo.GPOMutable)6 Aggregate (org.apache.apex.malhar.lib.dimensions.DimensionsEvent.Aggregate)3 FieldsDescriptor (org.apache.apex.malhar.lib.appdata.schemas.FieldsDescriptor)2 PreparedStatement (java.sql.PreparedStatement)1 SQLException (java.sql.SQLException)1 Map (java.util.Map)1 CustomTimeBucket (org.apache.apex.malhar.lib.appdata.schemas.CustomTimeBucket)1 Type (org.apache.apex.malhar.lib.appdata.schemas.Type)1 Test (org.junit.Test)1