Java Code Examples for org.apache.beam.sdk.transforms.windowing.PaneInfo#createPane()

The following examples show how to use org.apache.beam.sdk.transforms.windowing.PaneInfo#createPane() . 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: WindowedFilenamePolicyTest.java    From DataflowTemplates with Apache License 2.0 6 votes vote down vote up
/**
 * Tests that windowedFilename() constructs the filename correctly according to the parameters
 * when using ValueProviders.
 */
@Test
public void testWindowedFilenameFormatValueProvider() throws IOException {
  // Arrange
  //
  ResourceId outputDirectory = getBaseTempDirectory();
  WindowedContext context = mock(WindowedContext.class);
  BoundedWindow window = mock(BoundedWindow.class);
  PaneInfo paneInfo = PaneInfo.createPane(false, true, Timing.ON_TIME, 0, 0);
  WindowedFilenamePolicy policy =
      new WindowedFilenamePolicy(
          StaticValueProvider.of(outputDirectory.toString()),
          StaticValueProvider.of("output"),
          StaticValueProvider.of("-SSS-of-NNN"),
          StaticValueProvider.of(".txt"));

  // Act
  //
  ResourceId filename =
      policy.windowedFilename(1, 1, window, paneInfo, new TestOutputFileHints());

  // Assert
  //
  assertThat(filename, is(notNullValue()));
  assertThat(filename.getFilename(), is(equalTo("output-001-of-001.txt")));
}
 
Example 2
Source File: WindowedFilenamePolicyTest.java    From DataflowTemplates with Apache License 2.0 6 votes vote down vote up
/**
 * Tests that windowedFilename() constructs the filename correctly according to the parameters
 * when using Strings.
 */
@Test
public void testWindowedFilenameFormatString() throws IOException {
  // Arrange
  //
  ResourceId outputDirectory = getBaseTempDirectory();
  WindowedContext context = mock(WindowedContext.class);
  BoundedWindow window = mock(BoundedWindow.class);
  PaneInfo paneInfo = PaneInfo.createPane(false, true, Timing.ON_TIME, 0, 0);
  WindowedFilenamePolicy policy =
      new WindowedFilenamePolicy(
          outputDirectory.toString(), "string-output", "-SSS-of-NNN", ".csv");
  // Act
  //
  ResourceId filename =
      policy.windowedFilename(1, 1, window, paneInfo, new TestOutputFileHints());

  // Assert
  //
  assertThat(filename, is(notNullValue()));
  assertThat(filename.getFilename(), is(equalTo("string-output-001-of-001.csv")));
}
 
Example 3
Source File: WindowedFilenamePolicyTest.java    From DataflowTemplates with Apache License 2.0 6 votes vote down vote up
/**
 * Tests that windowedFilename() constructs the filename correctly according to the parameters
 * when the filename suffix is a null value.
 */
@Test
public void testWindowedFilenameNullSuffix() throws IOException {
  // Arrange
  //
  ResourceId outputDirectory = getBaseTempDirectory();
  WindowedContext context = mock(WindowedContext.class);
  BoundedWindow window = mock(BoundedWindow.class);
  PaneInfo paneInfo = PaneInfo.createPane(false, true, Timing.ON_TIME, 0, 0);
  WindowedFilenamePolicy policy =
      new WindowedFilenamePolicy(
          StaticValueProvider.of(outputDirectory.toString()),
          StaticValueProvider.of("output"),
          StaticValueProvider.of("-SSS-of-NNN"),
          StaticValueProvider.of(null));

  // Act
  //
  ResourceId filename =
      policy.windowedFilename(1, 1, window, paneInfo, new TestOutputFileHints());
  // Assert
  //
  assertThat(filename, is(notNullValue()));
  assertThat(filename.getFilename(), is(equalTo("output-001-of-001")));
}
 
Example 4
Source File: WindowedValueTest.java    From beam with Apache License 2.0 6 votes vote down vote up
@Test
public void testExplodeWindowsManyWindowsMultipleWindowedValues() {
  Instant now = Instant.now();
  BoundedWindow centerWindow = new IntervalWindow(now.minus(1000L), now.plus(1000L));
  BoundedWindow pastWindow = new IntervalWindow(now.minus(1500L), now.plus(500L));
  BoundedWindow futureWindow = new IntervalWindow(now.minus(500L), now.plus(1500L));
  BoundedWindow futureFutureWindow = new IntervalWindow(now, now.plus(2000L));
  PaneInfo pane = PaneInfo.createPane(false, false, Timing.ON_TIME, 3L, 0L);
  WindowedValue<String> value =
      WindowedValue.of(
          "foo",
          now,
          ImmutableList.of(pastWindow, centerWindow, futureWindow, futureFutureWindow),
          pane);

  assertThat(
      value.explodeWindows(),
      containsInAnyOrder(
          WindowedValue.of("foo", now, futureFutureWindow, pane),
          WindowedValue.of("foo", now, futureWindow, pane),
          WindowedValue.of("foo", now, centerWindow, pane),
          WindowedValue.of("foo", now, pastWindow, pane)));

  assertThat(value.isSingleWindowedValue(), equalTo(false));
}
 
Example 5
Source File: WindowedFilenamePolicyTest.java    From DataflowTemplates with Apache License 2.0 5 votes vote down vote up
/**
 * Tests that windowedFilename() produces the correct directory when the directory has dynamic
 * date variables.
 */
@Test
public void testWithDynamicDirectory() throws IOException {
  // Arrange
  //
  ResourceId outputDirectory = getBaseTempDirectory()
      .resolve("YYYY/MM/DD/HH:mm", StandardResolveOptions.RESOLVE_DIRECTORY);
  WindowedContext context = mock(WindowedContext.class);
  IntervalWindow window = mock(IntervalWindow.class);
  PaneInfo paneInfo = PaneInfo.createPane(false, true, Timing.ON_TIME, 0, 0);
  Instant windowBegin = new DateTime(2017, 1, 8, 10, 55, 0).toInstant();
  Instant windowEnd = new DateTime(2017, 1, 8, 10, 56, 0).toInstant();
  when(window.maxTimestamp()).thenReturn(windowEnd);
  when(window.start()).thenReturn(windowBegin);
  when(window.end()).thenReturn(windowEnd);

  WindowedFilenamePolicy policy =
      new WindowedFilenamePolicy(
          StaticValueProvider.of(outputDirectory.toString()),
          StaticValueProvider.of("output"),
          StaticValueProvider.of("-SSS-of-NNN"),
          StaticValueProvider.of(null));

  // Act
  //
  ResourceId filename =
      policy.windowedFilename(1, 1, window, paneInfo, new TestOutputFileHints());

  // Assert
  //
  assertThat(filename, is(notNullValue()));
  assertThat(
      filename.getCurrentDirectory().toString().endsWith("2017/01/08/10:56/"), is(equalTo(true)));
  assertThat(filename.getFilename(), is(equalTo("output-001-of-001")));
}
 
Example 6
Source File: PaneInfoTracker.java    From beam with Apache License 2.0 4 votes vote down vote up
private <W> PaneInfo describePane(
    Object key, Instant windowMaxTimestamp, PaneInfo previousPane, boolean isFinal) {
  boolean isFirst = previousPane == null;
  Timing previousTiming = isFirst ? null : previousPane.getTiming();
  long index = isFirst ? 0 : previousPane.getIndex() + 1;
  long nonSpeculativeIndex = isFirst ? 0 : previousPane.getNonSpeculativeIndex() + 1;
  Instant outputWM = timerInternals.currentOutputWatermarkTime();
  Instant inputWM = timerInternals.currentInputWatermarkTime();

  // True if it is not possible to assign the element representing this pane a timestamp
  // which will make an ON_TIME pane for any following computation.
  // Ie true if the element's latest possible timestamp is before the current output watermark.
  boolean isLateForOutput = outputWM != null && windowMaxTimestamp.isBefore(outputWM);

  // True if all emitted panes (if any) were EARLY panes.
  // Once the ON_TIME pane has fired, all following panes must be considered LATE even
  // if the output watermark is behind the end of the window.
  boolean onlyEarlyPanesSoFar = previousTiming == null || previousTiming == Timing.EARLY;

  // True is the input watermark hasn't passed the window's max timestamp.
  boolean isEarlyForInput = !inputWM.isAfter(windowMaxTimestamp);

  Timing timing;
  if (isLateForOutput || !onlyEarlyPanesSoFar) {
    // The output watermark has already passed the end of this window, or we have already
    // emitted a non-EARLY pane. Irrespective of how this pane was triggered we must
    // consider this pane LATE.
    timing = Timing.LATE;
  } else if (isEarlyForInput) {
    // This is an EARLY firing.
    timing = Timing.EARLY;
    nonSpeculativeIndex = -1;
  } else {
    // This is the unique ON_TIME firing for the window.
    timing = Timing.ON_TIME;
  }

  WindowTracing.debug(
      "describePane: {} pane (prev was {}) for key:{}; windowMaxTimestamp:{}; "
          + "inputWatermark:{}; outputWatermark:{}; isLateForOutput:{}",
      timing,
      previousTiming,
      key,
      windowMaxTimestamp,
      inputWM,
      outputWM,
      isLateForOutput);

  if (previousPane != null) {
    // Timing transitions should follow EARLY* ON_TIME? LATE*
    switch (previousTiming) {
      case EARLY:
        checkState(
            timing == Timing.EARLY || timing == Timing.ON_TIME || timing == Timing.LATE,
            "EARLY cannot transition to %s",
            timing);
        break;
      case ON_TIME:
        checkState(timing == Timing.LATE, "ON_TIME cannot transition to %s", timing);
        break;
      case LATE:
        checkState(timing == Timing.LATE, "LATE cannot transtion to %s", timing);
        break;
      case UNKNOWN:
        break;
    }
    checkState(!previousPane.isLast(), "Last pane was not last after all.");
  }

  return PaneInfo.createPane(isFirst, isFinal, timing, index, nonSpeculativeIndex);
}
 
Example 7
Source File: WindmillKeyedWorkItemTest.java    From beam with Apache License 2.0 4 votes vote down vote up
private PaneInfo paneInfo(int index) {
  return PaneInfo.createPane(false, false, Timing.EARLY, index, -1);
}