Bez popisu

AkkaStreamFileProcessingImpl.scala 5.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. package fr.natan.akkastreamfileprocessingapi.service
  2. import akka.actor.ActorSystem
  3. import akka.stream.scaladsl.{Flow, Sink, Source}
  4. import akka.{Done, NotUsed}
  5. import com.typesafe.scalalogging.slf4j.Logger
  6. import fr.natan.akkastreamfileprocessingapi.datasource.Datasource.{nameBasics, titleBasics}
  7. import fr.natan.akkastreamfileprocessingapi.models.Models.{Person, TvSeries}
  8. import fr.natan.akkastreamfileprocessingapi.models.ModelsBuilder.buildTvSerieModel
  9. import fr.natan.akkastreamfileprocessingapi.service.AkkaStreamComponents.{buildAllPersonsSink, buildAndValidateSource, buildPersonFlow, buildPersonSink, buildSource, buildTvSerieFlow, buildTvSeriesSink, filterByPersonIdFlow, filterByPersonNameFlow, filterByTvSeriePrimaryTitleFlow}
  10. import org.slf4j.LoggerFactory
  11. import org.springframework.stereotype.Component
  12. import scala.concurrent.ExecutionContext.Implicits.global
  13. import scala.concurrent.duration.DurationInt
  14. import scala.concurrent.{Await, Future}
  15. import scala.util.{Failure, Success}
  16. @Component
  17. class AkkaStreamFileProcessingImpl extends AkkaStreamFileProcessing {
  18. implicit val actorSystem: ActorSystem = ActorSystem("AkkaStreamActor")
  19. implicit val logger: Logger = Logger(LoggerFactory.getLogger(this.getClass))
  20. override def getAllPersons(): Unit = {
  21. val personSource: Source[Map[String, String], _] = buildSource(inputFile = nameBasics)
  22. val personSink: Sink[Person, Future[Done]] = buildAllPersonsSink(logger = logger)
  23. //graph
  24. val startTime: Long = System.currentTimeMillis()
  25. val result: Future[Done] = personSource
  26. .via(flow = buildPersonFlow())
  27. .runWith(sink = personSink)
  28. .andThen {
  29. case Success(value) =>
  30. val elapsedTime: Long = (System.currentTimeMillis() - startTime) / 1000
  31. logger.info(s"$value: Successfully processed, elapsed time: $elapsedTime")
  32. case Failure(exception) => logger.error(s"$exception: Fail")
  33. }
  34. }
  35. override def getPersonById(nconst: String): Person = {
  36. val source: Source[Map[String, String], _] = buildSource(inputFile = nameBasics)
  37. val startTime: Long = System.currentTimeMillis()
  38. val res = source
  39. .via(flow = filterByPersonIdFlow(nconst = nconst))
  40. .runWith(sink = buildPersonSink())
  41. .andThen {
  42. case Success(value) =>
  43. val elapsedTime: Long = (System.currentTimeMillis()-startTime)/1000
  44. logger.info(s"$value: Successfully processed, elapsed time: $elapsedTime")
  45. case Failure(exception) => logger.error(s"$exception: Fail")
  46. }
  47. Await.result(res,1 minutes)
  48. val person: Person = res.value.get.get
  49. person
  50. }
  51. override def getPersonByName(primaryName: String): Unit = {
  52. val source: Source[Map[String, String], _] = buildSource(inputFile = nameBasics)
  53. source
  54. .via(
  55. flow = filterByPersonNameFlow(personName = primaryName)
  56. )
  57. .runWith(sink = buildPersonSink())
  58. .andThen {
  59. case Failure(exception) => logger.info(s"$exception")
  60. case Success(value) => logger.info(s"$value: Successufully processing")
  61. }
  62. }
  63. override def getAllTvSeries(): Unit = {
  64. val source: Source[Map[String, String], _] = buildAndValidateSource(inputFile = titleBasics)
  65. val sink: Sink[TvSeries, Future[Done]] = buildTvSeriesSink(logger = logger)
  66. val startingTime: Long = System.currentTimeMillis()
  67. //graph sink->flow->sink
  68. source
  69. .via(flow = buildTvSerieFlow())
  70. .runWith(sink = sink)
  71. .andThen {
  72. case Success(value) =>
  73. val elapsedTime: Long = (System.currentTimeMillis() - startingTime) / 1000
  74. logger.info(s"$value: successfully processing file, elapsed time $titleBasics: $elapsedTime sec")
  75. case Failure(error: Error) => logger.error(s"$error")
  76. }
  77. }
  78. override def getByTvSeriePrimaryTitle(tvSeriePrimaryTitle: String): Unit = {
  79. val tvSeriesSource: Source[Map[String, String], _] = buildAndValidateSource(inputFile = titleBasics)
  80. val tvSeriesSink: Sink[TvSeries, Future[Done]] = buildTvSeriesSink(logger = logger)
  81. val filterByMovieTitleFlow: Flow[Map[String, String], TvSeries, NotUsed] =
  82. filterByTvSeriePrimaryTitleFlow(tvSeriePrimaryTitle = tvSeriePrimaryTitle)
  83. val startTime: Long = System.currentTimeMillis()
  84. val listTvSeries: Future[Done] = tvSeriesSource
  85. .via(flow = filterByMovieTitleFlow)
  86. .runWith(sink = tvSeriesSink)
  87. .andThen {
  88. case Success(value) =>
  89. val elapsedTime: Long = (System.currentTimeMillis() - startTime) / 1000
  90. logger.info(s"$value: successfully processing file, elapsed time: $elapsedTime sec")
  91. case Failure(error: Error) => logger.error(s"$error")
  92. }
  93. getTvSerieIdByPrimaryTitle(primaryTitle = tvSeriePrimaryTitle)
  94. }
  95. private def getTvSerieIdByPrimaryTitle(primaryTitle: String): String ={
  96. val source: Source[Map[String, String],_]= buildSource(inputFile = titleBasics)
  97. var tvSerie: TvSeries =null
  98. val res : Future[Option[String]]= source
  99. .filter((rowMap: Map[String, String]) =>{
  100. rowMap.getOrElse(key = "primaryTitle","")==primaryTitle
  101. })
  102. .map((rowMap: Map[String, String])=>{
  103. tvSerie = buildTvSerieModel(rowMap)
  104. rowMap.get("tconst")
  105. })
  106. .runWith(Sink.head)
  107. Await.result(res,1 minutes)
  108. val tconst:String = res.value.get.get.get
  109. tconst
  110. }
  111. }

Powered by TurnKey Linux.