tweise commented on a change in pull request #7896: [WIP] [FLINK-9007] 
[kinesis] Add Kinesis e2e test
URL: https://github.com/apache/flink/pull/7896#discussion_r262176060
 
 

 ##########
 File path: 
flink-end-to-end-tests/flink-streaming-kinesis-test/src/main/java/org/apache/flink/streaming/kinesis/test/KinesisExample.java
 ##########
 @@ -0,0 +1,157 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.flink.streaming.kinesis.test;
+
+import org.apache.flink.api.java.utils.ParameterTool;
+import org.apache.flink.streaming.api.datastream.DataStream;
+import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
+import org.apache.flink.streaming.connectors.kinesis.FlinkKinesisConsumer;
+import org.apache.flink.streaming.connectors.kinesis.FlinkKinesisProducer;
+import 
org.apache.flink.streaming.connectors.kinesis.config.ConsumerConfigConstants;
+import org.apache.flink.streaming.kafka.test.base.CustomWatermarkExtractor;
+import org.apache.flink.streaming.kafka.test.base.KafkaEvent;
+import org.apache.flink.streaming.kafka.test.base.KafkaEventSchema;
+import org.apache.flink.streaming.kafka.test.base.KafkaExampleUtil;
+import org.apache.flink.streaming.kafka.test.base.RollingAdditionMapper;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.net.URL;
+import java.util.List;
+import java.util.Properties;
+import java.util.concurrent.atomic.AtomicReference;
+
+/**
+ * A simple example that shows how to read from and write to Kinesis. This 
will read String messages
+ * from the input topic, parse them into a POJO type {@link KafkaEvent}, group 
by some key, and finally
+ * perform a rolling addition on each key for which the results are written 
back to another topic.
+ *
+ * <p>This example also demonstrates using a watermark assigner to generate 
per-partition
+ * watermarks directly in the Flink Kinesis consumer. For demonstration 
purposes, it is assumed that
+ * the String messages formatted as a (word,frequency,timestamp) tuple.
+ *
+ * <p>Example usage:
+ *     --input-topic test-input --output-topic test-output --bootstrap.servers 
localhost:9092 --zookeeper.connect localhost:2181 --group.id myconsumer
+ */
+public class KinesisExample {
+       private static final Logger LOG = 
LoggerFactory.getLogger(KinesisExample.class);
+
+       /**
+        * Interface to the pubsub system for this test.
+        */
+       interface PubsubClient {
+               void createTopic(String topic, int partitions, Properties 
props) throws Exception;
+
+               void sendMessage(String topic, String msg);
+
+               List<String> readAllMessages(String streamName) throws 
Exception;
+       }
+
+       public static void main(String[] args) throws Exception {
+               LOG.info("System properties: {}", System.getProperties());
+               // parse input arguments
+               final ParameterTool parameterTool = 
ParameterTool.fromArgs(args);
+               StreamExecutionEnvironment env = 
KafkaExampleUtil.prepareExecutionEnv(parameterTool);
+
+               String inputStream = parameterTool.getRequired("input-stream");
+               String outputStream = 
parameterTool.getRequired("output-stream");
+
+               PubsubClient pubsub = new 
KinesisPubsubClient(parameterTool.getProperties());
+               pubsub.createTopic(inputStream, 2, 
parameterTool.getProperties());
+               pubsub.createTopic(outputStream, 2, 
parameterTool.getProperties());
+
+               FlinkKinesisConsumer<KafkaEvent> consumer = new 
FlinkKinesisConsumer<>(
+                       inputStream,
+                       new KafkaEventSchema(),
+                       parameterTool.getProperties());
+               consumer.setPeriodicWatermarkAssigner(new 
CustomWatermarkExtractor());
+
+               DataStream<KafkaEvent> input = env
+                               .addSource(consumer)
+                               .keyBy("word")
+                               .map(new RollingAdditionMapper());
+
+               Properties producerProperties = new 
Properties(parameterTool.getProperties());
+               // needs region event when URL is specified
+               producerProperties.put(ConsumerConfigConstants.AWS_REGION, 
"us-east-1");
+
+               // KPL does not recognize endpoint URL..
+               String kinesisUrl = 
producerProperties.getProperty(ConsumerConfigConstants.AWS_ENDPOINT);
+               if (kinesisUrl != null) {
+                       URL url = new URL(kinesisUrl);
+                       producerProperties.put("KinesisEndpoint", 
url.getHost());
+                       producerProperties.put("KinesisPort", 
Integer.toString(url.getPort()));
+                       producerProperties.put("VerifyCertificate", "false");
+               }
+
+               FlinkKinesisProducer<KafkaEvent> producer = new 
FlinkKinesisProducer<>(
+                       new KafkaEventSchema(),
+                       producerProperties);
+               producer.setDefaultStream(outputStream);
+               producer.setDefaultPartition("fakePartition");
+
+               input.addSink(producer);
+
+               final AtomicReference<Exception> caughtException = new 
AtomicReference<>();
+
+               // TODO: Flink thinks nothing got executed when using different 
thread
+               // TODO: cancel job before stopping Kinesalite
+               Thread executeThread =
+                       new Thread(
+                               () -> {
+                                       try {
+                                               env.execute();
+                                               LOG.info("executed program");
+                                       } catch (Exception e) {
+                                               caughtException.set(e);
+                                       }
+                               });
+               executeThread.start();
+
+               // generate input
+               String[] messages = { "elephant,5,45218", "squirrel,12,46213", 
"bee,3,51348", "squirrel,22,52444", "bee,10,53412", "elephant,9,54867" };
+               for (String msg : messages) {
+                       pubsub.sendMessage(inputStream, msg);
+               }
+               LOG.info("generated records");
+
+               long timeoutMillis = System.currentTimeMillis() + 30_000;
+               List<String> results = pubsub.readAllMessages(outputStream);
+               while (System.currentTimeMillis() < timeoutMillis) {
+                       if (results.size() == messages.length) {
+                               break;
+                       }
+                       LOG.info("waiting for results..");
+                       Thread.sleep(1000);
+                       results = pubsub.readAllMessages(outputStream);
+               }
+
+               LOG.info("results: {}", results);
+               if (results.size() != messages.length) {
+                       throw new AssertionError("Expected results were not 
received from " + outputStream);
+               }
+               // TODO: compare records
+
+               // TODO: cancel job
+               System.out.println("environment: " + env);
 
 Review comment:
   Here I would like to cancel the job but I don't have an ID for it (blocking 
execution). I basically need job and test driver to run in parallel and join at 
this point.

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


With regards,
Apache Git Services

Reply via email to