Search in sources :

Example 1 with OutgoingMessage

use of org.apache.beam.sdk.io.gcp.pubsub.PubsubClient.OutgoingMessage in project beam by apache.

the class PubsubUnboundedSinkTest method sendMoreThanOneBatchByByteSize.

@Test
public void sendMoreThanOneBatchByByteSize() throws IOException {
    List<OutgoingMessage> outgoing = new ArrayList<>();
    List<String> data = new ArrayList<>();
    int batchSize = 100;
    int batchBytes = 10;
    int n = 0;
    while (n < batchBytes * 10) {
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < batchBytes; i++) {
            sb.append(String.valueOf(n));
        }
        String str = sb.toString();
        outgoing.add(new OutgoingMessage(str.getBytes(), ImmutableMap.<String, String>of(), TIMESTAMP, getRecordId(str)));
        data.add(str);
        n += str.length();
    }
    try (PubsubTestClientFactory factory = PubsubTestClient.createFactoryForPublish(TOPIC, outgoing, ImmutableList.<OutgoingMessage>of())) {
        PubsubUnboundedSink sink = new PubsubUnboundedSink(factory, StaticValueProvider.of(TOPIC), TIMESTAMP_ATTRIBUTE, ID_ATTRIBUTE, NUM_SHARDS, batchSize, batchBytes, Duration.standardSeconds(2), RecordIdMethod.DETERMINISTIC);
        p.apply(Create.of(data)).apply(ParDo.of(new Stamp())).apply(sink);
        p.run();
    }
// The PubsubTestClientFactory will assert fail on close if the actual published
// message does not match the expected publish message.
}
Also used : OutgoingMessage(org.apache.beam.sdk.io.gcp.pubsub.PubsubClient.OutgoingMessage) ArrayList(java.util.ArrayList) PubsubTestClientFactory(org.apache.beam.sdk.io.gcp.pubsub.PubsubTestClient.PubsubTestClientFactory) Test(org.junit.Test)

Example 2 with OutgoingMessage

use of org.apache.beam.sdk.io.gcp.pubsub.PubsubClient.OutgoingMessage in project beam by apache.

the class PubsubUnboundedSinkTest method saneCoder.

@Test
public void saneCoder() throws Exception {
    OutgoingMessage message = new OutgoingMessage(DATA.getBytes(), ImmutableMap.<String, String>of(), TIMESTAMP, getRecordId(DATA));
    CoderProperties.coderDecodeEncodeEqual(PubsubUnboundedSink.CODER, message);
    CoderProperties.coderSerializable(PubsubUnboundedSink.CODER);
}
Also used : OutgoingMessage(org.apache.beam.sdk.io.gcp.pubsub.PubsubClient.OutgoingMessage) Test(org.junit.Test)

Example 3 with OutgoingMessage

use of org.apache.beam.sdk.io.gcp.pubsub.PubsubClient.OutgoingMessage in project beam by apache.

the class PubsubJsonClientTest method publishOneMessage.

@Test
public void publishOneMessage() throws IOException {
    String expectedTopic = TOPIC.getPath();
    PubsubMessage expectedPubsubMessage = new PubsubMessage().encodeData(DATA.getBytes()).setAttributes(ImmutableMap.<String, String>builder().put(TIMESTAMP_ATTRIBUTE, String.valueOf(MESSAGE_TIME)).put(ID_ATTRIBUTE, RECORD_ID).put("k", "v").build());
    PublishRequest expectedRequest = new PublishRequest().setMessages(ImmutableList.of(expectedPubsubMessage));
    PublishResponse expectedResponse = new PublishResponse().setMessageIds(ImmutableList.of(MESSAGE_ID));
    Mockito.when((Object) (mockPubsub.projects().topics().publish(expectedTopic, expectedRequest).execute())).thenReturn(expectedResponse);
    Map<String, String> attrs = new HashMap<>();
    attrs.put("k", "v");
    OutgoingMessage actualMessage = new OutgoingMessage(DATA.getBytes(), attrs, MESSAGE_TIME, RECORD_ID);
    int n = client.publish(TOPIC, ImmutableList.of(actualMessage));
    assertEquals(1, n);
}
Also used : PublishResponse(com.google.api.services.pubsub.model.PublishResponse) OutgoingMessage(org.apache.beam.sdk.io.gcp.pubsub.PubsubClient.OutgoingMessage) HashMap(java.util.HashMap) PublishRequest(com.google.api.services.pubsub.model.PublishRequest) PubsubMessage(com.google.api.services.pubsub.model.PubsubMessage) Test(org.junit.Test)

Example 4 with OutgoingMessage

use of org.apache.beam.sdk.io.gcp.pubsub.PubsubClient.OutgoingMessage in project beam by apache.

the class PubsubUnboundedSinkTest method sendOneMessage.

@Test
public void sendOneMessage() throws IOException {
    List<OutgoingMessage> outgoing = ImmutableList.of(new OutgoingMessage(DATA.getBytes(), ATTRIBUTES, TIMESTAMP, getRecordId(DATA)));
    int batchSize = 1;
    int batchBytes = 1;
    try (PubsubTestClientFactory factory = PubsubTestClient.createFactoryForPublish(TOPIC, outgoing, ImmutableList.<OutgoingMessage>of())) {
        PubsubUnboundedSink sink = new PubsubUnboundedSink(factory, StaticValueProvider.of(TOPIC), TIMESTAMP_ATTRIBUTE, ID_ATTRIBUTE, NUM_SHARDS, batchSize, batchBytes, Duration.standardSeconds(2), RecordIdMethod.DETERMINISTIC);
        p.apply(Create.of(ImmutableList.of(DATA))).apply(ParDo.of(new Stamp(ATTRIBUTES))).apply(sink);
        p.run();
    }
// The PubsubTestClientFactory will assert fail on close if the actual published
// message does not match the expected publish message.
}
Also used : OutgoingMessage(org.apache.beam.sdk.io.gcp.pubsub.PubsubClient.OutgoingMessage) PubsubTestClientFactory(org.apache.beam.sdk.io.gcp.pubsub.PubsubTestClient.PubsubTestClientFactory) Test(org.junit.Test)

Example 5 with OutgoingMessage

use of org.apache.beam.sdk.io.gcp.pubsub.PubsubClient.OutgoingMessage in project beam by apache.

the class PubsubUnboundedSinkTest method sendMoreThanOneBatchByNumMessages.

@Test
public void sendMoreThanOneBatchByNumMessages() throws IOException {
    List<OutgoingMessage> outgoing = new ArrayList<>();
    List<String> data = new ArrayList<>();
    int batchSize = 2;
    int batchBytes = 1000;
    for (int i = 0; i < batchSize * 10; i++) {
        String str = String.valueOf(i);
        outgoing.add(new OutgoingMessage(str.getBytes(), ImmutableMap.<String, String>of(), TIMESTAMP, getRecordId(str)));
        data.add(str);
    }
    try (PubsubTestClientFactory factory = PubsubTestClient.createFactoryForPublish(TOPIC, outgoing, ImmutableList.<OutgoingMessage>of())) {
        PubsubUnboundedSink sink = new PubsubUnboundedSink(factory, StaticValueProvider.of(TOPIC), TIMESTAMP_ATTRIBUTE, ID_ATTRIBUTE, NUM_SHARDS, batchSize, batchBytes, Duration.standardSeconds(2), RecordIdMethod.DETERMINISTIC);
        p.apply(Create.of(data)).apply(ParDo.of(new Stamp())).apply(sink);
        p.run();
    }
// The PubsubTestClientFactory will assert fail on close if the actual published
// message does not match the expected publish message.
}
Also used : OutgoingMessage(org.apache.beam.sdk.io.gcp.pubsub.PubsubClient.OutgoingMessage) ArrayList(java.util.ArrayList) PubsubTestClientFactory(org.apache.beam.sdk.io.gcp.pubsub.PubsubTestClient.PubsubTestClientFactory) Test(org.junit.Test)

Aggregations

OutgoingMessage (org.apache.beam.sdk.io.gcp.pubsub.PubsubClient.OutgoingMessage)7 Test (org.junit.Test)7 PubsubTestClientFactory (org.apache.beam.sdk.io.gcp.pubsub.PubsubTestClient.PubsubTestClientFactory)4 ArrayList (java.util.ArrayList)3 PublishRequest (com.google.api.services.pubsub.model.PublishRequest)1 PublishResponse (com.google.api.services.pubsub.model.PublishResponse)1 PubsubMessage (com.google.api.services.pubsub.model.PubsubMessage)1 ByteString (com.google.protobuf.ByteString)1 PublishRequest (com.google.pubsub.v1.PublishRequest)1 PublishResponse (com.google.pubsub.v1.PublishResponse)1 PublisherImplBase (com.google.pubsub.v1.PublisherGrpc.PublisherImplBase)1 PubsubMessage (com.google.pubsub.v1.PubsubMessage)1 Server (io.grpc.Server)1 StreamObserver (io.grpc.stub.StreamObserver)1 HashMap (java.util.HashMap)1