如何使用发布者生成消息(在reactive-kafka中)?

时间:2016-08-23 07:31:02

标签: scala apache-kafka reactive-kafka

我必须使用来自reactive-kafka模块的代码:https://github.com/akka/reactive-kafka/blob/master/README.md

我的代码以:

开头
val kafka = new ReactiveKafka()

val kafkaIdpsMsgs: Publisher[StringKafkaMessage] = kafka.consume(
      ConsumerProperties(
        brokerList = kafkaHosts,
        zooKeeperHost = zkHosts,
        topic = "test",
        groupId = "idps-translator",
        decoder = new StringDecoder()
      ).readFromEndOfStream())

    val kafkaSamples: Subscriber[String] = kafka.publish(ProducerProperties(
      brokerList = kafkaHosts,
      topic = "test",
      encoder = new StringEncoder()
    ))

我想制作一条消息(由出版商提供)。为了实现它,我必须编写什么代码?

1 个答案:

答案 0 :(得分:0)

val done = Source(1 to 100)
  .map(_.toString)
  .map { elem =>
    new ProducerRecord[Array[Byte], String]("topic1", elem)
  }
  .runWith(Producer.plainSink(producerSettings))

您可以浏览此文档:http://doc.akka.io/docs/akka-stream-kafka/current/producer.html