Ejemplo n.º 1
0
 public void injectElement(InputT value, Instant timestamp) throws Exception {
   Collection<W> windows =
       windowFn.assignWindows(
           new TriggerTester.StubAssignContext<W>(
               windowFn, value, timestamp, Arrays.asList(GlobalWindow.INSTANCE)));
   logInteraction(
       "Element %s at time %d put in windows %s", value, timestamp.getMillis(), windows);
   runner.processElement(WindowedValue.of(value, timestamp, windows, PaneInfo.NO_FIRING));
 }
Ejemplo n.º 2
0
 @Override
 public void outputWindowedValue(
     KV<String, OutputT> output,
     Instant timestamp,
     Collection<? extends BoundedWindow> windows,
     PaneInfo pane) {
   // Copy the output value (using coders) before capturing it.
   KV<String, OutputT> copy =
       SerializableUtils.<KV<String, OutputT>>ensureSerializableByCoder(
           KvCoder.of(StringUtf8Coder.of(), outputCoder), output, "outputForWindow");
   WindowedValue<KV<String, OutputT>> value = WindowedValue.of(copy, timestamp, windows, pane);
   logInteraction("Outputting: %s", value);
   outputs.add(value);
 }
  @Override
  public void flatMap(WindowedValue<IN> value, Collector<WindowedValue<OUTFL>> out)
      throws Exception {
    this.initContext(doFn, out);

    // for each window the element belongs to, create a new copy here.
    Collection<? extends BoundedWindow> windows = value.getWindows();
    if (windows.size() <= 1) {
      processElement(value);
    } else {
      for (BoundedWindow window : windows) {
        processElement(
            WindowedValue.of(value.getValue(), value.getTimestamp(), window, value.getPane()));
      }
    }
  }
  protected <T> WindowedValue<T> makeWindowedValue(
      T output, Instant timestamp, Collection<? extends BoundedWindow> windows, PaneInfo pane) {
    final Instant inputTimestamp = timestamp;
    final WindowFn windowFn = windowingStrategy.getWindowFn();

    if (timestamp == null) {
      timestamp = BoundedWindow.TIMESTAMP_MIN_VALUE;
    }

    if (windows == null) {
      try {
        windows =
            windowFn.assignWindows(
                windowFn.new AssignContext() {
                  @Override
                  public Object element() {
                    throw new UnsupportedOperationException(
                        "WindowFn attempted to access input element when none was available"); // TODO: 12/16/15 aljoscha's comment in slack
                  }

                  @Override
                  public Instant timestamp() {
                    if (inputTimestamp == null) {
                      throw new UnsupportedOperationException(
                          "WindowFn attempted to access input timestamp when none was available");
                    }
                    return inputTimestamp;
                  }

                  @Override
                  public Collection<? extends BoundedWindow> windows() {
                    throw new UnsupportedOperationException(
                        "WindowFn attempted to access input windows when none were available");
                  }
                });
      } catch (Exception e) {
        Throwables.propagateIfInstanceOf(e, UserCodeException.class);
        throw new UserCodeException(e);
      }
    }

    return WindowedValue.of(output, timestamp, windows, pane);
  }