Java Code Examples for org.apache.beam.sdk.transforms.windowing.GlobalWindow#INSTANCE

The following examples show how to use org.apache.beam.sdk.transforms.windowing.GlobalWindow#INSTANCE . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: RepeatedlyStateMachineTest.java    From beam with Apache License 2.0 6 votes vote down vote up
@Test
public void testRepeatedlyAfterFirstElementCount() throws Exception {
  SimpleTriggerStateMachineTester<GlobalWindow> tester =
      TriggerStateMachineTester.forTrigger(
          RepeatedlyStateMachine.forever(
              AfterFirstStateMachine.of(
                  AfterProcessingTimeStateMachine.pastFirstElementInPane()
                      .plusDelayOf(Duration.standardMinutes(15)),
                  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));
}
 
Example 2
Source File: RepeatedlyStateMachineTest.java    From beam with Apache License 2.0 6 votes vote down vote up
@Test
public void testRepeatedlyAfterFirstProcessingTime() throws Exception {
  SimpleTriggerStateMachineTester<GlobalWindow> tester =
      TriggerStateMachineTester.forTrigger(
          RepeatedlyStateMachine.forever(
              AfterFirstStateMachine.of(
                  AfterProcessingTimeStateMachine.pastFirstElementInPane()
                      .plusDelayOf(Duration.standardMinutes(15)),
                  AfterPaneStateMachine.elementCountAtLeast(5))),
          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));
}
 
Example 3
Source File: RepeatedlyStateMachineTest.java    From beam with Apache License 2.0 6 votes vote down vote up
@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));
}
 
Example 4
Source File: RepeatedlyStateMachineTest.java    From beam with Apache License 2.0 6 votes vote down vote up
@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));
}
 
Example 5
Source File: SparkCombineFnTest.java    From beam with Apache License 2.0 6 votes vote down vote up
<V> WindowFn<V, BoundedWindow>.AssignContext assignContext(
    WindowFn<V, BoundedWindow> windowFn, V value, Instant timestamp) {
  return windowFn.new AssignContext() {
    @Override
    public V element() {
      return value;
    }

    @Override
    public Instant timestamp() {
      return timestamp;
    }

    @Override
    public BoundedWindow window() {
      return GlobalWindow.INSTANCE;
    }
  };
}
 
Example 6
Source File: ExecutableStageDoFnOperatorTest.java    From beam with Apache License 2.0 5 votes vote down vote up
@Test
public void testEnsureStateCleanupWithKeyedInputCleanupTimer() {
  InMemoryTimerInternals inMemoryTimerInternals = new InMemoryTimerInternals();
  KeyedStateBackend keyedStateBackend = Mockito.mock(KeyedStateBackend.class);
  Lock stateBackendLock = Mockito.mock(Lock.class);
  StringUtf8Coder keyCoder = StringUtf8Coder.of();
  GlobalWindow window = GlobalWindow.INSTANCE;
  GlobalWindow.Coder windowCoder = GlobalWindow.Coder.INSTANCE;

  // Test that cleanup timer is set correctly
  ExecutableStageDoFnOperator.CleanupTimer cleanupTimer =
      new ExecutableStageDoFnOperator.CleanupTimer<>(
          inMemoryTimerInternals,
          stateBackendLock,
          WindowingStrategy.globalDefault(),
          keyCoder,
          windowCoder,
          keyedStateBackend);
  cleanupTimer.setForWindow(KV.of("key", "string"), window);

  Mockito.verify(stateBackendLock).lock();
  ByteBuffer key = FlinkKeyUtils.encodeKey("key", keyCoder);
  Mockito.verify(keyedStateBackend).setCurrentKey(key);
  assertThat(
      inMemoryTimerInternals.getNextTimer(TimeDomain.EVENT_TIME),
      is(window.maxTimestamp().plus(1)));
  Mockito.verify(stateBackendLock).unlock();
}
 
Example 7
Source File: BufferedElementsTest.java    From beam with Apache License 2.0 5 votes vote down vote up
@Test
public void testCoder() throws IOException {

  StringUtf8Coder elementCoder = StringUtf8Coder.of();
  // Generics fail to see here that this is Coder<BoundedWindow>
  org.apache.beam.sdk.coders.Coder windowCoder = GlobalWindow.Coder.INSTANCE;
  WindowedValue.WindowedValueCoder windowedValueCoder =
      WindowedValue.FullWindowedValueCoder.of(elementCoder, windowCoder);
  KV<String, Integer> key = KV.of("one", 1);
  BufferedElements.Coder coder = new BufferedElements.Coder(windowedValueCoder, windowCoder, key);

  BufferedElement element =
      new BufferedElements.Element(
          WindowedValue.of("test", new Instant(2), GlobalWindow.INSTANCE, PaneInfo.NO_FIRING));
  BufferedElement timerElement =
      new BufferedElements.Timer(
          "timerId",
          "timerId",
          key,
          GlobalWindow.INSTANCE,
          new Instant(1),
          new Instant(1),
          TimeDomain.EVENT_TIME);

  testRoundTrip(ImmutableList.of(element), coder);
  testRoundTrip(ImmutableList.of(timerElement), coder);
  testRoundTrip(ImmutableList.of(element, timerElement), coder);
  testRoundTrip(ImmutableList.of(element, timerElement, element), coder);
  testRoundTrip(ImmutableList.of(element, element, element, timerElement, timerElement), coder);
}
 
Example 8
Source File: FnApiWindowMappingFnTest.java    From beam with Apache License 2.0 5 votes vote down vote up
@Test
public void testWindowMapping() throws Exception {
  TestSdkHarness testSdkHarness = new TestSdkHarness(GlobalWindow.INSTANCE);

  FnApiWindowMappingFn windowMappingFn =
      new FnApiWindowMappingFn(
          IdGenerators.decrementingLongs(),
          testSdkHarness,
          DATA_SERVICE,
          testSdkHarness,
          WINDOW_MAPPING_SPEC,
          IntervalWindowCoder.of(),
          GlobalWindow.Coder.INSTANCE);

  // Check mapping an element returns the expected result.
  BoundedWindow inputWindow = new IntervalWindow(Instant.now(), Duration.standardMinutes(1));
  assertEquals(GlobalWindow.INSTANCE, windowMappingFn.getSideInputWindow(inputWindow));
  assertEquals(inputWindow, ((KV) testSdkHarness.getInputValues().get(0).getValue()).getValue());

  // Check mapping a different element returns the expected result.
  BoundedWindow inputWindow2 = new IntervalWindow(Instant.now(), Duration.standardMinutes(2));
  assertEquals(GlobalWindow.INSTANCE, windowMappingFn.getSideInputWindow(inputWindow2));
  assertEquals(inputWindow2, ((KV) testSdkHarness.getInputValues().get(1).getValue()).getValue());

  // Check that mapping the same element returns a cached result.
  assertEquals(GlobalWindow.INSTANCE, windowMappingFn.getSideInputWindow(inputWindow));
  assertEquals(2, testSdkHarness.getInputValues().size());
}
 
Example 9
Source File: WindowedValue.java    From beam with Apache License 2.0 4 votes vote down vote up
@Override
public BoundedWindow getWindow() {
  return GlobalWindow.INSTANCE;
}
 
Example 10
Source File: WindowedValue.java    From beam with Apache License 2.0 4 votes vote down vote up
@Override
public BoundedWindow getWindow() {
  return GlobalWindow.INSTANCE;
}
 
Example 11
Source File: WindowFnTestUtils.java    From beam with Apache License 2.0 4 votes vote down vote up
@Override
public BoundedWindow window() {
  return GlobalWindow.INSTANCE;
}
 
Example 12
Source File: FileIO.java    From beam with Apache License 2.0 4 votes vote down vote up
/**
 * Defines a default {@link FileNaming} which will use the prefix and suffix supplied to create
 * a name based on the window, pane, number of shards, shard index, and compression. Removes
 * window when in the {@link GlobalWindow} and pane info when it is the only firing of the pane.
 */
public static FileNaming defaultNaming(
    final ValueProvider<String> prefix, final ValueProvider<String> suffix) {
  return (window, pane, numShards, shardIndex, compression) -> {
    checkArgument(window != null, "window can not be null");
    checkArgument(pane != null, "pane can not be null");
    checkArgument(compression != null, "compression can not be null");
    StringBuilder res = new StringBuilder(prefix.get());
    if (window != GlobalWindow.INSTANCE) {
      if (res.length() > 0) {
        res.append("-");
      }
      checkArgument(
          window instanceof IntervalWindow,
          "defaultNaming() supports only windows of type %s, " + "but got window %s of type %s",
          IntervalWindow.class.getSimpleName(),
          window,
          window.getClass().getSimpleName());
      IntervalWindow iw = (IntervalWindow) window;
      res.append(iw.start().toString()).append("-").append(iw.end().toString());
    }
    boolean isOnlyFiring = pane.isFirst() && pane.isLast();
    if (!isOnlyFiring) {
      if (res.length() > 0) {
        res.append("-");
      }
      res.append(pane.getIndex());
    }
    if (res.length() > 0) {
      res.append("-");
    }
    String numShardsStr = String.valueOf(numShards);
    // A trillion shards per window per pane ought to be enough for everybody.
    DecimalFormat df =
        new DecimalFormat("000000000000".substring(0, Math.max(5, numShardsStr.length())));
    res.append(df.format(shardIndex)).append("-of-").append(df.format(numShards));
    res.append(suffix.get());
    res.append(compression.getSuggestedSuffix());
    return res.toString();
  };
}
 
Example 13
Source File: WindowIntoTranslationTest.java    From beam with Apache License 2.0 4 votes vote down vote up
@Override
public BoundedWindow assignWindow(Instant timestamp) {
  return GlobalWindow.INSTANCE;
}
 
Example 14
Source File: GroupNonMergingWindowsFunctionsTest.java    From beam with Apache License 2.0 4 votes vote down vote up
static <K, V> ItemFactory<K, V, GlobalWindow> forGlogalWindow(
    Coder<K> keyCoder, FullWindowedValueCoder<KV<K, V>> winValCoder) {
  return new ItemFactory<>(
      keyCoder, winValCoder, GlobalWindow.Coder.INSTANCE, GlobalWindow.INSTANCE);
}