一、添加相关依赖
4.0.0
org.example
flink-proj
1.0-SNAPSHOT
8
8
org.apache.flink
flink-java
1.11.1
org.apache.flink
flink-streaming-java_2.11
1.11.1
org.apache.flink
flink-clients_2.11
1.11.1
org.apache.flink
flink-connector-kafka_2.12
1.11.1
二、主类:
package cn.edu.tju.demo;
import org.apache.flink.api.common.serialization.SimpleStringSchema;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.connectors.kafka.FlinkKafkaConsumer;
import org.apache.kafka.clients.consumer.ConsumerConfig;
import org.apache.kafka.clients.producer.ProducerConfig;
import org.apache.kafka.common.serialization.StringDeserializer;
import org.apache.kafka.common.serialization.StringSerializer;
import java.util.Arrays;
import java.util.List;
import java.util.Properties;
public class Test03 {
public static void main(String[] args) throws Exception {
StreamExecutionEnvironment environment = StreamExecutionEnvironment
.getExecutionEnvironment();
Properties properties = new Properties();
properties.setProperty(ConsumerConfig.KEY_DESERIALIZER_CLASS_CONFIG, StringDeserializer.class.getName());
properties.setProperty(ConsumerConfig.VALUE_DESERIALIZER_CLASS_CONFIG, StringDeserializer.class.getName());
properties.setProperty(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG,"xx.xx.xx.xx:9092");
properties.setProperty(ConsumerConfig.GROUP_ID_CONFIG,"testGroup");
DataStreamSource myTopic = environment.addSource(
new FlinkKafkaConsumer("myTopic", new SimpleStringSchema(), properties));
myTopic.print();
environment.execute("my job");
}
}
kafka为2.x版本