Nessuna descrizione

AkkaStreamFileProcessingImpl.scala 6.6KB

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

Powered by TurnKey Linux.