问题
I am trying to read kafka data from flink and as I am new to kafka and flink, I don't know how to connect them.
回答1:
Flink provides Kafka connector. In order read data from Kafka topics, first you need add Flink -Kafka connector dependency.
<dependency>
<groupId>org.apache.flink</groupId>
<artifactId>flink-connector-kafka-0.8_2.10</artifactId>
<version>1.1.3</version>
</dependency>
Next you simply invoke Streaming execution environment and add Kafka source. Here is a sample
Properties properties = new Properties();
properties.setProperty("bootstrap.servers", "localhost:9092");
properties.setProperty("zookeeper.connect", "localhost:2181");
properties.setProperty("group.id", "test");
DataStream<String> stream = env
.addSource(new FlinkKafkaConsumer08<>("topic", new SimpleStringSchema(),properties))
.print();
That's it. You are all set to consume data from Kafka topic.
The complete code is available for download at this link
来源:https://stackoverflow.com/questions/41972417/how-to-access-read-kafka-topic-data-from-flink