|
| 1 | +package com.javaedge.scala.chapter7 |
| 2 | + |
| 3 | +import java.util.Properties |
| 4 | + |
| 5 | +import org.apache.flink.api.common.serialization.SimpleStringSchema |
| 6 | +import org.apache.flink.streaming.api.CheckpointingMode |
| 7 | +import org.apache.flink.streaming.api.scala.StreamExecutionEnvironment |
| 8 | +import org.apache.flink.streaming.connectors.kafka.FlinkKafkaProducer |
| 9 | +import org.apache.flink.streaming.connectors.kafka.internals.KeyedSerializationSchemaWrapper |
| 10 | + |
| 11 | + |
| 12 | +/** |
| 13 | + * @author JavaEdge |
| 14 | + * @date 2019-07-26 |
| 15 | + */ |
| 16 | +object KafkaConnectorProducerApp { |
| 17 | + |
| 18 | + def main(args: Array[String]): Unit = { |
| 19 | + |
| 20 | + val env = StreamExecutionEnvironment.getExecutionEnvironment |
| 21 | + |
| 22 | + // 常用检查点设置参数 |
| 23 | + env.enableCheckpointing(4000) |
| 24 | + env.getCheckpointConfig.setCheckpointingMode(CheckpointingMode.EXACTLY_ONCE) |
| 25 | + env.getCheckpointConfig.setCheckpointTimeout(10000) |
| 26 | + env.getCheckpointConfig.setMaxConcurrentCheckpoints(1) |
| 27 | + |
| 28 | + // 从socket接收数据,通过Flink将数据Sink到Kafka |
| 29 | + val data = env.socketTextStream("localhost", 9999) |
| 30 | + |
| 31 | + val topic = "testJavaEdge" |
| 32 | + val properties = new Properties() |
| 33 | + properties.setProperty("bootstrap.servers", "192.168.0.106:9092") |
| 34 | + |
| 35 | + // val kafkaSink = new FlinkKafkaProducer[String](topic, |
| 36 | + // new KeyedSerializationSchemaWrapper[String](new SimpleStringSchema()), |
| 37 | + // properties) |
| 38 | + val kafkaSink = new FlinkKafkaProducer[String](topic, |
| 39 | + new KeyedSerializationSchemaWrapper[String](new SimpleStringSchema()), |
| 40 | + properties, |
| 41 | + FlinkKafkaProducer.Semantic.EXACTLY_ONCE) |
| 42 | + data.addSink(kafkaSink) |
| 43 | + |
| 44 | + env.execute("KafkaConnectorProducerApp") |
| 45 | + } |
| 46 | +} |
| 47 | + |
0 commit comments