/** * Attaches a stage that performs the given group-and-aggregate operation. * It emits one key-value pair (in a {@link TimestampedEntry}) for each * distinct key it observes in its input belonging to a given window. The * value is the result of the aggregate operation across all the items with * the given grouping key. * * @see com.hazelcast.jet.aggregate.AggregateOperations AggregateOperations * @param aggrOp the aggregate operation to perform * @param <R> type of the aggregation result */ @Nonnull default <R> StreamStage<TimestampedEntry<K, R>> aggregate(@Nonnull AggregateOperation1<? super T, ?, R> aggrOp) { return aggregate(aggrOp, TimestampedEntry::fromWindowResult); }
/** * Attaches a stage that passes through just the items that are distinct * within their window according to the grouping key (no two items emitted * for a window map to the same key). There is no guarantee among the items * with the same key which one it will pass through. To create the item to * emit, the stage calls the supplied {@code mapToOutputFn}. * * @param mapToOutputFn function that returns the items to emit * @return the newly attached stage */ @Nonnull default <R> StreamStage<R> distinct(@Nonnull WindowResultFunction<? super T, ? extends R> mapToOutputFn) { return aggregate(pickAny(), mapToOutputFn.toKeyedWindowResultFn()); }
.groupingKey(CarCount::getLocation) .window(sliding(MINUTES.toMillis(120), MINUTES.toMillis(15))) .aggregate(linearTrend(CarCount::getTime, CarCount::getCount)) .map((TimestampedEntry<String, Double> e) -> entry(new TrendKey(e.getKey(), e.getTimestamp()), e.getValue()))
private static Pipeline buildPipeline() { Pipeline p = Pipeline.create(); p.drawFrom(Sources.<Trade, Integer, Trade>mapJournal(TRADES_MAP_NAME, DistributedPredicate.alwaysTrue(), EventJournalMapEvent::getNewValue, START_FROM_CURRENT)) .addTimestamps(Trade::getTime, 3000) .groupingKey(Trade::getTicker) .window(WindowDefinition.sliding(SLIDING_WINDOW_LENGTH_MILLIS, SLIDE_STEP_MILLIS)) .aggregate(counting(), (winStart, winEnd, key, result) -> String.format("%s %5s %4d", toLocalTime(winEnd), key, result)) .drainTo(Sinks.logger()); return p; }
@SuppressWarnings("Convert2MethodRef") // https://bugs.openjdk.java.net/browse/JDK-8154236 private static Pipeline groupAndAggregate() { Pipeline p = Pipeline.create(); p.drawFrom(Sources.<PageVisit, Integer, PageVisit>mapJournal(PAGE_VISIT, mapPutEvents(), mapEventNewValue(), START_FROM_OLDEST)) .addTimestamps(pv -> pv.timestamp(), 100) .window(sliding(10, 1)) .groupingKey(pv -> pv.userId()) .aggregate(toList()) .drainTo(Sinks.logger()); return p; }
private static Pipeline buildPipeline() { Pipeline p = Pipeline.create(); p.drawFrom(Sources.<PriceUpdateEvent, String, Tuple2<Integer, Long>>mapJournal( "prices", mapPutEvents(), e -> new PriceUpdateEvent(e.getKey(), e.getNewValue().f0(), e.getNewValue().f1()), START_FROM_CURRENT )) .addTimestamps(PriceUpdateEvent::timestamp, LAG_SECONDS * 1000) .setLocalParallelism(1) .groupingKey(PriceUpdateEvent::ticker) .window(WindowDefinition.sliding(WINDOW_SIZE_SECONDS * 1000, 1000)) .aggregate(AggregateOperations.counting()) .drainTo(Sinks.logger()); return p; }
private static Pipeline buildPipeline() { // we'll calculate two aggregations over the same input data: // 1. number of viewed product listings // 2. set of purchased product IDs // Output of the aggregation will be List{Integer, Set<String>} AggregateOperation1<ProductEvent, ?, Tuple2<Long, Set<String>>> aggrOp = allOf( summingLong(e -> e.getProductEventType() == VIEW_LISTING ? 1 : 0), mapping(e -> e.getProductEventType() == PURCHASE ? e.getProductId() : null, toSet()) ); Pipeline p = Pipeline.create(); p.drawFrom(Sources.<ProductEvent>streamFromProcessor("generator", ProcessorMetaSupplier.of(GenerateEventsP::new, 1))) .addTimestamps(ProductEvent::getTimestamp, 0) .groupingKey(ProductEvent::getUserId) .window(WindowDefinition.session(SESSION_TIMEOUT)) .aggregate(aggrOp, SessionWindow::sessionToString) .drainTo(Sinks.logger()); return p; }