Search in sources :

Example 1 with GlobalWindow

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

the class ProcessFnRunner method checkTrivialOuterWindows.

private static <T> void checkTrivialOuterWindows(WindowedValue<KeyedWorkItem<String, T>> windowedKWI) {
    // In practice it will be in 0 or 1 windows (ValueInEmptyWindows or ValueInGlobalWindow)
    Collection<? extends BoundedWindow> outerWindows = windowedKWI.getWindows();
    if (!outerWindows.isEmpty()) {
        checkArgument(outerWindows.size() == 1, "The KeyedWorkItem itself must not be in multiple windows, but was in: %s", outerWindows);
        BoundedWindow onlyWindow = Iterables.getOnlyElement(outerWindows);
        checkArgument(onlyWindow instanceof GlobalWindow, "KeyedWorkItem must be in the Global window, but was in: %s", onlyWindow);
    }
}
Also used : BoundedWindow(org.apache.beam.sdk.transforms.windowing.BoundedWindow) GlobalWindow(org.apache.beam.sdk.transforms.windowing.GlobalWindow)

Example 2 with GlobalWindow

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

the class TimestampPrefixingWindowCoderTest method testEncodeAndDecode.

@Test
public void testEncodeAndDecode() throws Exception {
    List<IntervalWindow> intervalWindowsToTest = Lists.newArrayList(new IntervalWindow(new Instant(0L), new Instant(1L)), new IntervalWindow(new Instant(100L), new Instant(200L)), new IntervalWindow(new Instant(0L), BoundedWindow.TIMESTAMP_MAX_VALUE));
    TimestampPrefixingWindowCoder<IntervalWindow> coder1 = TimestampPrefixingWindowCoder.of(IntervalWindow.getCoder());
    for (IntervalWindow window : intervalWindowsToTest) {
        CoderProperties.coderDecodeEncodeEqual(coder1, window);
    }
    GlobalWindow globalWindow = GlobalWindow.INSTANCE;
    TimestampPrefixingWindowCoder<GlobalWindow> coder2 = TimestampPrefixingWindowCoder.of(GlobalWindow.Coder.INSTANCE);
    CoderProperties.coderDecodeEncodeEqual(coder2, globalWindow);
    TimestampPrefixingWindowCoder<CustomWindow> coder3 = TimestampPrefixingWindowCoder.of(CustomWindowCoder.of(true, true));
    for (CustomWindow window : CUSTOM_WINDOW_LIST) {
        CoderProperties.coderDecodeEncodeEqual(coder3, window);
    }
}
Also used : Instant(org.joda.time.Instant) IntervalWindow(org.apache.beam.sdk.transforms.windowing.IntervalWindow) GlobalWindow(org.apache.beam.sdk.transforms.windowing.GlobalWindow) Test(org.junit.Test)

Example 3 with GlobalWindow

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

the class TransformTranslatorTest method testSplitBySameKey.

@Test
public void testSplitBySameKey() {
    VarIntCoder coder = VarIntCoder.of();
    WindowedValue.WindowedValueCoder<Integer> wvCoder = WindowedValue.FullWindowedValueCoder.of(coder, GlobalWindow.Coder.INSTANCE);
    Instant now = Instant.now();
    List<GlobalWindow> window = Arrays.asList(GlobalWindow.INSTANCE);
    PaneInfo paneInfo = PaneInfo.NO_FIRING;
    List<Tuple2<ByteArray, byte[]>> firstKey = Arrays.asList(new Tuple2(new ByteArray(CoderHelpers.toByteArrayWithTs(1, coder, now)), CoderHelpers.toByteArray(WindowedValue.of(1, now, window, paneInfo), wvCoder)), new Tuple2(new ByteArray(CoderHelpers.toByteArrayWithTs(1, coder, now.plus(Duration.millis(1)))), CoderHelpers.toByteArray(WindowedValue.of(2, now.plus(Duration.millis(1)), window, paneInfo), wvCoder)));
    List<Tuple2<ByteArray, byte[]>> secondKey = Arrays.asList(new Tuple2(new ByteArray(CoderHelpers.toByteArrayWithTs(2, coder, now)), CoderHelpers.toByteArray(WindowedValue.of(3, now, window, paneInfo), wvCoder)), new Tuple2(new ByteArray(CoderHelpers.toByteArrayWithTs(2, coder, now.plus(Duration.millis(2)))), CoderHelpers.toByteArray(WindowedValue.of(4, now.plus(Duration.millis(2)), window, paneInfo), wvCoder)));
    Iterable<Tuple2<ByteArray, byte[]>> concat = Iterables.concat(firstKey, secondKey);
    Iterator<Iterator<WindowedValue<KV<Integer, Integer>>>> keySplit;
    keySplit = TransformTranslator.splitBySameKey(concat.iterator(), coder, wvCoder);
    for (int i = 0; i < 2; i++) {
        Iterator<WindowedValue<KV<Integer, Integer>>> iter = keySplit.next();
        List<WindowedValue<KV<Integer, Integer>>> list = new ArrayList<>();
        Iterators.addAll(list, iter);
        if (i == 0) {
            // first key
            assertEquals(Arrays.asList(WindowedValue.of(KV.of(1, 1), now, window, paneInfo), WindowedValue.of(KV.of(1, 2), now.plus(Duration.millis(1)), window, paneInfo)), list);
        } else {
            // second key
            assertEquals(Arrays.asList(WindowedValue.of(KV.of(2, 3), now, window, paneInfo), WindowedValue.of(KV.of(2, 4), now.plus(Duration.millis(2)), window, paneInfo)), list);
        }
    }
}
Also used : VarIntCoder(org.apache.beam.sdk.coders.VarIntCoder) Instant(org.joda.time.Instant) ArrayList(java.util.ArrayList) KV(org.apache.beam.sdk.values.KV) WindowedValue(org.apache.beam.sdk.util.WindowedValue) Tuple2(scala.Tuple2) PaneInfo(org.apache.beam.sdk.transforms.windowing.PaneInfo) Iterator(java.util.Iterator) ByteArray(org.apache.beam.runners.spark.util.ByteArray) GlobalWindow(org.apache.beam.sdk.transforms.windowing.GlobalWindow) Test(org.junit.Test)

Example 4 with GlobalWindow

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

the class RepeatedlyStateMachineTest method testRepeatedlyElementCount.

@Test
public void testRepeatedlyElementCount() throws Exception {
    SimpleTriggerStateMachineTester<GlobalWindow> tester = TriggerStateMachineTester.forTrigger(RepeatedlyStateMachine.forever(AfterPaneStateMachine.elementCountAtLeast(5)), new GlobalWindows());
    GlobalWindow window = GlobalWindow.INSTANCE;
    tester.injectElements(1);
    assertFalse(tester.shouldFire(window));
    tester.injectElements(2, 3, 4, 5);
    assertTrue(tester.shouldFire(window));
    tester.fireIfShouldFire(window);
    assertFalse(tester.shouldFire(window));
}
Also used : GlobalWindows(org.apache.beam.sdk.transforms.windowing.GlobalWindows) GlobalWindow(org.apache.beam.sdk.transforms.windowing.GlobalWindow) Test(org.junit.Test)

Example 5 with GlobalWindow

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

the class RepeatedlyStateMachineTest method testRepeatedlyProcessingTime.

@Test
public void testRepeatedlyProcessingTime() throws Exception {
    SimpleTriggerStateMachineTester<GlobalWindow> tester = TriggerStateMachineTester.forTrigger(RepeatedlyStateMachine.forever(AfterProcessingTimeStateMachine.pastFirstElementInPane().plusDelayOf(Duration.standardMinutes(15))), new GlobalWindows());
    GlobalWindow window = GlobalWindow.INSTANCE;
    tester.injectElements(1);
    assertFalse(tester.shouldFire(window));
    tester.advanceProcessingTime(new Instant(0).plus(Duration.standardMinutes(15)));
    assertTrue(tester.shouldFire(window));
    tester.fireIfShouldFire(window);
    assertFalse(tester.shouldFire(window));
}
Also used : GlobalWindows(org.apache.beam.sdk.transforms.windowing.GlobalWindows) Instant(org.joda.time.Instant) GlobalWindow(org.apache.beam.sdk.transforms.windowing.GlobalWindow) Test(org.junit.Test)

Aggregations

GlobalWindow (org.apache.beam.sdk.transforms.windowing.GlobalWindow)17 Test (org.junit.Test)13 Instant (org.joda.time.Instant)9 GlobalWindows (org.apache.beam.sdk.transforms.windowing.GlobalWindows)8 WindowedValue (org.apache.beam.sdk.util.WindowedValue)4 ByteBuffer (java.nio.ByteBuffer)3 BoundedWindow (org.apache.beam.sdk.transforms.windowing.BoundedWindow)3 ArrayList (java.util.ArrayList)2 HashMap (java.util.HashMap)2 Map (java.util.Map)2 WindowMatchers.isSingleWindowedValue (org.apache.beam.runners.core.WindowMatchers.isSingleWindowedValue)2 WindowMatchers.isWindowedValue (org.apache.beam.runners.core.WindowMatchers.isWindowedValue)2 StreamRecordStripper.stripStreamRecordFromWindowedValue (org.apache.beam.runners.flink.translation.wrappers.streaming.StreamRecordStripper.stripStreamRecordFromWindowedValue)2 KV (org.apache.beam.sdk.values.KV)2 ByteString (org.apache.beam.vendor.grpc.v1p43p2.com.google.protobuf.ByteString)2 ImmutableMap (org.apache.beam.vendor.guava.v26_0_jre.com.google.common.collect.ImmutableMap)2 Matchers.containsString (org.hamcrest.Matchers.containsString)2 InstructionOutput (com.google.api.services.dataflow.model.InstructionOutput)1 MultiOutputInfo (com.google.api.services.dataflow.model.MultiOutputInfo)1 ParDoInstruction (com.google.api.services.dataflow.model.ParDoInstruction)1