Search in sources :

Example 21 with PTransform

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

the class BeamFnDataWriteRunnerTest method testReuseForMultipleBundles.

@Test
public void testReuseForMultipleBundles() throws Exception {
    AtomicReference<String> bundleId = new AtomicReference<>("0");
    String localInputId = "inputPC";
    RunnerApi.PTransform pTransform = RemoteGrpcPortWrite.writeToPort(localInputId, PORT_SPEC).toPTransform();
    List<WindowedValue<String>> output0 = new ArrayList<>();
    List<WindowedValue<String>> output1 = new ArrayList<>();
    Map<ApiServiceDescriptor, BeamFnDataOutboundAggregator> aggregators = new HashMap<>();
    BeamFnDataOutboundAggregator aggregator = createRecordingAggregator(ImmutableMap.of("0", output0, "1", output1), bundleId::get);
    aggregators.put(PORT_SPEC.getApiServiceDescriptor(), aggregator);
    PTransformRunnerFactoryTestContext context = PTransformRunnerFactoryTestContext.builder(TRANSFORM_ID, pTransform).beamFnDataClient(mockBeamFnDataClient).processBundleInstructionIdSupplier(bundleId::get).outboundAggregators(aggregators).pCollections(ImmutableMap.of(localInputId, RunnerApi.PCollection.newBuilder().setCoderId(ELEM_CODER_ID).build())).coders(COMPONENTS.getCodersMap()).windowingStrategies(COMPONENTS.getWindowingStrategiesMap()).build();
    new BeamFnDataWriteRunner.Factory<String>().createRunnerForPTransform(context);
    assertThat(context.getPCollectionConsumers().keySet(), containsInAnyOrder(localInputId));
    FnDataReceiver<Object> pCollectionConsumer = context.getPCollectionConsumer(localInputId);
    pCollectionConsumer.accept(valueInGlobalWindow("ABC"));
    pCollectionConsumer.accept(valueInGlobalWindow("DEF"));
    assertThat(output0, contains(valueInGlobalWindow("ABC"), valueInGlobalWindow("DEF")));
    output0.clear();
    // Process for bundle id 1
    bundleId.set("1");
    pCollectionConsumer.accept(valueInGlobalWindow("GHI"));
    pCollectionConsumer.accept(valueInGlobalWindow("JKL"));
    assertThat(output1, contains(valueInGlobalWindow("GHI"), valueInGlobalWindow("JKL")));
    verifyNoMoreInteractions(mockBeamFnDataClient);
}
Also used : BeamFnDataOutboundAggregator(org.apache.beam.sdk.fn.data.BeamFnDataOutboundAggregator) ApiServiceDescriptor(org.apache.beam.model.pipeline.v1.Endpoints.ApiServiceDescriptor) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) AtomicReference(java.util.concurrent.atomic.AtomicReference) RunnerApi(org.apache.beam.model.pipeline.v1.RunnerApi) WindowedValue(org.apache.beam.sdk.util.WindowedValue) Test(org.junit.Test)

Example 22 with PTransform

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

the class WindowMappingFnRunner method createMapFunctionForPTransform.

static <T, W1 extends BoundedWindow, W2 extends BoundedWindow> ThrowingFunction<KV<T, W1>, KV<T, W2>> createMapFunctionForPTransform(String ptransformId, PTransform pTransform) throws IOException {
    FunctionSpec windowMappingFnPayload = FunctionSpec.parseFrom(pTransform.getSpec().getPayload());
    WindowMappingFn<W2> windowMappingFn = (WindowMappingFn<W2>) PCollectionViewTranslation.windowMappingFnFromProto(windowMappingFnPayload);
    return (KV<T, W1> input) -> KV.of(input.getKey(), windowMappingFn.getSideInputWindow(input.getValue()));
}
Also used : WindowMappingFn(org.apache.beam.sdk.transforms.windowing.WindowMappingFn) FunctionSpec(org.apache.beam.model.pipeline.v1.RunnerApi.FunctionSpec)

Example 23 with PTransform

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

the class JavaClassLookupTransformProvider method findMappingConstructor.

private Constructor<PTransform<InputT, OutputT>> findMappingConstructor(Constructor<?>[] constructors, JavaClassLookupPayload payload) {
    Row constructorRow = decodeRow(payload.getConstructorSchema(), payload.getConstructorPayload());
    List<Constructor<?>> mappingConstructors = Arrays.stream(constructors).filter(c -> c.getParameterCount() == payload.getConstructorSchema().getFieldsCount()).filter(c -> parametersCompatible(c.getParameters(), constructorRow)).collect(Collectors.toList());
    if (mappingConstructors.size() == 0) {
        throw new RuntimeException("Could not find a matching constructor. When using field names, make sure they are " + "available in the compiled Java class.");
    } else if (mappingConstructors.size() != 1) {
        throw new RuntimeException("Expected to find a single mapping constructor but found " + mappingConstructors.size());
    }
    return (Constructor<PTransform<InputT, OutputT>>) mappingConstructors.get(0);
}
Also used : JsonProperty(com.fasterxml.jackson.annotation.JsonProperty) BuilderMethod(org.apache.beam.model.pipeline.v1.ExternalTransforms.BuilderMethod) Arrays(java.util.Arrays) Array(java.lang.reflect.Array) NoSuchSchemaException(org.apache.beam.sdk.schemas.NoSuchSchemaException) SchemaApi(org.apache.beam.model.pipeline.v1.SchemaApi) RowCoder(org.apache.beam.sdk.coders.RowCoder) SerializableFunction(org.apache.beam.sdk.transforms.SerializableFunction) Constructor(java.lang.reflect.Constructor) ArrayList(java.util.ArrayList) PTransform(org.apache.beam.sdk.transforms.PTransform) FunctionSpec(org.apache.beam.model.pipeline.v1.RunnerApi.FunctionSpec) SchemaRegistry(org.apache.beam.sdk.schemas.SchemaRegistry) ByteString(org.apache.beam.vendor.grpc.v1p43p2.com.google.protobuf.ByteString) JavaFieldSchema(org.apache.beam.sdk.schemas.JavaFieldSchema) PInput(org.apache.beam.sdk.values.PInput) Row(org.apache.beam.sdk.values.Row) Method(java.lang.reflect.Method) Nullable(org.checkerframework.checker.nullness.qual.Nullable) InvalidProtocolBufferException(org.apache.beam.vendor.grpc.v1p43p2.com.google.protobuf.InvalidProtocolBufferException) Field(org.apache.beam.sdk.schemas.Schema.Field) TransformProvider(org.apache.beam.sdk.expansion.service.ExpansionService.TransformProvider) Collection(java.util.Collection) IOException(java.io.IOException) Collectors(java.util.stream.Collectors) Schema(org.apache.beam.sdk.schemas.Schema) TypeName(org.apache.beam.sdk.schemas.Schema.TypeName) JavaClassLookupPayload(org.apache.beam.model.pipeline.v1.ExternalTransforms.JavaClassLookupPayload) InvocationTargetException(java.lang.reflect.InvocationTargetException) ExpansionMethods(org.apache.beam.model.pipeline.v1.ExternalTransforms.ExpansionMethods) ClassUtils(org.apache.beam.repackaged.core.org.apache.commons.lang3.ClassUtils) POutput(org.apache.beam.sdk.values.POutput) List(java.util.List) ParameterizedType(java.lang.reflect.ParameterizedType) Type(java.lang.reflect.Type) ReflectHelpers(org.apache.beam.sdk.util.common.ReflectHelpers) JsonCreator(com.fasterxml.jackson.annotation.JsonCreator) AutoValue(com.google.auto.value.AutoValue) Annotation(java.lang.annotation.Annotation) Pattern(java.util.regex.Pattern) SchemaTranslation(org.apache.beam.sdk.schemas.SchemaTranslation) Collections(java.util.Collections) BeamUrns.getUrn(org.apache.beam.runners.core.construction.BeamUrns.getUrn) SuppressFBWarnings(edu.umd.cs.findbugs.annotations.SuppressFBWarnings) Constructor(java.lang.reflect.Constructor) Row(org.apache.beam.sdk.values.Row)

Example 24 with PTransform

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

the class RemoteGrpcPortReadTest method toFromPTransform.

@Test
public void toFromPTransform() throws InvalidProtocolBufferException {
    RemoteGrpcPort port = RemoteGrpcPort.newBuilder().setApiServiceDescriptor(ApiServiceDescriptor.newBuilder().setUrl("foo").setAuthentication(AuthenticationSpec.getDefaultInstance()).build()).build();
    RemoteGrpcPortRead read = RemoteGrpcPortRead.readFromPort(port, "myPort");
    PTransform ptransform = PTransform.parseFrom(read.toPTransform().toByteArray());
    RemoteGrpcPortRead serDeRead = RemoteGrpcPortRead.fromPTransform(ptransform);
    assertThat(serDeRead, equalTo(read));
    assertThat(serDeRead.getPort(), equalTo(read.getPort()));
    assertThat(serDeRead.toPTransform(), equalTo(ptransform));
}
Also used : RemoteGrpcPort(org.apache.beam.model.fnexecution.v1.BeamFnApi.RemoteGrpcPort) PTransform(org.apache.beam.model.pipeline.v1.RunnerApi.PTransform) Test(org.junit.Test)

Example 25 with PTransform

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

the class ParDoTranslation method getMainInputName.

/**
 * Returns the name of the main input of the ptransform.
 */
public static String getMainInputName(RunnerApi.PTransformOrBuilder ptransform) throws IOException {
    checkArgument(PAR_DO_TRANSFORM_URN.equals(ptransform.getSpec().getUrn()) || SPLITTABLE_PAIR_WITH_RESTRICTION_URN.equals(ptransform.getSpec().getUrn()) || SPLITTABLE_SPLIT_AND_SIZE_RESTRICTIONS_URN.equals(ptransform.getSpec().getUrn()) || SPLITTABLE_TRUNCATE_SIZED_RESTRICTION_URN.equals(ptransform.getSpec().getUrn()) || SPLITTABLE_PROCESS_ELEMENTS_URN.equals(ptransform.getSpec().getUrn()) || SPLITTABLE_PROCESS_SIZED_ELEMENTS_AND_RESTRICTIONS_URN.equals(ptransform.getSpec().getUrn()), "Unexpected payload type %s", ptransform.getSpec().getUrn());
    ParDoPayload payload = ParDoPayload.parseFrom(ptransform.getSpec().getPayload());
    return getMainInputName(ptransform, payload);
}
Also used : ParDoPayload(org.apache.beam.model.pipeline.v1.RunnerApi.ParDoPayload)

Aggregations

PTransform (org.apache.beam.model.pipeline.v1.RunnerApi.PTransform)58 Test (org.junit.Test)41 RunnerApi (org.apache.beam.model.pipeline.v1.RunnerApi)28 Components (org.apache.beam.model.pipeline.v1.RunnerApi.Components)23 PTransformNode (org.apache.beam.runners.core.construction.graph.PipelineNode.PTransformNode)22 PCollectionNode (org.apache.beam.runners.core.construction.graph.PipelineNode.PCollectionNode)21 PCollection (org.apache.beam.model.pipeline.v1.RunnerApi.PCollection)19 Map (java.util.Map)18 Environment (org.apache.beam.model.pipeline.v1.RunnerApi.Environment)18 ArrayList (java.util.ArrayList)16 ByteString (org.apache.beam.vendor.grpc.v1p43p2.com.google.protobuf.ByteString)16 WindowedValue (org.apache.beam.sdk.util.WindowedValue)11 Collection (java.util.Collection)9 SdkComponents (org.apache.beam.runners.core.construction.SdkComponents)9 Collectors (java.util.stream.Collectors)8 ImmutableMap (org.apache.beam.vendor.guava.v26_0_jre.com.google.common.collect.ImmutableMap)8 IOException (java.io.IOException)7 HashSet (java.util.HashSet)7 FunctionSpec (org.apache.beam.model.pipeline.v1.RunnerApi.FunctionSpec)7 Pipeline (org.apache.beam.model.pipeline.v1.RunnerApi.Pipeline)7