Search in sources :

Example 1 with FixedWindows

use of org.apache.beam.sdk.transforms.windowing.FixedWindows in project beam by apache.

the class LateDataUtilsTest method garbageCollectionTimeAfterEndOfGlobalWindow.

@Test
public void garbageCollectionTimeAfterEndOfGlobalWindow() {
    FixedWindows windowFn = FixedWindows.of(Duration.standardMinutes(5));
    WindowingStrategy<?, ?> strategy = WindowingStrategy.globalDefault().withWindowFn(windowFn);
    IntervalWindow window = windowFn.assignWindow(new Instant(BoundedWindow.TIMESTAMP_MAX_VALUE));
    assertThat(window.maxTimestamp(), Matchers.<ReadableInstant>greaterThan(GlobalWindow.INSTANCE.maxTimestamp()));
    assertThat(LateDataUtils.garbageCollectionTime(window, strategy), equalTo(GlobalWindow.INSTANCE.maxTimestamp()));
}
Also used : FixedWindows(org.apache.beam.sdk.transforms.windowing.FixedWindows) ReadableInstant(org.joda.time.ReadableInstant) Instant(org.joda.time.Instant) IntervalWindow(org.apache.beam.sdk.transforms.windowing.IntervalWindow) Test(org.junit.Test)

Example 2 with FixedWindows

use of org.apache.beam.sdk.transforms.windowing.FixedWindows in project beam by apache.

the class LateDataUtilsTest method beforeEndOfGlobalWindowSame.

@Test
public void beforeEndOfGlobalWindowSame() {
    FixedWindows windowFn = FixedWindows.of(Duration.standardMinutes(5));
    Duration allowedLateness = Duration.standardMinutes(2);
    WindowingStrategy<?, ?> strategy = WindowingStrategy.globalDefault().withWindowFn(windowFn).withAllowedLateness(allowedLateness);
    IntervalWindow window = windowFn.assignWindow(new Instant(10));
    assertThat(LateDataUtils.garbageCollectionTime(window, strategy), equalTo(window.maxTimestamp().plus(allowedLateness)));
}
Also used : FixedWindows(org.apache.beam.sdk.transforms.windowing.FixedWindows) ReadableInstant(org.joda.time.ReadableInstant) Instant(org.joda.time.Instant) Duration(org.joda.time.Duration) IntervalWindow(org.apache.beam.sdk.transforms.windowing.IntervalWindow) Test(org.junit.Test)

Example 3 with FixedWindows

use of org.apache.beam.sdk.transforms.windowing.FixedWindows in project beam by apache.

the class TestStreamTest method testFirstElementLate.

@Test
@Category({ NeedsRunner.class, UsesTestStream.class })
public void testFirstElementLate() {
    Instant lateElementTimestamp = new Instant(-1_000_000);
    TestStream<String> stream = TestStream.create(StringUtf8Coder.of()).advanceWatermarkTo(new Instant(0)).addElements(TimestampedValue.of("late", lateElementTimestamp)).addElements(TimestampedValue.of("onTime", new Instant(100))).advanceWatermarkToInfinity();
    FixedWindows windowFn = FixedWindows.of(Duration.millis(1000L));
    Duration allowedLateness = Duration.millis(5000L);
    PCollection<String> values = p.apply(stream).apply(Window.<String>into(windowFn).triggering(DefaultTrigger.of()).discardingFiredPanes().withAllowedLateness(allowedLateness)).apply(WithKeys.<Integer, String>of(1)).apply(GroupByKey.<Integer, String>create()).apply(Values.<Iterable<String>>create()).apply(Flatten.<String>iterables());
    PAssert.that(values).inWindow(windowFn.assignWindow(lateElementTimestamp)).empty();
    PAssert.that(values).inWindow(windowFn.assignWindow(new Instant(100))).containsInAnyOrder("onTime");
    p.run();
}
Also used : FixedWindows(org.apache.beam.sdk.transforms.windowing.FixedWindows) Instant(org.joda.time.Instant) Duration(org.joda.time.Duration) Category(org.junit.experimental.categories.Category) Test(org.junit.Test)

Example 4 with FixedWindows

use of org.apache.beam.sdk.transforms.windowing.FixedWindows in project beam by apache.

the class CreateStreamTest method testFirstElementLate.

@Test
public void testFirstElementLate() throws IOException {
    Instant lateElementTimestamp = new Instant(-1_000_000);
    CreateStream<String> source = CreateStream.of(StringUtf8Coder.of(), batchDuration()).emptyBatch().advanceWatermarkForNextBatch(new Instant(0)).nextBatch(TimestampedValue.of("late", lateElementTimestamp), TimestampedValue.of("onTime", new Instant(100))).advanceNextBatchWatermarkToInfinity();
    FixedWindows windowFn = FixedWindows.of(Duration.millis(1000L));
    Duration allowedLateness = Duration.millis(5000L);
    PCollection<String> values = p.apply(source).apply(Window.<String>into(windowFn).triggering(DefaultTrigger.of()).discardingFiredPanes().withAllowedLateness(allowedLateness)).apply(WithKeys.<Integer, String>of(1)).apply(GroupByKey.<Integer, String>create()).apply(Values.<Iterable<String>>create()).apply(Flatten.<String>iterables());
    PAssert.that(values).inWindow(windowFn.assignWindow(lateElementTimestamp)).empty();
    PAssert.that(values).inWindow(windowFn.assignWindow(new Instant(100))).containsInAnyOrder("onTime");
    p.run();
}
Also used : FixedWindows(org.apache.beam.sdk.transforms.windowing.FixedWindows) Instant(org.joda.time.Instant) Duration(org.joda.time.Duration) StreamingTest(org.apache.beam.runners.spark.StreamingTest) Test(org.junit.Test)

Example 5 with FixedWindows

use of org.apache.beam.sdk.transforms.windowing.FixedWindows in project beam by apache.

the class CreateStreamTest method testElementsAtAlmostPositiveInfinity.

@Test
public void testElementsAtAlmostPositiveInfinity() throws IOException {
    Instant endOfGlobalWindow = GlobalWindow.INSTANCE.maxTimestamp();
    CreateStream<String> source = CreateStream.of(StringUtf8Coder.of(), batchDuration()).nextBatch(TimestampedValue.of("foo", endOfGlobalWindow), TimestampedValue.of("bar", endOfGlobalWindow)).advanceNextBatchWatermarkToInfinity();
    FixedWindows windows = FixedWindows.of(Duration.standardHours(6));
    PCollection<String> windowedValues = p.apply(source).apply(Window.<String>into(windows)).apply(WithKeys.<Integer, String>of(1)).apply(GroupByKey.<Integer, String>create()).apply(Values.<Iterable<String>>create()).apply(Flatten.<String>iterables());
    PAssert.that(windowedValues).inWindow(windows.assignWindow(GlobalWindow.INSTANCE.maxTimestamp())).containsInAnyOrder("foo", "bar");
    p.run();
}
Also used : FixedWindows(org.apache.beam.sdk.transforms.windowing.FixedWindows) Instant(org.joda.time.Instant) StreamingTest(org.apache.beam.runners.spark.StreamingTest) Test(org.junit.Test)

Aggregations

FixedWindows (org.apache.beam.sdk.transforms.windowing.FixedWindows)11 Instant (org.joda.time.Instant)11 Test (org.junit.Test)11 Duration (org.joda.time.Duration)6 IntervalWindow (org.apache.beam.sdk.transforms.windowing.IntervalWindow)5 Category (org.junit.experimental.categories.Category)5 StreamingTest (org.apache.beam.runners.spark.StreamingTest)3 ReadableInstant (org.joda.time.ReadableInstant)3 ProcessElement (org.apache.beam.sdk.transforms.DoFn.ProcessElement)1 StringUtils.byteArrayToJsonString (org.apache.beam.sdk.util.StringUtils.byteArrayToJsonString)1 Matchers.containsString (org.hamcrest.Matchers.containsString)1