Search in sources :

Example 6 with DoFnSignature

use of org.apache.beam.sdk.transforms.reflect.DoFnSignature in project beam by apache.

the class PTransformMatchers method stateOrTimerParDoMulti.

/**
   * A {@link PTransformMatcher} that matches a {@link ParDo.MultiOutput} containing a {@link DoFn}
   * that uses state or timers, as specified by {@link DoFnSignature#usesState()} and
   * {@link DoFnSignature#usesTimers()}.
   */
public static PTransformMatcher stateOrTimerParDoMulti() {
    return new PTransformMatcher() {

        @Override
        public boolean matches(AppliedPTransform<?, ?, ?> application) {
            PTransform<?, ?> transform = application.getTransform();
            if (transform instanceof ParDo.MultiOutput) {
                DoFn<?, ?> fn = ((ParDo.MultiOutput<?, ?>) transform).getFn();
                DoFnSignature signature = DoFnSignatures.signatureForDoFn(fn);
                return signature.usesState() || signature.usesTimers();
            }
            return false;
        }

        @Override
        public String toString() {
            return MoreObjects.toStringHelper("StateOrTimerParDoMultiMatcher").toString();
        }
    };
}
Also used : PTransformMatcher(org.apache.beam.sdk.runners.PTransformMatcher) AppliedPTransform(org.apache.beam.sdk.runners.AppliedPTransform) DoFnSignature(org.apache.beam.sdk.transforms.reflect.DoFnSignature)

Example 7 with DoFnSignature

use of org.apache.beam.sdk.transforms.reflect.DoFnSignature in project beam by apache.

the class ParDoTranslation method toProto.

public static ParDoPayload toProto(ParDo.MultiOutput<?, ?> parDo, SdkComponents components) throws IOException {
    DoFn<?, ?> doFn = parDo.getFn();
    DoFnSignature signature = DoFnSignatures.getSignature(doFn.getClass());
    Map<String, StateDeclaration> states = signature.stateDeclarations();
    Map<String, TimerDeclaration> timers = signature.timerDeclarations();
    List<Parameter> parameters = signature.processElement().extraParameters();
    ParDoPayload.Builder builder = ParDoPayload.newBuilder();
    builder.setDoFn(toProto(parDo.getFn(), parDo.getMainOutputTag()));
    for (PCollectionView<?> sideInput : parDo.getSideInputs()) {
        builder.putSideInputs(sideInput.getTagInternal().getId(), toProto(sideInput));
    }
    for (Parameter parameter : parameters) {
        Optional<RunnerApi.Parameter> protoParameter = toProto(parameter);
        if (protoParameter.isPresent()) {
            builder.addParameters(protoParameter.get());
        }
    }
    for (Map.Entry<String, StateDeclaration> state : states.entrySet()) {
        RunnerApi.StateSpec spec = toProto(getStateSpecOrCrash(state.getValue(), doFn), components);
        builder.putStateSpecs(state.getKey(), spec);
    }
    for (Map.Entry<String, TimerDeclaration> timer : timers.entrySet()) {
        RunnerApi.TimerSpec spec = toProto(getTimerSpecOrCrash(timer.getValue(), doFn));
        builder.putTimerSpecs(timer.getKey(), spec);
    }
    return builder.build();
}
Also used : ParDoPayload(org.apache.beam.sdk.common.runner.v1.RunnerApi.ParDoPayload) ByteString(com.google.protobuf.ByteString) TimerDeclaration(org.apache.beam.sdk.transforms.reflect.DoFnSignature.TimerDeclaration) RunnerApi(org.apache.beam.sdk.common.runner.v1.RunnerApi) Parameter(org.apache.beam.sdk.transforms.reflect.DoFnSignature.Parameter) WindowParameter(org.apache.beam.sdk.transforms.reflect.DoFnSignature.Parameter.WindowParameter) RestrictionTrackerParameter(org.apache.beam.sdk.transforms.reflect.DoFnSignature.Parameter.RestrictionTrackerParameter) Map(java.util.Map) DoFnSignature(org.apache.beam.sdk.transforms.reflect.DoFnSignature) StateDeclaration(org.apache.beam.sdk.transforms.reflect.DoFnSignature.StateDeclaration)

Example 8 with DoFnSignature

use of org.apache.beam.sdk.transforms.reflect.DoFnSignature in project beam by apache.

the class DataflowPipelineTranslator method translateFn.

private static void translateFn(StepTranslationContext stepContext, DoFn fn, WindowingStrategy windowingStrategy, Iterable<PCollectionView<?>> sideInputs, Coder inputCoder, TranslationContext context, long mainOutput, Map<Long, TupleTag<?>> outputMap) {
    DoFnSignature signature = DoFnSignatures.getSignature(fn.getClass());
    if (signature.processElement().isSplittable()) {
        throw new UnsupportedOperationException(String.format("%s does not currently support splittable DoFn: %s", DataflowRunner.class.getSimpleName(), fn));
    }
    stepContext.addInput(PropertyNames.USER_FN, fn.getClass().getName());
    stepContext.addInput(PropertyNames.SERIALIZED_FN, byteArrayToJsonString(serializeToByteArray(DoFnInfo.forFn(fn, windowingStrategy, sideInputs, inputCoder, mainOutput, outputMap))));
    // in streaming but does not work in batch
    if (context.getPipelineOptions().isStreaming() && (signature.usesState() || signature.usesTimers())) {
        stepContext.addInput(PropertyNames.USES_KEYED_STATE, "true");
    }
}
Also used : DoFnSignature(org.apache.beam.sdk.transforms.reflect.DoFnSignature)

Example 9 with DoFnSignature

use of org.apache.beam.sdk.transforms.reflect.DoFnSignature in project beam by apache.

the class BatchStatefulParDoOverrides method verifyFnIsStateful.

private static <InputT, OutputT> void verifyFnIsStateful(DoFn<InputT, OutputT> fn) {
    DoFnSignature signature = DoFnSignatures.getSignature(fn.getClass());
    // It is still correct to use this without state or timers, but a bad idea.
    // Since it is internal it should never be used wrong, so it is OK to crash.
    checkState(signature.usesState() || signature.usesTimers(), "%s used for %s that does not use state or timers.", BatchStatefulParDoOverrides.class.getSimpleName(), ParDo.class.getSimpleName());
}
Also used : ParDo(org.apache.beam.sdk.transforms.ParDo) DoFnSignature(org.apache.beam.sdk.transforms.reflect.DoFnSignature)

Example 10 with DoFnSignature

use of org.apache.beam.sdk.transforms.reflect.DoFnSignature in project beam by apache.

the class ParDo method finishSpecifyingStateSpecs.

private static void finishSpecifyingStateSpecs(DoFn<?, ?> fn, CoderRegistry coderRegistry, Coder<?> inputCoder) {
    DoFnSignature signature = DoFnSignatures.getSignature(fn.getClass());
    Map<String, DoFnSignature.StateDeclaration> stateDeclarations = signature.stateDeclarations();
    for (DoFnSignature.StateDeclaration stateDeclaration : stateDeclarations.values()) {
        try {
            StateSpec<?> stateSpec = (StateSpec<?>) stateDeclaration.field().get(fn);
            stateSpec.offerCoders(codersForStateSpecTypes(stateDeclaration, coderRegistry, inputCoder));
            stateSpec.finishSpecifying();
        } catch (IllegalAccessException e) {
            throw new RuntimeException(e);
        }
    }
}
Also used : StateSpec(org.apache.beam.sdk.state.StateSpec) DoFnSignature(org.apache.beam.sdk.transforms.reflect.DoFnSignature)

Aggregations

DoFnSignature (org.apache.beam.sdk.transforms.reflect.DoFnSignature)13 AppliedPTransform (org.apache.beam.sdk.runners.AppliedPTransform)4 PTransformMatcher (org.apache.beam.sdk.runners.PTransformMatcher)4 Map (java.util.Map)2 ParDo (org.apache.beam.sdk.transforms.ParDo)2 KV (org.apache.beam.sdk.values.KV)2 PValue (org.apache.beam.sdk.values.PValue)2 TupleTag (org.apache.beam.sdk.values.TupleTag)2 OutputPort (com.datatorrent.api.Operator.OutputPort)1 ByteString (com.google.protobuf.ByteString)1 ApexParDoOperator (org.apache.beam.runners.apex.translation.operators.ApexParDoOperator)1 SplittableParDo (org.apache.beam.runners.core.construction.SplittableParDo)1 MetricsContainerStepMap (org.apache.beam.runners.core.metrics.MetricsContainerStepMap)1 NamedAggregators (org.apache.beam.runners.spark.aggregators.NamedAggregators)1 KvCoder (org.apache.beam.sdk.coders.KvCoder)1 RunnerApi (org.apache.beam.sdk.common.runner.v1.RunnerApi)1 ParDoPayload (org.apache.beam.sdk.common.runner.v1.RunnerApi.ParDoPayload)1 StateSpec (org.apache.beam.sdk.state.StateSpec)1 DoFn (org.apache.beam.sdk.transforms.DoFn)1 MultiOutput (org.apache.beam.sdk.transforms.ParDo.MultiOutput)1