Flink输出操作之Kafka Sink

Flink没有spark输出操作那么直接,spark可以进行迭代输出操作,而Flink对外的输出操作都是用sink进行完成,下面是kafka sink输出操作的demo

1、添加pom依赖

<dependency>
    <groupId>org.apache.flink</groupId>
    <artifactId>flink-scala_2.11</artifactId>
    <version>1.10.0</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.flink/flink-streaming-scala -->
<dependency>
    <groupId>org.apache.flink</groupId>
    <artifactId>flink-streaming-scala_2.11</artifactId>
    <version>1.10.0</version>
</dependency>
<dependency>
    <groupId>org.apache.flink</groupId>
    <artifactId>flink-connector-kafka-0.11_2.11</artifactId>
    <version>1.10.0</version>
</dependency>

2、demo的核心部分编码


import java.util.Base64.Encoder
import java.util.Properties

import com.flink.stu.InfoReading
import org.apache.flink.api.common.serialization.{SimpleStringEncoder, SimpleStringSchema}
import org.apache.flink.core.fs.Path
import org.apache.flink.streaming.api.functions.sink.filesystem.StreamingFileSink
import org.apache.flink.streaming.api.scala._
import org.apache.flink.streaming.connectors.kafka.{FlinkKafkaConsumer011, FlinkKafkaProducer011}

object KafkaSinkTest {
  def main(args: Array[String]): Unit = {
    val env = StreamExecutionEnvironment.getExecutionEnvironment
    env.setParallelism(1) //设置并行度
    val prop = new Properties()
    prop.setProperty("bootstrap.servers", "hadoop01:9092,hadoop02:9092")
    prop.setProperty("group.id", "group01")
    prop.setProperty("key.deserializer", "org.apache.kafka.common.serialization.StringDeserializer") //对入参key进行反序列化
    prop.setProperty("value.deserializer", "org.apache.kafka.common.serialization.StringDeserializer")
    prop.setProperty("auto.offset.reset", "latest")
    val inputStream = env.addSource( new FlinkKafkaConsumer011[String]("inforead", new SimpleStringSchema(), prop) )
    val dataStream: DataStream[String] = inputStream.map(data => {
        val dataArray = data.split(",") //对传入的对象进行分割
        InfoReading(dataArray(0), dataArray(1).toLong).toString //映射到样例类
      })
    //inputStream.print()
    dataStream.addSink(new FlinkKafkaProducer011[String]("hadoop02:9092", "sinkDemo", new SimpleStringSchema())) //写入kafka sink

    env.execute("kafka sink demo.") //启动执行器,必要操作
  }
}

posted @ 2021-01-05 18:27  Leo-Wong  阅读(1296)  评论(1编辑  收藏  举报