Search in sources :

Example 1 with ProtoDomain

use of org.apache.beam.sdk.extensions.protobuf.ProtoDomain in project beam by apache.

the class PubsubIOTest method testProtoDynamicMessages.

@Test
public void testProtoDynamicMessages() {
    ProtoCoder<Primitive> coder = ProtoCoder.of(Primitive.class);
    ImmutableList<Primitive> inputs = ImmutableList.of(Primitive.newBuilder().setPrimitiveInt32(42).build(), Primitive.newBuilder().setPrimitiveBool(true).build(), Primitive.newBuilder().setPrimitiveString("Hello, World!").build());
    setupTestClient(inputs, coder);
    ProtoDomain domain = ProtoDomain.buildFrom(Primitive.getDescriptor());
    String name = Primitive.getDescriptor().getFullName();
    PCollection<Primitive> read = readPipeline.apply(PubsubIO.readProtoDynamicMessages(domain, name).fromSubscription(SUBSCRIPTION.getPath()).withClock(CLOCK).withClientFactory(clientFactory)).apply("Return To Primitive", MapElements.into(TypeDescriptor.of(Primitive.class)).via((DynamicMessage message) -> {
        try {
            return Primitive.parseFrom(message.toByteArray());
        } catch (InvalidProtocolBufferException e) {
            throw new RuntimeException("Could not return to Primitive", e);
        }
    }));
    PAssert.that(read).containsInAnyOrder(inputs);
    readPipeline.run();
}
Also used : ProtoDomain(org.apache.beam.sdk.extensions.protobuf.ProtoDomain) Primitive(org.apache.beam.sdk.extensions.protobuf.Proto3SchemaMessages.Primitive) InvalidProtocolBufferException(com.google.protobuf.InvalidProtocolBufferException) ByteString(com.google.protobuf.ByteString) DynamicMessage(com.google.protobuf.DynamicMessage) Test(org.junit.Test)

Aggregations

ByteString (com.google.protobuf.ByteString)1 DynamicMessage (com.google.protobuf.DynamicMessage)1 InvalidProtocolBufferException (com.google.protobuf.InvalidProtocolBufferException)1 Primitive (org.apache.beam.sdk.extensions.protobuf.Proto3SchemaMessages.Primitive)1 ProtoDomain (org.apache.beam.sdk.extensions.protobuf.ProtoDomain)1 Test (org.junit.Test)1