@ProcessElement public void processElement(ProcessContext context, BoundedWindow window) { WindowedStructuralKey<K> key = WindowedStructuralKey.create(keyCoder, context.element().getKey(), window); AccumT accumulator = accumulators.get(key); Instant assignedTs = timestampCombiner.assign(window, context.timestamp()); if (accumulator == null) { accumulator = combineFn.createAccumulator(); accumulators.put(key, accumulator); timestamps.put(key, assignedTs); } accumulators.put(key, combineFn.addInput(accumulator, context.element().getValue())); timestamps.put(key, timestampCombiner.combine(assignedTs, timestamps.get(key))); }
@Override public KV<Instant, WindowedValue<KV<K, V>>> map(WindowedValue<KV<K, V>> wv) { BoundedWindow window = Iterables.getOnlyElement(wv.getWindows()); Instant timestamp = timestampCombiner.assign(window, windowFn.getOutputTime(wv.getTimestamp(), window)); return KV.of(timestamp, wv); } }
options, sideInputReader, singletonW); Instant windowTimestamp = timestampCombiner.assign( mergedWindow, windowFn.getOutputTime(currentValue.getTimestamp(), mergedWindow)); accumAndInstant = new Tuple2<>(accumT, windowTimestamp); timestampCombiner.combine( accumAndInstant.f1, timestampCombiner.assign( mergedWindow, windowingStrategy
key, currentValue.getValue().getValue(), options, sideInputReader, singletonW); Instant windowTimestamp = timestampCombiner.assign( mergedWindow, windowFn.getOutputTime(currentValue.getTimestamp(), mergedWindow)); accumAndInstant = new Tuple2<>(accumT, windowTimestamp); timestampCombiner.combine( accumAndInstant.f1, timestampCombiner.assign( mergedWindow, windowingStrategy
key, currentValue.getValue().getValue(), options, sideInputReader, singletonW); Instant windowTimestamp = timestampCombiner.assign( mergedWindow, windowFn.getOutputTime(currentValue.getTimestamp(), mergedWindow)); accumAndInstant = new Tuple2<>(accumT, windowTimestamp); timestampCombiner.combine( accumAndInstant.f1, timestampCombiner.assign( mergedWindow, windowingStrategy
/** * Return {@code timestamp}, possibly shifted forward in time according to the window strategy's * output time function. */ private Instant shift(Instant timestamp, W window) { Instant shifted = windowingStrategy .getTimestampCombiner() .assign(window, windowingStrategy.getWindowFn().getOutputTime(timestamp, window)); // Don't call checkState(), to avoid calling BoundedWindow.formatTimestamp() every time if (shifted.isBefore(timestamp)) { throw new IllegalStateException( String.format( "TimestampCombiner moved element from %s to earlier time %s for window %s", BoundedWindow.formatTimestamp(timestamp), BoundedWindow.formatTimestamp(shifted), window)); } checkState( timestamp.isAfter(window.maxTimestamp()) || !shifted.isAfter(window.maxTimestamp()), "TimestampCombiner moved element from %s to %s which is beyond end of " + "window %s", timestamp, shifted, window); return shifted; }
timestampCombiner.assign( currentWindow, windowingStrategy timestampCombiner.combine( windowTimestamp, timestampCombiner.assign( currentWindow, windowFn.getOutputTime(nextValue.getTimestamp(), currentWindow))); currentWindow = nextWindow; windowTimestamp = timestampCombiner.assign( currentWindow, windowFn.getOutputTime(nextValue.getTimestamp(), currentWindow));
timestampCombiner.assign( currentWindow, windowFn.getOutputTime(currentValue.getTimestamp(), currentWindow)); timestampCombiner.combine( windowTimestamp, timestampCombiner.assign( currentWindow, windowFn.getOutputTime(nextValue.getTimestamp(), currentWindow))); key, value, options, sideInputReader, currentValue.getWindows()); windowTimestamp = timestampCombiner.assign( currentWindow, windowFn.getOutputTime(nextValue.getTimestamp(), currentWindow));
timestampCombiner.assign( currentWindow, windowFn.getOutputTime(currentValue.getTimestamp(), currentWindow)); timestampCombiner.combine( windowTimestamp, timestampCombiner.assign( currentWindow, windowFn.getOutputTime(nextValue.getTimestamp(), currentWindow))); options, sideInputReader, currentValue.getWindows()); windowTimestamp = timestampCombiner.assign( currentWindow, windowFn.getOutputTime(nextValue.getTimestamp(), currentWindow));
timestampCombiner.assign( currentWindow, windowFn.getOutputTime(currentValue.getTimestamp(), currentWindow)); timestampCombiner.combine( windowTimestamp, timestampCombiner.assign( currentWindow, windowFn.getOutputTime(nextValue.getTimestamp(), currentWindow))); key, value, options, sideInputReader, currentValue.getWindows()); windowTimestamp = timestampCombiner.assign( currentWindow, windowFn.getOutputTime(nextValue.getTimestamp(), currentWindow));
@Test @SuppressWarnings({"rawtypes", "unchecked"}) public void testKeyedByTimestamp() { WindowFn slidingWindows = Sessions.withGapDuration(Duration.millis(10)); BoundedWindow window = new IntervalWindow(new org.joda.time.Instant(0), new org.joda.time.Instant(10)); GroupByKeyTranslator.KeyedByTimestamp keyedByTimestamp = new GroupByKeyTranslator.KeyedByTimestamp(slidingWindows, timestampCombiner); WindowedValue<KV<String, String>> value = WindowedValue.of( KV.of("key", "val"), org.joda.time.Instant.now(), window, PaneInfo.NO_FIRING); KV<org.joda.time.Instant, WindowedValue<KV<String, String>>> result = keyedByTimestamp.map(value); org.joda.time.Instant time = timestampCombiner.assign( window, slidingWindows.getOutputTime(value.getTimestamp(), window)); assertThat(result, equalTo(KV.of(time, value))); }