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)));
}
}
Aggregations