Github user tzulitai commented on a diff in the pull request: https://github.com/apache/flink/pull/3911#discussion_r116659749 --- Diff: flink-examples/flink-examples-streaming/src/main/java/org/apache/flink/streaming/examples/kafka/Kafka010Example.java --- @@ -17,49 +17,74 @@ package org.apache.flink.streaming.examples.kafka; +import org.apache.flink.api.common.functions.MapFunction; import org.apache.flink.api.common.restartstrategy.RestartStrategies; 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.kafka.FlinkKafkaConsumer08; +import org.apache.flink.streaming.connectors.kafka.FlinkKafkaConsumer010; +import org.apache.flink.streaming.connectors.kafka.FlinkKafkaProducer010; import org.apache.flink.streaming.util.serialization.SimpleStringSchema; /** - * Read Strings from Kafka and print them to standard out. - * Note: On a cluster, DataStream.print() will print to the TaskManager's .out file! - * - * Please pass the following arguments to run the example: - * --topic test --bootstrap.servers localhost:9092 --zookeeper.connect localhost:2181 --group.id myconsumer + * An example that shows how to read from and write to Kafka. This will read String messages + * from the input topic, prefix them by a configured prefix and output to the output topic. * + * <p>Example usage: + * --input-topic test-input --output-topic test-outpu --bootstrap.servers localhost:9092 --zookeeper.connect localhost:2181 --group.id myconsumer */ -public class ReadFromKafka { +public class Kafka010Example { public static void main(String[] args) throws Exception { // parse input arguments final ParameterTool parameterTool = ParameterTool.fromArgs(args); - if(parameterTool.getNumberOfParameters() < 4) { - System.out.println("Missing parameters!\nUsage: Kafka --topic <topic> " + - "--bootstrap.servers <kafka brokers> --zookeeper.connect <zk quorum> --group.id <some id>"); + if (parameterTool.getNumberOfParameters() < 5) { + System.out.println("Missing parameters!\n" + + "Usage: Kafka --input-topic <topic> --output-topic <topic> " + + "--bootstrap.servers <kafka brokers> " + + "--zookeeper.connect <zk quorum> --group.id <some id> [--prefix <prefix>]"); return; } + String prefix = parameterTool.get("prefix", "PREFIX:"); + StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment(); env.getConfig().disableSysoutLogging(); env.getConfig().setRestartStrategy(RestartStrategies.fixedDelayRestart(4, 10000)); - env.enableCheckpointing(5000); // create a checkpoint every 5 seconds - env.getConfig().setGlobalJobParameters(parameterTool); // make parameters available in the web interface +// env.enableCheckpointing(5000); // create a checkpoint every 5 seconds + + // make parameters available in the web interface + env.getConfig().setGlobalJobParameters(parameterTool); - DataStream<String> messageStream = env - .addSource(new FlinkKafkaConsumer08<>( - parameterTool.getRequired("topic"), + DataStream<String> input = env + .addSource(new FlinkKafkaConsumer010<>( + parameterTool.getRequired("input-topic"), + new SimpleStringSchema(), + parameterTool.getProperties())) + .map(new PrefixingMapper(prefix)); + + input.addSink( + new FlinkKafkaProducer010<>( + parameterTool.getRequired("output-topic"), new SimpleStringSchema(), parameterTool.getProperties())); - // write kafka stream to standard out. - messageStream.print(); - env.execute("Read from Kafka example"); + env.execute("Kafka 0.8 Example"); --- End diff -- "0.10 example"
--- If your project is set up for it, you can reply to this email and have your reply appear on GitHub as well. If your project does not have this feature enabled and wishes so, or if the feature is enabled but not working, please contact infrastructure at infrastruct...@apache.org or file a JIRA ticket with INFRA. ---