Search in sources :

Example 16 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 17 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 18 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)

Example 19 with DoFnSignature

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

the class ParDoMultiOverrideFactory method getReplacementTransform.

@SuppressWarnings("unchecked")
private PTransform<PCollection<? extends InputT>, PCollectionTuple> getReplacementTransform(MultiOutput<InputT, OutputT> transform) {
    DoFn<InputT, OutputT> fn = transform.getFn();
    DoFnSignature signature = DoFnSignatures.getSignature(fn.getClass());
    if (signature.processElement().isSplittable()) {
        return new SplittableParDo(transform);
    } else if (signature.stateDeclarations().size() > 0 || signature.timerDeclarations().size() > 0) {
        // Based on the fact that the signature is stateful, DoFnSignatures ensures
        // that it is also keyed
        MultiOutput<KV<?, ?>, OutputT> keyedTransform = (MultiOutput<KV<?, ?>, OutputT>) transform;
        return new GbkThenStatefulParDo(keyedTransform);
    } else {
        return transform;
    }
}
Also used : SplittableParDo(org.apache.beam.runners.core.construction.SplittableParDo) KV(org.apache.beam.sdk.values.KV) DoFnSignature(org.apache.beam.sdk.transforms.reflect.DoFnSignature) MultiOutput(org.apache.beam.sdk.transforms.ParDo.MultiOutput)

Example 20 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)

Aggregations

DoFnSignature (org.apache.beam.sdk.transforms.reflect.DoFnSignature)26 AppliedPTransform (org.apache.beam.sdk.runners.AppliedPTransform)8 PTransformMatcher (org.apache.beam.sdk.runners.PTransformMatcher)8 HashMap (java.util.HashMap)5 Map (java.util.Map)5 Coder (org.apache.beam.sdk.coders.Coder)4 StateSpec (org.apache.beam.sdk.state.StateSpec)4 PCollectionView (org.apache.beam.sdk.values.PCollectionView)4 KvCoder (org.apache.beam.sdk.coders.KvCoder)3 TupleTag (org.apache.beam.sdk.values.TupleTag)3 IOException (java.io.IOException)2 StatefulDoFnRunner (org.apache.beam.runners.core.StatefulDoFnRunner)2 SplittableParDo (org.apache.beam.runners.core.construction.SplittableParDo)2 SamzaExecutionContext (org.apache.beam.runners.samza.SamzaExecutionContext)2 SchemaCoder (org.apache.beam.sdk.schemas.SchemaCoder)2 DoFn (org.apache.beam.sdk.transforms.DoFn)2 DoFnSchemaInformation (org.apache.beam.sdk.transforms.DoFnSchemaInformation)2 ParDo (org.apache.beam.sdk.transforms.ParDo)2 BoundedWindow (org.apache.beam.sdk.transforms.windowing.BoundedWindow)2 WindowedValue (org.apache.beam.sdk.util.WindowedValue)2