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