将案例类别列表连接到kafka生产者?

时间:2019-04-02 17:30:59

标签: scala apache-kafka akka alpakka

我有以下案例类:

case class Alpakka(id:Int,name:String,animal_type:String)

我试图通过使用以下代码将这些案例类的列表连接到kafka中的生产者:

  def connectEntriesToProducer(seq: Seq[Alpakka]) = {


    val producerSettings = ProducerSettings(system, new StringSerializer, new StringSerializer)
      .withBootstrapServers("localhost:9092")

seq.map(alpakka => new ProducerRecord[String, String]("alpakkas", alpakka.asJson.noSpaces))   
      .runWith(Producer.plainSink(producerSettings))
  }

我正在使用circe将case类转换为json。但是,我一直收到这样的编译器错误:

Error:(87, 34) type mismatch;
 found   : akka.stream.scaladsl.Sink[org.apache.kafka.clients.producer.ProducerRecord[String,String],scala.concurrent.Future[akka.Done]]
 required: org.apache.kafka.clients.producer.ProducerRecord[String,String] => ?
      .runWith(Producer.plainSink(producerSettings))

我不确定发生了什么事!

1 个答案:

答案 0 :(得分:0)

您正在尝试从Graph而非Seq构建Source

您的方法connectEntriesToProducer应该看起来

def connectEntriesToProducer(seq: Source[Alpakka]) = {

请注意,Source代替了Seq

或者,您可以从Seq构建源,但是必须使用immutable.Seq,因为Source.apply只需要一个不变的可迭代对象。

def connectEntriesToProducer(seq: scala.collection.immutable.Seq[Alpakka]) = {
val producerSettings = ProducerSettings(system, new StringSerializer, new StringSerializer)
  .withBootstrapServers("localhost:9092")

Source(seq).
  map(alpakka => new ProducerRecord[String, String]("alpakkas", alpakka.asJson.noSpaces))
  .runWith(Producer.plainSink(producerSettings))
}