Search in sources :

Example 11 with ReceivedMessage

use of com.google.pubsub.v1.ReceivedMessage in project flink by apache.

the class EmulatedPubSubSinkTest method testFlinkSink.

@Test
public void testFlinkSink() throws Exception {
    StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
    env.setParallelism(4);
    List<String> input = Arrays.asList("One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten");
    // Create test stream
    DataStream<String> theData = env.fromCollection(input).name("Test input").map((MapFunction<String, String>) StringUtils::reverse);
    // Sink into pubsub
    theData.addSink(PubSubSink.newBuilder().withSerializationSchema(new SimpleStringSchema()).withProjectName(PROJECT_NAME).withTopicName(TOPIC_NAME).withHostAndPortForEmulator(getPubSubHostPort()).withCredentials(EmulatorCredentials.getInstance()).build()).name("PubSub sink");
    // Run
    env.execute();
    // Now get the result from PubSub and verify if everything is there
    List<ReceivedMessage> receivedMessages = pubsubHelper.pullMessages(PROJECT_NAME, SUBSCRIPTION_NAME, 100);
    assertEquals("Wrong number of elements", input.size(), receivedMessages.size());
    // Check output strings
    List<String> output = new ArrayList<>();
    receivedMessages.forEach(msg -> output.add(msg.getMessage().getData().toStringUtf8()));
    for (String test : input) {
        assertTrue("Missing " + test, output.contains(StringUtils.reverse(test)));
    }
}
Also used : ArrayList(java.util.ArrayList) SimpleStringSchema(org.apache.flink.api.common.serialization.SimpleStringSchema) StreamExecutionEnvironment(org.apache.flink.streaming.api.environment.StreamExecutionEnvironment) ReceivedMessage(com.google.pubsub.v1.ReceivedMessage) Test(org.junit.Test)

Aggregations

ReceivedMessage (com.google.pubsub.v1.ReceivedMessage)9 ArrayList (java.util.ArrayList)6 Test (org.junit.Test)5 ByteString (com.google.protobuf.ByteString)4 PubsubMessage (com.google.pubsub.v1.PubsubMessage)4 PullRequest (com.google.pubsub.v1.PullRequest)4 PullResponse (com.google.pubsub.v1.PullResponse)4 Timestamp (com.google.protobuf.Timestamp)3 Publisher (com.google.cloud.pubsub.v1.Publisher)2 SubscriberStub (com.google.cloud.pubsub.v1.stub.SubscriberStub)2 AcknowledgeRequest (com.google.pubsub.v1.AcknowledgeRequest)2 SubscriberImplBase (com.google.pubsub.v1.SubscriberGrpc.SubscriberImplBase)2 Server (io.grpc.Server)2 StreamObserver (io.grpc.stub.StreamObserver)2 IncomingMessage (org.apache.beam.sdk.io.gcp.pubsub.PubsubClient.IncomingMessage)2 SimpleStringSchema (org.apache.flink.api.common.serialization.SimpleStringSchema)2 StreamExecutionEnvironment (org.apache.flink.streaming.api.environment.StreamExecutionEnvironment)2 GrpcSubscriberStub (com.google.cloud.pubsub.v1.stub.GrpcSubscriberStub)1 SubscriberStubSettings (com.google.cloud.pubsub.v1.stub.SubscriberStubSettings)1 StringUtils (org.apache.commons.lang3.StringUtils)1