6

任何人都可以在 Scala 中分享 Flink Kafka(主要从 Kafka 接收消息)的工作示例吗?我知道 Spark 中有一个KafkaWordCount示例。我只需要在 Flink 中打印出 Kafka 消息。这真的很有帮助。

4

2 回答 2

12

以下代码展示了如何使用 Flink 的 Scala DataStream API 从 Kafka 主题中读取数据:

import org.apache.flink.streaming.api.scala._
import org.apache.flink.streaming.connectors.kafka.FlinkKafkaConsumer082
import org.apache.flink.streaming.util.serialization.SimpleStringSchema


object Main {
  def main(args: Array[String]) {
    val env = StreamExecutionEnvironment.getExecutionEnvironment
    val properties = new Properties()
    properties.setProperty("bootstrap.servers", "localhost:9092")
    properties.setProperty("zookeeper.connect", "localhost:2181")
    properties.setProperty("group.id", "test")
    val stream = env
      .addSource(new FlinkKafkaConsumer082[String]("topic", new SimpleStringSchema(), properties))
      .print

    env.execute("Flink Kafka Example")
  }
}
于 2015-11-23T16:37:11.707 回答
2

与 Robert 添加的相反,下面是一段用于向 Kafka 主题发送消息的应用程序代码。

import org.apache.kafka.clients.producer.{KafkaProducer, ProducerRecord}

object KafkaProducer {

  def main(args: Array[String]): Unit = {
    KafkaProducer.sendMessageToKafkaTopic("localhost:9092", "topic_name")
  }    

  def sendMessageToKafkaTopic(server: String, topic:String): Unit = {
    val props = new Properties()
    props.put("bootstrap.servers", servers)
    props.put("key.serializer", "org.apache.kafka.common.serialization.StringSerializer")
    props.put("value.serializer", "org.apache.kafka.common.serialization.StringSerializer")
    val producer = new KafkaProducer[String,String](props)
    val record = new ProducerRecord[String,String](topic, "HELLO WORLD!")
    producer.send(record)
    producer.close()
  }
}
于 2020-12-29T20:42:00.200 回答