我有以下案例类:
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))
我不确定发生了什么事!
答案 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))
}