Search in sources :

Example 6 with SlidingWindows

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

the class SplittableDoFnTest method testPairWithIndexWindowedTimestamped.

@Test
@Category({ ValidatesRunner.class, UsesSplittableParDo.class })
public void testPairWithIndexWindowedTimestamped() {
    // Tests that Splittable DoFn correctly propagates windowing strategy, windows and timestamps
    // of elements in the input collection.
    MutableDateTime mutableNow = Instant.now().toMutableDateTime();
    mutableNow.setMillisOfSecond(0);
    Instant now = mutableNow.toInstant();
    Instant nowP1 = now.plus(Duration.standardSeconds(1));
    Instant nowP2 = now.plus(Duration.standardSeconds(2));
    SlidingWindows windowFn = SlidingWindows.of(Duration.standardSeconds(5)).every(Duration.standardSeconds(1));
    PCollection<KV<String, Integer>> res = p.apply(Create.timestamped(TimestampedValue.of("a", now), TimestampedValue.of("bb", nowP1), TimestampedValue.of("ccccc", nowP2))).apply(Window.<String>into(windowFn)).apply(ParDo.of(new PairStringWithIndexToLength())).setCoder(KvCoder.of(StringUtf8Coder.of(), BigEndianIntegerCoder.of()));
    assertEquals(windowFn, res.getWindowingStrategy().getWindowFn());
    PCollection<TimestampedValue<KV<String, Integer>>> timestamped = res.apply("Reify timestamps", ParDo.of(new ReifyTimestampsFn<KV<String, Integer>>()));
    for (int i = 0; i < 4; ++i) {
        Instant base = now.minus(Duration.standardSeconds(i));
        IntervalWindow window = new IntervalWindow(base, base.plus(Duration.standardSeconds(5)));
        List<TimestampedValue<KV<String, Integer>>> expectedUnfiltered = Arrays.asList(TimestampedValue.of(KV.of("a", 0), now), TimestampedValue.of(KV.of("bb", 0), nowP1), TimestampedValue.of(KV.of("bb", 1), nowP1), TimestampedValue.of(KV.of("ccccc", 0), nowP2), TimestampedValue.of(KV.of("ccccc", 1), nowP2), TimestampedValue.of(KV.of("ccccc", 2), nowP2), TimestampedValue.of(KV.of("ccccc", 3), nowP2), TimestampedValue.of(KV.of("ccccc", 4), nowP2));
        List<TimestampedValue<KV<String, Integer>>> expected = new ArrayList<>();
        for (TimestampedValue<KV<String, Integer>> tv : expectedUnfiltered) {
            if (!window.start().isAfter(tv.getTimestamp()) && !tv.getTimestamp().isAfter(window.maxTimestamp())) {
                expected.add(tv);
            }
        }
        assertFalse(expected.isEmpty());
        PAssert.that(timestamped).inWindow(window).containsInAnyOrder(expected);
    }
    p.run();
}
Also used : Instant(org.joda.time.Instant) ArrayList(java.util.ArrayList) MutableDateTime(org.joda.time.MutableDateTime) KV(org.apache.beam.sdk.values.KV) TimestampedValue(org.apache.beam.sdk.values.TimestampedValue) IntervalWindow(org.apache.beam.sdk.transforms.windowing.IntervalWindow) SlidingWindows(org.apache.beam.sdk.transforms.windowing.SlidingWindows) Category(org.junit.experimental.categories.Category) Test(org.junit.Test)

Example 7 with SlidingWindows

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

the class BeamAggregationRel method computeWindowingCostEffect.

private NodeStats computeWindowingCostEffect(NodeStats inputStat) {
    if (windowFn == null) {
        return inputStat;
    }
    WindowFn w = windowFn;
    double multiplicationFactor = 1;
    // tuples repeat in multiple windows).
    if (w instanceof SlidingWindows) {
        multiplicationFactor = ((double) ((SlidingWindows) w).getSize().getStandardSeconds()) / ((SlidingWindows) w).getPeriod().getStandardSeconds();
    }
    return NodeStats.create(inputStat.getRowCount() * multiplicationFactor, inputStat.getRate() * multiplicationFactor, BeamIOSourceRel.CONSTANT_WINDOW_SIZE);
}
Also used : WindowFn(org.apache.beam.sdk.transforms.windowing.WindowFn) SlidingWindows(org.apache.beam.sdk.transforms.windowing.SlidingWindows)

Aggregations

SlidingWindows (org.apache.beam.sdk.transforms.windowing.SlidingWindows)7 IntervalWindow (org.apache.beam.sdk.transforms.windowing.IntervalWindow)5 Instant (org.joda.time.Instant)5 KV (org.apache.beam.sdk.values.KV)4 Test (org.junit.Test)4 MutableDateTime (org.joda.time.MutableDateTime)3 Category (org.junit.experimental.categories.Category)3 ArrayList (java.util.ArrayList)2 WindowFn (org.apache.beam.sdk.transforms.windowing.WindowFn)2 StringUtils.byteArrayToJsonString (org.apache.beam.sdk.util.StringUtils.byteArrayToJsonString)2 TimestampedValue (org.apache.beam.sdk.values.TimestampedValue)2 Matchers.containsString (org.hamcrest.Matchers.containsString)2 Timer (org.apache.beam.sdk.state.Timer)1 TimerSpec (org.apache.beam.sdk.state.TimerSpec)1 OnTimer (org.apache.beam.sdk.transforms.DoFn.OnTimer)1 BoundedWindow (org.apache.beam.sdk.transforms.windowing.BoundedWindow)1 FixedWindows (org.apache.beam.sdk.transforms.windowing.FixedWindows)1 Sessions (org.apache.beam.sdk.transforms.windowing.Sessions)1 TypeDescriptor (org.apache.beam.sdk.values.TypeDescriptor)1