Flink入门(三) 读写kafka

maven配置


      UTF-8
      1.6.2
      1.2.47



     
         org.apache.flink
         flink-java
         ${flink.version}
     
     
         org.apache.flink
         flink-streaming-java_2.11
         ${flink.version}
     
      
     
         org.apache.flink
         flink-connector-kafka-0.11_2.11
         ${flink.version}
     

     
         com.alibaba
         fastjson
         ${fastjson.version}
             

读kafka-demo

final StreamExecutionEnvironment env = StreamExecutionEnvironment.createLocalEnvironment();

Properties propsConsumer = new Properties();
propsConsumer.setProperty("bootstrap.servers", "xxxx:9092");
propsConsumer.setProperty("group.id", "trafficwisdom-streaming");
propsConsumer.put("enable.auto.commit", false);
propsConsumer.put("max.poll.records", 1000);
FlinkKafkaConsumer011 consumer = new FlinkKafkaConsumer011("topic-test", new SimpleStringSchema(), propsConsumer);
consumer.setStartFromLatest();
DataStream stream = env.addSource(consumer);

stream.print();

env.execute();

写入kafka-demo

Properties propsProducer = new Properties();
propsProducer.setProperty("bootstrap.servers", "xxxx:9092");
FlinkKafkaProducer011 flinkKafkaProducer=new FlinkKafkaProducer011("write-test",new SimpleStringSchema(),propsProducer);
stream.addSink(flinkKafkaProducer);

你可能感兴趣的:(Flink,Flink入门)