大家好,我如何通过带有Alpakka Kafka连接器的http-akka广告将数据发送到kafka,这是我的来源:
object WebTrack extends App with Directives with LazyLogging {
val host = "localhost"
val port = 7070
val authorization = "Authorization"
val route = withSizeLimit(96239727) {
post {
headerValueByName("Authorization") { auth =>
entity(as[data]) { trans =>
//************************** I need send data to Alpakka Kafka connector ********************************
complete(HttpEntity(ContentTypes.`text/html(UTF-8)`, "<h1>Say hello to akka-http</h1>"))
}
}
}
}
val bindingFuture = Http().bindAndHandleAsync(Route.asyncHandler(route), host, port)
.onComplete {
case Success(_) => {
logger.debug("This is very convenient ;-)")
println(s"Server online at http://localhost:7070 \\nPress RETURN to stop...")
}
case Failure(e) => {
println("Error Bind Http().bindAndHandleAsync")
}
}
}
答案 0 :(得分:0)
Akka Streams可以完成很多不同的事情
根据文档https://doc.akka.io/docs/akka-stream-kafka/current/producer.html,这里仅是一个使用Producer
接收器的示例。请务必查看passThrough
及其可以执行的一些操作:
implicit val system: ActorSystem = ActorSystem("my-system")
implicit val materializer: ActorMaterializer = ActorMaterializer()
implicit val executionContext: ExecutionContextExecutor = system.dispatcher
case class User(firstName:String, lastName:String)
object UserJsonSupport extends DefaultJsonProtocol with SprayJsonSupport {
implicit val PortofolioFormats = jsonFormat2(User)
}
import UserJsonSupport._
val config = system.settings.config.getConfig("akka.kafka.producer")
val producerSettings = ProducerSettings(config, new StringSerializer, new StringSerializer)
val route = withSizeLimit(96239727) {
post {
headerValueByName("Authorization") { auth =>
entity(as[User]) { user: User =>
Source.single(user)
.map(user => new ProducerRecord[String, String]("last_names_topic", user.lastName, user.firstName))
.runWith(Producer.plainSink(producerSettings))
complete(HttpEntity(ContentTypes.`text/html(UTF-8)`,
"<h1>Say hello to akka-http</h1>"))
}
}
}
}