apache/beam · error · UnsupportedOperationException

Accumulators not supported in this context

Error message

Accumulators not supported in this context

What it means

SingleValueContext.getCounter throws UnsupportedOperationException when the context was created without a wrapped accumulator context (wrap == null). Single-value element contexts have no accumulator backing, so counters are unavailable in that scope.

Solutions

  1. Remove counter usage from the single-value functor body
  2. Use a context/windowing variant that provides accumulators (wrap != null), e.g. via a translation path that supplies AccumulatorProvider
  3. Accumulate results into the returned single value instead of counters
  4. Guard with a check on context capabilities before calling getCounter

Example fix

// before
context.getCounter("my-counter").increment();
// after
// accumulate in the value instead of a counter
value.add(1); // or drop metrics in this context
Defensive patterns

Strategy: type-guard

Validate before calling

if (context instanceof SingleValueContext && !((SingleValueContext) context).hasAccumulators()) { /* skip counters */ }

Type guard

boolean supportsAccumulators(Context ctx) { return !(ctx instanceof SingleValueContext) || ((SingleValueContext) ctx).hasAccumulators(); }

Try / catch

try { context.getCounter("c").increment(); } catch (UnsupportedOperationException e) { /* metrics unavailable in this context */ }

Prevention

When it happens

Trigger: Calling context.getCounter(name) inside a e.g. ElementsToOne / single-value functor whose SingleValueContext was constructed with a null wrap.

Common situations: User code in a reduce/ElementsToOne transformation trying to increment a counter although the context only carries a single element and no accumulator facility.

Related errors


AI-assisted analysis of apache/beam@12126d8942 (2026-09-13). Data as JSON: /api/errors/a4d9a8e830dfd5bd. Report an issue: GitHub.

Appendix: source

Thrown at sdks/java/extensions/euphoria/src/main/java/org/apache/beam/sdk/extensions/euphoria/core/translate/SingleValueContext.java:71

  /**
   * Replace the stored value with given one.
   *
   * @param elem the element to store
   */
  @Override
  public void collect(T elem) {
    value = elem;
  }

  @Override
  public Context asContext() {
    return this;
  }

  @Override
  public Counter getCounter(String name) {
    if (wrap == null) {
      throw new UnsupportedOperationException("Accumulators not supported in this context");
    }
    return wrap.getCounter(name);
  }

  @Override
  public Histogram getHistogram(String name) {
    if (wrap == null) {
      throw new UnsupportedOperationException("Accumulators not supported in this context");
    }
    return wrap.getHistogram(name);
  }

  @Override
  public Timer getTimer(String name) {
    if (wrap == null) {
      throw new UnsupportedOperationException("Accumulators not supported in this context");
    }
    return wrap.getTimer(name);

View on GitHub (pinned to 12126d8942)