Java Code Examples for org.apache.beam.sdk.values.WindowingStrategy#getTrigger()

The following examples show how to use org.apache.beam.sdk.values.WindowingStrategy#getTrigger() . 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: Window.java    From beam with Apache License 2.0 6 votes vote down vote up
private void applicableTo(PCollection<?> input) {
  WindowingStrategy<?, ?> outputStrategy =
      getOutputStrategyInternal(input.getWindowingStrategy());

  // Make sure that the windowing strategy is complete & valid.
  if (outputStrategy.isTriggerSpecified()
      && !(outputStrategy.getTrigger() instanceof DefaultTrigger)
      && !(outputStrategy.getWindowFn() instanceof GlobalWindows)
      && !outputStrategy.isAllowedLatenessSpecified()) {
    throw new IllegalArgumentException(
        "Except when using GlobalWindows,"
            + " calling .triggering() to specify a trigger requires that the allowed lateness"
            + " be specified using .withAllowedLateness() to set the upper bound on how late"
            + " data can arrive before being dropped. See Javadoc for more details.");
  }

  if (!outputStrategy.isModeSpecified() && canProduceMultiplePanes(outputStrategy)) {
    throw new IllegalArgumentException(
        "Calling .triggering() to specify a trigger or calling .withAllowedLateness() to"
            + " specify an allowed lateness greater than zero requires that the accumulation"
            + " mode be specified using .discardingFiredPanes() or .accumulatingFiredPanes()."
            + " See Javadoc for more details.");
  }
}
 
Example 2
Source File: GroupByKey.java    From beam with Apache License 2.0 6 votes vote down vote up
private static boolean triggerIsSafe(WindowingStrategy<?, ?> windowingStrategy) {
  if (!windowingStrategy.getTrigger().mayFinish()) {
    return true;
  }

  if (windowingStrategy.getTrigger() instanceof NeverTrigger) {
    return true;
  }

  if (windowingStrategy.getTrigger() instanceof FromEndOfWindow
      && windowingStrategy.getAllowedLateness().getMillis() == 0) {
    return true;
  }

  if (windowingStrategy.getTrigger() instanceof AfterWatermarkEarlyAndLate
      && windowingStrategy.getAllowedLateness().getMillis() == 0) {
    return true;
  }

  if (windowingStrategy.getTrigger() instanceof AfterWatermarkEarlyAndLate
      && ((AfterWatermarkEarlyAndLate) windowingStrategy.getTrigger()).getLateTrigger() != null) {
    return true;
  }

  return false;
}
 
Example 3
Source File: BeamCoGBKJoinRel.java    From beam with Apache License 2.0 5 votes vote down vote up
private boolean triggersOncePerWindow(WindowingStrategy windowingStrategy) {
  Trigger trigger = windowingStrategy.getTrigger();

  return !(windowingStrategy.getWindowFn() instanceof GlobalWindows)
      && trigger instanceof DefaultTrigger
      && ZERO.equals(windowingStrategy.getAllowedLateness());
}
 
Example 4
Source File: BeamAggregationRel.java    From beam with Apache License 2.0 5 votes vote down vote up
/**
 * Performs the same check as {@link GroupByKey}, provides more context in exception.
 *
 * <p>Verifies that the input PCollection is bounded, or that there is windowing/triggering
 * being used. Without this, the watermark (at end of global window) will never be reached.
 *
 * <p>Throws {@link UnsupportedOperationException} if validation fails.
 */
private void validateWindowIsSupported(PCollection<Row> upstream) {
  WindowingStrategy<?, ?> windowingStrategy = upstream.getWindowingStrategy();
  if (windowingStrategy.getWindowFn() instanceof GlobalWindows
      && windowingStrategy.getTrigger() instanceof DefaultTrigger
      && upstream.isBounded() != BOUNDED) {

    throw new UnsupportedOperationException(
        "Please explicitly specify windowing in SQL query using HOP/TUMBLE/SESSION functions "
            + "(default trigger will be used in this case). "
            + "Unbounded input with global windowing and default trigger is not supported "
            + "in Beam SQL aggregations. "
            + "See GroupByKey section in Beam Programming Guide");
  }
}
 
Example 5
Source File: Flatten.java    From beam with Apache License 2.0 5 votes vote down vote up
@Override
public PCollection<T> expand(PCollectionList<T> inputs) {
  WindowingStrategy<?, ?> windowingStrategy;
  IsBounded isBounded = IsBounded.BOUNDED;
  if (!inputs.getAll().isEmpty()) {
    windowingStrategy = inputs.get(0).getWindowingStrategy();
    for (PCollection<?> input : inputs.getAll()) {
      WindowingStrategy<?, ?> other = input.getWindowingStrategy();
      if (!windowingStrategy.getWindowFn().isCompatible(other.getWindowFn())) {
        throw new IllegalStateException(
            "Inputs to Flatten had incompatible window windowFns: "
                + windowingStrategy.getWindowFn()
                + ", "
                + other.getWindowFn());
      }

      if (!windowingStrategy.getTrigger().isCompatible(other.getTrigger())) {
        throw new IllegalStateException(
            "Inputs to Flatten had incompatible triggers: "
                + windowingStrategy.getTrigger()
                + ", "
                + other.getTrigger());
      }
      isBounded = isBounded.and(input.isBounded());
    }
  } else {
    windowingStrategy = WindowingStrategy.globalDefault();
  }

  return PCollection.createPrimitiveOutputInternal(
      inputs.getPipeline(),
      windowingStrategy,
      isBounded,
      // Take coder from first collection. If there are none, will be left unspecified.
      inputs.getAll().isEmpty() ? null : inputs.get(0).getCoder());
}
 
Example 6
Source File: Window.java    From beam with Apache License 2.0 5 votes vote down vote up
private boolean canProduceMultiplePanes(WindowingStrategy<?, ?> strategy) {
  // The default trigger is Repeatedly.forever(AfterWatermark.pastEndOfWindow()); This fires
  // for every late-arriving element if allowed lateness is nonzero, and thus we must have
  // an accumulating mode specified
  boolean dataCanArriveLate =
      !(strategy.getWindowFn() instanceof GlobalWindows)
          && strategy.getAllowedLateness().getMillis() > 0;
  boolean hasCustomTrigger = !(strategy.getTrigger() instanceof DefaultTrigger);
  return dataCanArriveLate || hasCustomTrigger;
}
 
Example 7
Source File: GroupByKey.java    From beam with Apache License 2.0 5 votes vote down vote up
public static void applicableTo(PCollection<?> input) {
  WindowingStrategy<?, ?> windowingStrategy = input.getWindowingStrategy();
  // Verify that the input PCollection is bounded, or that there is windowing/triggering being
  // used. Without this, the watermark (at end of global window) will never be reached.
  if (windowingStrategy.getWindowFn() instanceof GlobalWindows
      && windowingStrategy.getTrigger() instanceof DefaultTrigger
      && input.isBounded() != IsBounded.BOUNDED) {
    throw new IllegalStateException(
        "GroupByKey cannot be applied to non-bounded PCollection in the GlobalWindow without a"
            + " trigger. Use a Window.into or Window.triggering transform prior to GroupByKey.");
  }

  // Validate the window merge function.
  if (windowingStrategy.getWindowFn() instanceof InvalidWindows) {
    String cause = ((InvalidWindows<?>) windowingStrategy.getWindowFn()).getCause();
    throw new IllegalStateException(
        "GroupByKey must have a valid Window merge function.  " + "Invalid because: " + cause);
  }

  // Validate that the trigger does not finish before garbage collection time
  if (!triggerIsSafe(windowingStrategy)) {
    throw new IllegalArgumentException(
        String.format(
            "Unsafe trigger '%s' may lose data, did you mean to wrap it in"
                + "`Repeatedly.forever(...)`?%nSee "
                + "https://s.apache.org/finishing-triggers-drop-data "
                + "for details.",
            windowingStrategy.getTrigger()));
  }
}
 
Example 8
Source File: DataflowPipelineTranslator.java    From beam with Apache License 2.0 5 votes vote down vote up
private <K, V> void groupByKeyHelper(
    GroupByKey<K, V> transform, TranslationContext context) {
  StepTranslationContext stepContext = context.addStep(transform, "GroupByKey");
  PCollection<KV<K, V>> input = context.getInput(transform);
  stepContext.addInput(PropertyNames.PARALLEL_INPUT, input);
  stepContext.addOutput(PropertyNames.OUTPUT, context.getOutput(transform));

  WindowingStrategy<?, ?> windowingStrategy = input.getWindowingStrategy();
  boolean isStreaming =
      context.getPipelineOptions().as(StreamingOptions.class).isStreaming();
  boolean allowCombinerLifting =
      windowingStrategy.getWindowFn().isNonMerging()
          && windowingStrategy.getWindowFn().assignsToOneWindow();
  if (isStreaming) {
    allowCombinerLifting &= transform.fewKeys();
    // TODO: Allow combiner lifting on the non-default trigger, as appropriate.
    allowCombinerLifting &= (windowingStrategy.getTrigger() instanceof DefaultTrigger);
  }
  stepContext.addInput(PropertyNames.DISALLOW_COMBINER_LIFTING, !allowCombinerLifting);
  stepContext.addInput(
      PropertyNames.SERIALIZED_FN,
      byteArrayToJsonString(
          serializeWindowingStrategy(windowingStrategy, context.getPipelineOptions())));
  stepContext.addInput(
      PropertyNames.IS_MERGING_WINDOW_FN,
      !windowingStrategy.getWindowFn().isNonMerging());
}
 
Example 9
Source File: StreamingGroupAlsoByWindowReshuffleFn.java    From beam with Apache License 2.0 4 votes vote down vote up
public static boolean isReshuffle(WindowingStrategy<?, ?> strategy) {
  return strategy.getTrigger() instanceof ReshuffleTrigger;
}
 
Example 10
Source File: BatchGroupAlsoByWindowReshuffleFn.java    From beam with Apache License 2.0 4 votes vote down vote up
public static boolean isReshuffle(WindowingStrategy<?, ?> strategy) {
  return strategy.getTrigger() instanceof ReshuffleTrigger;
}