使用Akka流和http的连续REST消耗

使用Akka流和http的连续REST消耗,akka,hdfs,akka-http,akka-stream,akka-persistence,Akka,Hdfs,Akka Http,Akka Stream,Akka Persistence,我正在尝试构建一个基于akka的系统,该系统将定期(每15秒)发送REST请求,对接收到的数据进行一些过滤、一些数据清理和验证,并保存到HDFS中 下面是我写的代码 import akka.actor.ActorSystem import akka.stream.ActorMaterializer import akka.stream.scaladsl.{Flow, Sink, Source} import akka.http.scaladsl.Http import akka.http.sca

我正在尝试构建一个基于akka的系统,该系统将定期(每15秒)发送REST请求,对接收到的数据进行一些过滤、一些数据清理和验证,并保存到HDFS中

下面是我写的代码

import akka.actor.ActorSystem
import akka.stream.ActorMaterializer
import akka.stream.scaladsl.{Flow, Sink, Source}
import akka.http.scaladsl.Http
import akka.http.scaladsl.model.{HttpRequest, HttpResponse, StatusCodes}
import akka.actor.Props
import akka.event.Logging
import akka.actor.Actor

import scala.concurrent.{ExecutionContext, Future}
import scala.util.Try
import akka.http.scaladsl.client.RequestBuilding._

/**
  * Created by rabbanerjee on 4/6/2017.
  */
class MyActor extends Actor {
  val log = Logging(context.system, this)

  import scala.concurrent.ExecutionContext.Implicits.global
  def receive = {
    case j:HttpResponse => log.info("received" +j)
    case k:AnyRef      => log.info("received unknown message"+k)
  }
}

object STest extends App{

  implicit val system = ActorSystem("Sys")
  import system.dispatcher

  implicit val materializer = ActorMaterializer()

  val ss = system.actorOf(Props[MyActor])
  val httpClient = Http().outgoingConnection(host = "rest_server.com", port = 8080)
  val filterSuccess = Flow[HttpResponse].filter(_.status.isSuccess())

    val runnnn = Source.tick(
            FiniteDuration(1,TimeUnit.SECONDS),
            FiniteDuration(15,TimeUnit.SECONDS),
            Get("/"))
        .via(httpClient)
        .via(filterSuccess)
        .to(Sink.actorRef(ss,onCompleteMessage = "done"))

  runnnn.run()
} 
我现在面临的问题是,

即使我对源代码使用了repeat/tick,我也能看到一次结果。它不会重复触发请求。


我还试图找到50个这样的请求的分组结果,因为我将把它写到hadoop,我不能写每一个请求,因为它会用多个文件淹没HDFS。

您没有使用从HTTP调用返回的响应。必须使用Akka HTTP返回的实体字节,即使您对它们不感兴趣

有关这方面的更多信息,请参阅

在您的示例中,由于您不使用响应实体,因此可以丢弃其字节。见下例:

val runnnn = Source.tick(FiniteDuration(1,TimeUnit.SECONDS),FiniteDuration(15,TimeUnit.SECONDS),Get("/"))
    .via(httpClient)
    .map{resp => resp.discardEntityBytes(); resp}
    .via(filterSuccess)
    .to(Sink.actorRef(ss,onCompleteMessage = "done"))