Search in sources :

Example 11 with Instant

use of org.joda.time.Instant in project beam by apache.

the class GatherAllPanesTest method singlePaneSingleReifiedPane.

@Test
@Category(NeedsRunner.class)
public void singlePaneSingleReifiedPane() {
    PCollection<Iterable<ValueInSingleWindow<Iterable<Long>>>> accumulatedPanes = p.apply(GenerateSequence.from(0).to(20000)).apply(WithTimestamps.of(new SerializableFunction<Long, Instant>() {

        @Override
        public Instant apply(Long input) {
            return new Instant(input * 10);
        }
    })).apply(Window.<Long>into(FixedWindows.of(Duration.standardMinutes(1))).triggering(AfterWatermark.pastEndOfWindow()).withAllowedLateness(Duration.ZERO).discardingFiredPanes()).apply(WithKeys.<Void, Long>of((Void) null).withKeyType(new TypeDescriptor<Void>() {
    })).apply(GroupByKey.<Void, Long>create()).apply(Values.<Iterable<Long>>create()).apply(GatherAllPanes.<Iterable<Long>>globally());
    PAssert.that(accumulatedPanes).satisfies(new SerializableFunction<Iterable<Iterable<ValueInSingleWindow<Iterable<Long>>>>, Void>() {

        @Override
        public Void apply(Iterable<Iterable<ValueInSingleWindow<Iterable<Long>>>> input) {
            for (Iterable<ValueInSingleWindow<Iterable<Long>>> windowedInput : input) {
                if (Iterables.size(windowedInput) > 1) {
                    fail("Expected all windows to have exactly one pane, got " + windowedInput);
                    return null;
                }
            }
            return null;
        }
    });
    p.run();
}
Also used : Instant(org.joda.time.Instant) ValueInSingleWindow(org.apache.beam.sdk.values.ValueInSingleWindow) Category(org.junit.experimental.categories.Category) Test(org.junit.Test)

Example 12 with Instant

use of org.joda.time.Instant in project beam by apache.

the class CreateTest method testCreateTimestampedDefaultOutputCoderUsingCoder.

@Test
public void testCreateTimestampedDefaultOutputCoderUsingCoder() throws Exception {
    Coder<Record> coder = new RecordCoder();
    PBegin pBegin = PBegin.in(p);
    Create.TimestampedValues<Record> values = Create.timestamped(TimestampedValue.of(new Record(), new Instant(0)), TimestampedValue.<Record>of(new Record2(), new Instant(0))).withCoder(coder);
    Coder<Record> defaultCoder = values.getDefaultOutputCoder(pBegin);
    assertThat(defaultCoder, equalTo(coder));
}
Also used : Instant(org.joda.time.Instant) PBegin(org.apache.beam.sdk.values.PBegin) Test(org.junit.Test)

Example 13 with Instant

use of org.joda.time.Instant in project beam by apache.

the class CreateTest method testCreateTimestampedDefaultOutputCoderUsingTypeDescriptor.

@Test
public void testCreateTimestampedDefaultOutputCoderUsingTypeDescriptor() throws Exception {
    Coder<Record> coder = new RecordCoder();
    p.getCoderRegistry().registerCoderForClass(Record.class, coder);
    PBegin pBegin = PBegin.in(p);
    Create.TimestampedValues<Record> values = Create.timestamped(TimestampedValue.of(new Record(), new Instant(0)), TimestampedValue.<Record>of(new Record2(), new Instant(0))).withType(new TypeDescriptor<Record>() {
    });
    Coder<Record> defaultCoder = values.getDefaultOutputCoder(pBegin);
    assertThat(defaultCoder, equalTo(coder));
}
Also used : Instant(org.joda.time.Instant) PBegin(org.apache.beam.sdk.values.PBegin) Test(org.junit.Test)

Example 14 with Instant

use of org.joda.time.Instant in project beam by apache.

the class DistinctTest method testTriggeredDistinct.

@Test
@Category({ ValidatesRunner.class, UsesTestStream.class })
public void testTriggeredDistinct() {
    Instant base = new Instant(0);
    TestStream<String> values = TestStream.create(StringUtf8Coder.of()).advanceWatermarkTo(base).addElements(TimestampedValue.of("k1", base), TimestampedValue.of("k2", base.plus(Duration.standardSeconds(10))), TimestampedValue.of("k3", base.plus(Duration.standardSeconds(20)))).advanceProcessingTime(Duration.standardMinutes(1)).addElements(TimestampedValue.of("k1", base.plus(Duration.standardSeconds(30))), TimestampedValue.of("k2", base.plus(Duration.standardSeconds(40))), TimestampedValue.of("k3", base.plus(Duration.standardSeconds(50)))).advanceWatermarkToInfinity();
    PCollection<String> distinctValues = triggeredDistinctPipeline.apply(values).apply(Window.<String>into(FixedWindows.of(Duration.standardMinutes(1))).triggering(Repeatedly.forever(AfterProcessingTime.pastFirstElementInPane().plusDelayOf(Duration.standardSeconds(30)))).withAllowedLateness(Duration.ZERO).accumulatingFiredPanes()).apply(Distinct.<String>create());
    PAssert.that(distinctValues).containsInAnyOrder("k1", "k2", "k3");
    triggeredDistinctPipeline.run();
}
Also used : Instant(org.joda.time.Instant) Category(org.junit.experimental.categories.Category) Test(org.junit.Test)

Example 15 with Instant

use of org.joda.time.Instant 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)

Aggregations

Instant (org.joda.time.Instant)411 Test (org.junit.Test)326 IntervalWindow (org.apache.beam.sdk.transforms.windowing.IntervalWindow)135 KV (org.apache.beam.sdk.values.KV)64 Category (org.junit.experimental.categories.Category)60 WindowedValue (org.apache.beam.sdk.util.WindowedValue)47 Duration (org.joda.time.Duration)44 ReadableInstant (org.joda.time.ReadableInstant)36 BoundedWindow (org.apache.beam.sdk.transforms.windowing.BoundedWindow)30 WatermarkHoldState (org.apache.beam.sdk.state.WatermarkHoldState)24 TimerData (org.apache.beam.runners.core.TimerInternals.TimerData)22 Matchers.emptyIterable (org.hamcrest.Matchers.emptyIterable)22 HashMap (java.util.HashMap)19 TransformWatermarks (org.apache.beam.runners.direct.WatermarkManager.TransformWatermarks)18 StateNamespaceForTest (org.apache.beam.runners.core.StateNamespaceForTest)17 WindowMatchers.isSingleWindowedValue (org.apache.beam.runners.core.WindowMatchers.isSingleWindowedValue)17 WindowMatchers.isWindowedValue (org.apache.beam.runners.core.WindowMatchers.isWindowedValue)17 TupleTag (org.apache.beam.sdk.values.TupleTag)14 ArrayList (java.util.ArrayList)13 Map (java.util.Map)12