Refine search
@Override public IntervalWindow getSideInputWindow(BoundedWindow mainWindow) { if (mainWindow instanceof GlobalWindow) { throw new IllegalArgumentException( "Attempted to get side input window for GlobalWindow from non-global WindowFn"); } long lastStart = lastStartFor(mainWindow.maxTimestamp().minus(size)); return new IntervalWindow(new Instant(lastStart + period.getMillis()), size); } };
@Test public void testSimpleFixedWindow() throws Exception { Map<IntervalWindow, Set<String>> expected = new HashMap<>(); expected.put(new IntervalWindow(new Instant(0), new Instant(10)), set(1, 2, 5, 9)); expected.put(new IntervalWindow(new Instant(10), new Instant(20)), set(10, 11)); expected.put(new IntervalWindow(new Instant(100), new Instant(110)), set(100)); assertEquals( expected, runWindowFn( FixedWindows.of(new Duration(10)), Arrays.asList(1L, 2L, 5L, 9L, 10L, 11L, 100L))); }
@Test public void testExplodeWindowsInOneWindowEquals() { Instant now = Instant.now(); BoundedWindow window = new IntervalWindow(now.minus(1000L), now.plus(1000L)); WindowedValue<String> value = WindowedValue.of("foo", now, window, PaneInfo.ON_TIME_AND_ONLY_FIRING); assertThat(Iterables.getOnlyElement(value.explodeWindows()), equalTo(value)); }
@Test public void testTimeUnit() throws Exception { Map<IntervalWindow, Set<String>> expected = new HashMap<>(); expected.put(new IntervalWindow(new Instant(1), new Instant(2000)), set(1, 2, 1000)); expected.put(new IntervalWindow(new Instant(5000), new Instant(6001)), set(5000, 5001)); expected.put(new IntervalWindow(new Instant(10000), new Instant(11000)), set(10000)); assertEquals( expected, runWindowFn( Sessions.withGapDuration(Duration.standardSeconds(1)), Arrays.asList(1L, 2L, 1000L, 5000L, 5001L, 10000L))); }
@Test public void testMonths() throws Exception { Map<IntervalWindow, Set<String>> expected = new HashMap<>(); final List<Long> timestamps = Arrays.asList( makeTimestamp(2014, 1, 1, 0, 0).getMillis(), makeTimestamp(2014, 1, 31, 5, 5).getMillis(), makeTimestamp(2014, 2, 1, 0, 0).getMillis(), makeTimestamp(2014, 2, 15, 5, 5).getMillis(), makeTimestamp(2015, 1, 1, 0, 0).getMillis(), makeTimestamp(2015, 1, 31, 5, 5).getMillis()); expected.put( new IntervalWindow(makeTimestamp(2014, 1, 1, 0, 0), makeTimestamp(2014, 2, 1, 0, 0)), set(timestamps.get(0), timestamps.get(1))); expected.put( new IntervalWindow(makeTimestamp(2014, 2, 1, 0, 0), makeTimestamp(2014, 3, 1, 0, 0)), set(timestamps.get(2), timestamps.get(3))); expected.put( new IntervalWindow(makeTimestamp(2015, 1, 1, 0, 0), makeTimestamp(2015, 2, 1, 0, 0)), set(timestamps.get(4), timestamps.get(5))); assertEquals(expected, runWindowFn(CalendarWindows.months(1), timestamps)); }
public Collection<IntervalWindow> assignWindows(Instant timestamp) { List<IntervalWindow> windows = new ArrayList<>((int) (size.getMillis() / period.getMillis())); long lastStart = lastStartFor(timestamp); for (long start = lastStart; start > timestamp.minus(size).getMillis(); start -= period.getMillis()) { windows.add(new IntervalWindow(new Instant(start), size)); } return windows; }
@Test public void testSimple() throws Exception { Map<IntervalWindow, Set<String>> expected = new HashMap<>(); expected.put(new IntervalWindow(new Instant(0), new Instant(10)), set(0)); expected.put(new IntervalWindow(new Instant(10), new Instant(20)), set(10)); expected.put(new IntervalWindow(new Instant(101), new Instant(111)), set(101)); assertEquals( expected, runWindowFn(Sessions.withGapDuration(new Duration(10)), Arrays.asList(0L, 10L, 101L))); }
@Test public void testDecodeEncodeEqual() throws Exception { Instant now = Instant.now(); ValueInSingleWindow<String> value = ValueInSingleWindow.of( "foo", now, new IntervalWindow(now, now.plus(Duration.standardSeconds(10))), PaneInfo.NO_FIRING); CoderProperties.coderDecodeEncodeEqual( ValueInSingleWindow.Coder.of(StringUtf8Coder.of(), IntervalWindow.getCoder()), value); }
/** Returns the minimal window that includes both this window and the given window. */ public IntervalWindow span(IntervalWindow other) { return new IntervalWindow( new Instant(Math.min(start.getMillis(), other.start.getMillis())), new Instant(Math.max(end.getMillis(), other.end.getMillis()))); }
@Test public void testSlightlyOverlapping() throws Exception { Map<IntervalWindow, Set<String>> expected = new HashMap<>(); expected.put(new IntervalWindow(new Instant(-5), new Instant(2)), set(1)); expected.put(new IntervalWindow(new Instant(0), new Instant(7)), set(1, 2, 5)); expected.put(new IntervalWindow(new Instant(5), new Instant(12)), set(5, 9, 10, 11)); expected.put(new IntervalWindow(new Instant(10), new Instant(17)), set(10, 11)); SlidingWindows windowFn = SlidingWindows.of(new Duration(7)).every(new Duration(5)); assertEquals(expected, runWindowFn(windowFn, Arrays.asList(1L, 2L, 5L, 9L, 10L, 11L))); assertThat(windowFn.assignsToOneWindow(), is(false)); }
@Test public void testSupportsWindowParameter() throws Exception { Instant now = Instant.now(); try (DoFnTester<Integer, KV<Integer, BoundedWindow>> tester = DoFnTester.of(new DoFnWithWindowParameter())) { BoundedWindow firstWindow = new IntervalWindow(now, now.plus(Duration.standardMinutes(1))); tester.processWindowedElement(1, now, firstWindow); tester.processWindowedElement(2, now, firstWindow); BoundedWindow secondWindow = new IntervalWindow(now, now.plus(Duration.standardMinutes(4))); tester.processWindowedElement(3, now, secondWindow); tester.finishBundle(); assertThat( tester.peekOutputElementsInWindow(firstWindow), containsInAnyOrder( TimestampedValue.of(KV.of(1, firstWindow), now), TimestampedValue.of(KV.of(2, firstWindow), now))); assertThat( tester.peekOutputElementsInWindow(secondWindow), containsInAnyOrder(TimestampedValue.of(KV.of(3, secondWindow), now))); } }
private IntervalWindow window(long start, long size) { return new IntervalWindow(new Instant(start), new Duration(size)); }
@Test public void testConsecutive() throws Exception { Map<IntervalWindow, Set<String>> expected = new HashMap<>(); expected.put(new IntervalWindow(new Instant(1), new Instant(19)), set(1, 2, 5, 9)); expected.put(new IntervalWindow(new Instant(100), new Instant(111)), set(100, 101)); assertEquals( expected, runWindowFn( Sessions.withGapDuration(new Duration(10)), Arrays.asList(1L, 2L, 5L, 9L, 100L, 101L))); }
public static Matcher<IntervalWindow> intervalWindow(long start, long end) { return Matchers.equalTo(new IntervalWindow(new Instant(start), new Instant(end))); }
@Test public void testFireDeadline() throws Exception { assertEquals( BoundedWindow.TIMESTAMP_MAX_VALUE, underTest.getWatermarkThatGuaranteesFiring( new IntervalWindow(new Instant(0), new Instant(10)))); }
private IntervalWindow intervalWindow(long start, long end) { return new IntervalWindow(new Instant(start), new Instant(end)); }
@Test public void testFixedOffsetWindow() throws Exception { Map<IntervalWindow, Set<String>> expected = new HashMap<>(); expected.put(new IntervalWindow(new Instant(-5), new Instant(5)), set(1, 2)); expected.put(new IntervalWindow(new Instant(5), new Instant(15)), set(5, 9, 10, 11)); expected.put(new IntervalWindow(new Instant(95), new Instant(105)), set(100)); assertEquals( expected, runWindowFn( FixedWindows.of(new Duration(10)).withOffset(new Duration(5)), Arrays.asList(1L, 2L, 5L, 9L, 10L, 11L, 100L))); }
public static <T> Matcher<WindowedValue<? extends T>> isSingleWindowedValue( Matcher<T> valueMatcher, long timestamp, long windowStart, long windowEnd, PaneInfo paneInfo) { IntervalWindow intervalWindow = new IntervalWindow(new Instant(windowStart), new Instant(windowEnd)); return WindowMatchers.isSingleWindowedValue( valueMatcher, Matchers.describedAs("%0", Matchers.equalTo(new Instant(timestamp)), timestamp), Matchers.equalTo(intervalWindow), Matchers.equalTo(paneInfo)); }
@Test public void testMerging() throws Exception { Map<IntervalWindow, Set<String>> expected = new HashMap<>(); expected.put(new IntervalWindow(new Instant(1), new Instant(40)), set(1, 10, 15, 22, 30)); expected.put(new IntervalWindow(new Instant(95), new Instant(111)), set(95, 100, 101)); assertEquals( expected, runWindowFn( Sessions.withGapDuration(new Duration(10)), Arrays.asList(1L, 15L, 30L, 100L, 101L, 95L, 22L, 10L))); }
public static <T> Matcher<WindowedValue<? extends T>> isSingleWindowedValue( Matcher<T> valueMatcher, long timestamp, long windowStart, long windowEnd) { IntervalWindow intervalWindow = new IntervalWindow(new Instant(windowStart), new Instant(windowEnd)); return WindowMatchers.isSingleWindowedValue( valueMatcher, Matchers.describedAs("%0", Matchers.equalTo(new Instant(timestamp)), timestamp), Matchers.equalTo(intervalWindow), Matchers.anything()); }