@Override public TriggerResult onElement(long time, TimeInterval window, TriggerContext ctx) { ValueStorage<Long> fireStamp = ctx.getValueStorage(FIRE_TIME_DESCR); if (fireStamp.get() == Long.MAX_VALUE) { // register first timer aligned with window start long start = window.getStartMillis() - (window.getStartMillis() % interval); long nextFireTimestamp = start + interval; LOG.trace("Registering PeriodicTimeTrigger for time {}", nextFireTimestamp); ctx.registerTimer(nextFireTimestamp, window); fireStamp.set(nextFireTimestamp); } return TriggerResult.NOOP; }
@Override public Iterable<TimeInterval> assignWindowsToElement(WindowedElement<?, T> input) { return Collections.singleton(new TimeInterval(0, Long.MAX_VALUE)); }
while (windows.hasNext()) { TimeInterval w = windows.next(); if (mergeCandidate.intersects(w)) { if (toMerge == null) { toMerge = new ArrayList<>(); mergeCandidate = mergeCandidate.cover(w);
private void assertSessionWindow( TimeInterval window, long expectedStartMillis, long expectedEndMillis) { assertNotNull(window); assertEquals(expectedStartMillis, window.getStartMillis()); assertEquals(expectedEndMillis, window.getEndMillis()); }
.keyBy(e -> e.getFirst().getStartMillis()) .valueBy(new ToHashMap<>(Triple::getSecond, Triple::getThird)) .combineBy(new MergeMaps<>()) output.getOutputs(), Arrays.asList( Pair.of(new TimeInterval(0, 5), toMap(Pair.of("one", 2L), Pair.of("two", 4L), Pair.of("three", 1L))), Pair.of(new TimeInterval(10, 15), toMap(Pair.of("one", 1L), Pair.of("two", 1L))), Pair.of(new TimeInterval(5, 10), toMap(Pair.of("one", 3L), Pair.of("two", 2L)))) .stream() .map(p -> Triple.of(p.getFirst(), p.getFirst().getStartMillis(), p.getSecond())) .collect(Collectors.toList()));
@Override public TriggerResult onTimer(long time, TimeInterval window, TriggerContext ctx) { ValueStorage<Long> fireStamp = ctx.getValueStorage(FIRE_TIME_DESCR); if (fireStamp.get() == time) { long nextTimestamp = time + interval; if (nextTimestamp < window.getEndMillis()) { ctx.registerTimer(time + interval, window); fireStamp.set(time + interval); } return TriggerResult.FLUSH; } return TriggerResult.NOOP; }
@Override public TriggerResult onTimer(long time, TimeInterval window, TriggerContext ctx) { if (time == window.maxTimestamp()) { LOG.debug("Firing TimeTrigger, time {}, window: {}", time, window); return TriggerResult.FLUSH_AND_PURGE; } return TriggerResult.NOOP; }
long windowBegin = ((TimeInterval) out.getWindow()).getStartMillis(); long windowEnd = ((TimeInterval) out.getWindow()).getEndMillis(); out.collect(Triple.of( Instant.ofEpochMilli(windowBegin),
@Override public TriggerResult onTimer(long time, TimeInterval window, TriggerContext ctx) { ValueStorage<Long> fireStamp = ctx.getValueStorage(FIRE_TIME_DESCR); if (fireStamp.get() == time) { long nextTimestamp = time + interval; if (nextTimestamp < window.getEndMillis()) { ctx.registerTimer(time + interval, window); fireStamp.set(time + interval); } return TriggerResult.FLUSH; } return TriggerResult.NOOP; }
@Override public TriggerResult onTimer(long time, TimeInterval window, TriggerContext ctx) { if (time == window.maxTimestamp()) { LOG.debug("Firing TimeTrigger, time {}, window: {}", time, window); return TriggerResult.FLUSH_AND_PURGE; } return TriggerResult.NOOP; }
@Override public Iterable<TimeInterval> assignWindowsToElement(WindowedElement<?, T> input) { return Collections.singleton(new TimeInterval(0, Long.MAX_VALUE)); }
long windowBegin = ((TimeInterval) out.getWindow()).getStartMillis(); long windowEnd = ((TimeInterval) out.getWindow()).getEndMillis(); out.collect(Triple.of( Instant.ofEpochMilli(windowBegin),
@Override public TriggerResult onElement(long time, TimeInterval window, TriggerContext ctx) { ValueStorage<Long> fireStamp = ctx.getValueStorage(FIRE_TIME_DESCR); if (fireStamp.get() == Long.MAX_VALUE) { // register first timer aligned with window start long start = window.getStartMillis() - (window.getStartMillis() % interval); long nextFireTimestamp = start + interval; LOG.trace("Registering PeriodicTimeTrigger for time {}", nextFireTimestamp); ctx.registerTimer(nextFireTimestamp, window); fireStamp.set(nextFireTimestamp); } return TriggerResult.NOOP; }
.using((Triple<Byte, Pair<String, Double>, Double> e, Collector<String> c) -> { Date now = new Date(); Date stamp = new Date(TimeSliding.getLabel(c).getEndMillis()); c.collect(now + ": " + stamp + ", " + e.getSecond().getFirst() + ", " + e.getSecond().getSecond()); })
while (windows.hasNext()) { TimeInterval w = windows.next(); if (mergeCandidate.intersects(w)) { if (toMerge == null) { toMerge = new ArrayList<>(); mergeCandidate = mergeCandidate.cover(w);
private TriggerResult registerTimer(TimeInterval window, TriggerContext ctx) { if (ctx.registerTimer(window.maxTimestamp(), window)) { return TriggerResult.NOOP; } // if the time already passed discard the late coming element return TriggerResult.PURGE; } }
/** * Returns the minimal window covers both this window and the given window. */ TimeInterval cover(TimeInterval that) { return new TimeInterval( Math.min(this.startMillis, that.startMillis), Math.max(this.endMillis, that.endMillis)); }
@Test public void testTimeSlidingLabelAssignment() { TimeSliding<Long> windowing = TimeSliding .of(Duration.ofHours(1), Duration.ofMinutes(20)); UnaryFunction<Long, Long> eventTimeAssigner = e -> e * 1000L; long[] data = { 3590, 3600, 3610, 3800, 7190, 7200, 7210 }; for (long event : data) { Iterable<TimeInterval> labels = windowing .assignWindowsToElement(new Elem<>( GlobalWindowing.Window.get(), event, eventTimeAssigner.apply(event))); // verify window count assertEquals(3, Iterables.size(labels)); // verify that each window contains the original event for (TimeInterval l : labels) { long stamp = event * 1000L; assertTrue(stamp >= l.getStartMillis()); assertTrue(stamp <= l.getEndMillis()); } } }
b -> b.windowBy(GlobalWindowing.get(), w -> ""), b -> b.windowBy( Time.of(Duration.ofMinutes(5)), w -> String.valueOf(w.getStartMillis()))) .build());
@Override protected Dataset<Triple<Instant, Type, Long>> getOutput( Dataset<Triple<Instant, Type, String>> input) { input = AssignEventTime.of(input).using(t -> t.getFirst().toEpochMilli()).output(); Dataset<ComparablePair<Type, String>> distinct = Distinct.of(input) .mapped(t -> new ComparablePair<>(t.getSecond(), t.getThird())) .windowBy(Time.of(Duration.ofHours(1))) .output(); Dataset<Pair<Type, Long>> reduced = ReduceByKey.of(distinct) .keyBy(ComparablePair::getFirst) .valueBy(p -> 1L) .combineBy(Sums.ofLongs()) .windowBy(Time.of(Duration.ofHours(1))) .output(); // extract window end timestamp return FlatMap .of(reduced) .using((Pair<Type, Long> p, Collector<Triple<Instant, Type, Long>> ctx) -> { long windowEnd = ((TimeInterval) ctx.getWindow()).getEndMillis(); ctx.collect(Triple.of(Instant.ofEpochMilli(windowEnd), p.getFirst(), p.getSecond())); }) .output(); }