Akka Kafka Producersettings:重载方法值适用于替代方案:

时间:2017-05-06 14:52:27

标签: scala akka-stream kafka-producer-api akka-kafka

当我在我的代码中放置生产者设置时,我一次又一次地遇到问题。当我没有它一切正常。下面给出了它包含所有代码的文件单个文件,我正在尝试将文件写入kafka流。并得到此编译错误。

package somePackage

import java.nio.file.Paths

import akka.Done
import akka.actor.{Actor, ActorLogging, ActorSystem, Cancellable, Props}
import akka.kafka.ProducerSettings
import akka.serialization.ByteArraySerializer
import akka.stream.{ActorMaterializer, Materializer}
import akka.stream.scaladsl.{FileIO, Sink}
import akka.util.ByteString
import org.apache.kafka.clients.producer.ProducerRecord
import org.apache.kafka.common.serialization.StringSerializer

import scala.concurrent.Future


// Lines Producer
class LinesProducer (implicit mat: Materializer) extends Actor with ActorLogging {
  import LinesProducerCompanion._

  override def preStart():Unit = {
    log.info("Not doing anything in PreStart!")
  }

  def receive = {
    case Start =>
      log.info("LinesProducer Started.")
      val future:Future[Done] = FileIO.fromPath(Paths.get("C:\\Users\\tnkteja\\Documents\\GitHub\\scala-immersion-program\\miniproject-1\\1000-genomes.csv"))
        .map( line => {new ProducerRecord[Array[Byte],ByteString]("genomes0", line)})
        .runWith( Sink.ignore )
  }

  override def postStop():Unit = {
    log.info("Doing nothing in postStop!")
  }
}

object LinesProducerCompanion {
  val props = Props[LinesProducer]
  case object Start
  case object Stop
}

object Application extends App {

  implicit val system:ActorSystem = ActorSystem("some")
  implicit val materializer:ActorMaterializer= ActorMaterializer()
  implicit val executor = system.dispatcher
  val LinesProducer = system.actorOf(LinesProducerCompanion.props, "LinesProducer")
  val producerSetting = ProducerSettings(system, new ByteArraySerializer(), new StringSerializer).withBootstrapServers("localhost:9092")
  LinesProducer ! LinesProducerCompanion.Start

  // This example app will ping pong 3 times and thereafter terminate the ActorSystem -
  // see counter logic in PingActor
  //system.awaitTermination()
}

,错误是

info] Resolving org.fusesource.jansi#jansi;1.4 ...
[info] Done updating.
[info] Set current project to project (in build file:/C:/Users/tnkteja/Documents/GitHub/scala-immersion-program/miniproject-1/)
[info] Compiling 1 Scala source to C:\Users\tnkteja\Documents\GitHub\scala-immersion-program\miniproject-1\target\scala-2.12\classes...
[error] C:\Users\tnkteja\Documents\GitHub\scala-immersion-program\miniproject-1\src\main\scala\Application.scala:29: overloaded method value apply with alternatives:
[error]   [K, V](config: com.typesafe.config.Config, keySerializer: org.apache.kafka.common.serialization.Serializer[K], valueSerializer: org.apache.kafka.common.serialization.Serializer[V
])akka.kafka.ProducerSettings[K,V] <and>
[error]   [K, V](system: akka.actor.ActorSystem, keySerializer: org.apache.kafka.common.serialization.Serializer[K], valueSerializer: org.apache.kafka.common.serialization.Serializer[V])ak
ka.kafka.ProducerSettings[K,V] <and>
[error]   [K, V](config: com.typesafe.config.Config, keySerializer: Option[org.apache.kafka.common.serialization.Serializer[K]], valueSerializer: Option[org.apache.kafka.common.serializati
on.Serializer[V]])akka.kafka.ProducerSettings[K,V] <and>
[error]   [K, V](system: akka.actor.ActorSystem, keySerializer: Option[org.apache.kafka.common.serialization.Serializer[K]], valueSerializer: Option[org.apache.kafka.common.serialization.S
erializer[V]])akka.kafka.ProducerSettings[K,V]
[error]  cannot be applied to (akka.actor.ActorSystem, akka.serialization.ByteArraySerializer, org.apache.kafka.common.serialization.StringSerializer)
[error]       val producerSetting = ProducerSettings(system, new ByteArraySerializer(), new StringSerializer).withBootstrapServers("localhost:9092")
[error]                             ^
[error] one error found
[error] (compile:compileIncremental) Compilation failed
[error] Total time: 8 s, completed 6 May, 2017 10:42:15 AM

1 个答案:

答案 0 :(得分:1)

您是否尝试过使用Kafka的ByteArraySerializer

import org.apache.kafka.common.serialization.ByteArraySerializer

Akka的ByteArraySerializer()实例构造函数已被弃用。