Search in sources :

Example 6 with CombinePayload

use of org.apache.beam.model.pipeline.v1.RunnerApi.CombinePayload in project beam by apache.

the class CombineRunners method createMergeAccumulatorsMapFunction.

static <KeyT, AccumT> ThrowingFunction<KV<KeyT, Iterable<AccumT>>, KV<KeyT, AccumT>> createMergeAccumulatorsMapFunction(String pTransformId, PTransform pTransform) throws IOException {
    CombinePayload combinePayload = CombinePayload.parseFrom(pTransform.getSpec().getPayload());
    CombineFn<?, AccumT, ?> combineFn = (CombineFn) SerializableUtils.deserializeFromByteArray(combinePayload.getCombineFn().getPayload().toByteArray(), "CombineFn");
    return (KV<KeyT, Iterable<AccumT>> input) -> KV.of(input.getKey(), combineFn.mergeAccumulators(input.getValue()));
}
Also used : CombinePayload(org.apache.beam.model.pipeline.v1.RunnerApi.CombinePayload) CombineFn(org.apache.beam.sdk.transforms.Combine.CombineFn)

Example 7 with CombinePayload

use of org.apache.beam.model.pipeline.v1.RunnerApi.CombinePayload in project beam by apache.

the class CombineRunners method createExtractOutputsMapFunction.

static <KeyT, AccumT, OutputT> ThrowingFunction<KV<KeyT, AccumT>, KV<KeyT, OutputT>> createExtractOutputsMapFunction(String pTransformId, PTransform pTransform) throws IOException {
    CombinePayload combinePayload = CombinePayload.parseFrom(pTransform.getSpec().getPayload());
    CombineFn<?, AccumT, OutputT> combineFn = (CombineFn) SerializableUtils.deserializeFromByteArray(combinePayload.getCombineFn().getPayload().toByteArray(), "CombineFn");
    return (KV<KeyT, AccumT> input) -> KV.of(input.getKey(), combineFn.extractOutput(input.getValue()));
}
Also used : CombinePayload(org.apache.beam.model.pipeline.v1.RunnerApi.CombinePayload) CombineFn(org.apache.beam.sdk.transforms.Combine.CombineFn)

Example 8 with CombinePayload

use of org.apache.beam.model.pipeline.v1.RunnerApi.CombinePayload in project beam by apache.

the class CombineRunners method createConvertToAccumulatorsMapFunction.

static <KeyT, InputT, AccumT> ThrowingFunction<KV<KeyT, InputT>, KV<KeyT, AccumT>> createConvertToAccumulatorsMapFunction(String pTransformId, PTransform pTransform) throws IOException {
    CombinePayload combinePayload = CombinePayload.parseFrom(pTransform.getSpec().getPayload());
    CombineFn<InputT, AccumT, ?> combineFn = (CombineFn) SerializableUtils.deserializeFromByteArray(combinePayload.getCombineFn().getPayload().toByteArray(), "CombineFn");
    return (KV<KeyT, InputT> input) -> KV.of(input.getKey(), combineFn.addInput(combineFn.createAccumulator(), input.getValue()));
}
Also used : CombinePayload(org.apache.beam.model.pipeline.v1.RunnerApi.CombinePayload) CombineFn(org.apache.beam.sdk.transforms.Combine.CombineFn)

Example 9 with CombinePayload

use of org.apache.beam.model.pipeline.v1.RunnerApi.CombinePayload in project beam by apache.

the class RegisterNodeFunction method transformCombineValuesFnToFunctionSpec.

/**
 * Transforms a CombineValuesFn {@link ParDoInstruction} to an Apache Beam {@link
 * RunnerApi.FunctionSpec}.
 */
private RunnerApi.FunctionSpec.Builder transformCombineValuesFnToFunctionSpec(CloudObject userFn) {
    // Grab the Combine PTransform. This transform is the composite PTransform representing the
    // entire CombinePerKey, and it contains the CombinePayload we need.
    String combinePTransformId = getString(userFn, PropertyNames.SERIALIZED_FN);
    RunnerApi.PTransform combinePerKeyPTransform = pipeline.getComponents().getTransformsOrDefault(combinePTransformId, null);
    checkArgument(combinePerKeyPTransform != null, "Transform with id \"%s\" not found in pipeline.", combinePTransformId);
    checkArgument(combinePerKeyPTransform.getSpec().getUrn().equals(COMBINE_PER_KEY_URN), "Found transform \"%s\" for Combine instruction, " + "but that transform had unexpected URN \"%s\" (expected \"%s\")", combinePerKeyPTransform, combinePerKeyPTransform.getSpec().getUrn(), COMBINE_PER_KEY_URN);
    RunnerApi.CombinePayload combinePayload;
    try {
        combinePayload = RunnerApi.CombinePayload.parseFrom(combinePerKeyPTransform.getSpec().getPayload());
    } catch (InvalidProtocolBufferException exc) {
        throw new RuntimeException("Combine did not have a CombinePayload", exc);
    }
    String phase = getString(userFn, WorkerPropertyNames.PHASE, CombinePhase.ALL);
    String urn;
    switch(phase) {
        case CombinePhase.ALL:
            urn = COMBINE_GROUPED_VALUES_URN;
            break;
        case CombinePhase.ADD:
            urn = COMBINE_PRECOMBINE_URN;
            break;
        case CombinePhase.MERGE:
            urn = COMBINE_MERGE_URN;
            break;
        case CombinePhase.EXTRACT:
            urn = COMBINE_EXTRACT_URN;
            break;
        default:
            throw new RuntimeException("Encountered unknown Combine Phase: " + phase);
    }
    return RunnerApi.FunctionSpec.newBuilder().setUrn(urn).setPayload(combinePayload.toByteString());
}
Also used : RunnerApi(org.apache.beam.model.pipeline.v1.RunnerApi) InvalidProtocolBufferException(org.apache.beam.vendor.grpc.v1p43p2.com.google.protobuf.InvalidProtocolBufferException) Structs.getString(org.apache.beam.runners.dataflow.util.Structs.getString) ByteString(org.apache.beam.vendor.grpc.v1p43p2.com.google.protobuf.ByteString)

Aggregations

CombinePayload (org.apache.beam.model.pipeline.v1.RunnerApi.CombinePayload)5 CombineFn (org.apache.beam.sdk.transforms.Combine.CombineFn)4 ByteString (org.apache.beam.vendor.grpc.v1p43p2.com.google.protobuf.ByteString)3 RunnerApi (org.apache.beam.model.pipeline.v1.RunnerApi)2 Structs.getString (org.apache.beam.runners.dataflow.util.Structs.getString)2 InvalidProtocolBufferException (org.apache.beam.vendor.grpc.v1p43p2.com.google.protobuf.InvalidProtocolBufferException)2 IOException (java.io.IOException)1 AtomicReference (java.util.concurrent.atomic.AtomicReference)1 Components (org.apache.beam.model.pipeline.v1.RunnerApi.Components)1 PipelineVisitor (org.apache.beam.sdk.Pipeline.PipelineVisitor)1 RunnerApi (org.apache.beam.sdk.common.runner.v1.RunnerApi)1 CombinePayload (org.apache.beam.sdk.common.runner.v1.RunnerApi.CombinePayload)1 AppliedPTransform (org.apache.beam.sdk.runners.AppliedPTransform)1 Node (org.apache.beam.sdk.runners.TransformHierarchy.Node)1 Combine (org.apache.beam.sdk.transforms.Combine)1 Test (org.junit.Test)1