任何人都可以在Scala中分享Flink Kafka(主要是从Kafka接收消息)的工作示例吗?我知道Spark中有一个KafkaWordCount示例。我只需要在Flink打印出Kafka消息。这真的很有帮助。
答案 0 :(得分:1)
与 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()
}
}