Нет описания

AkkaStreamFileProcessingImpl.scala 7.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  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, titlePrincipalsBasics}
  7. import fr.natan.akkastreamfileprocessingapi.models.Models.{Person, TvSeries}
  8. import fr.natan.akkastreamfileprocessingapi.models.ModelsBuilder.{buildPersonModel, buildTvSerieModel}
  9. import fr.natan.akkastreamfileprocessingapi.service.AkkaStreamComponents.{buildAllPersonsSink, buildAllTvSeriesSink, 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.language.postfixOps
  16. import scala.util.{Failure, Success}
  17. @Component
  18. class AkkaStreamFileProcessingImpl extends AkkaStreamFileProcessing {
  19. implicit val actorSystem: ActorSystem = ActorSystem("AkkaStreamActor")
  20. implicit val logger: Logger = Logger(LoggerFactory.getLogger(this.getClass))
  21. override def getAllPersons() = {
  22. val personSource: Source[Map[String, String], _] = buildSource(inputFile = nameBasics)
  23. //graph
  24. val startTime: Long = System.currentTimeMillis()
  25. val result: Future[Done] = personSource
  26. .via(flow = buildPersonFlow())
  27. .runWith(sink = buildAllPersonsSink(logger = logger))
  28. result.onComplete {
  29. case Failure(exception) => logger.error(s"$exception")
  30. case Success(value) =>
  31. logger.info(s"$value")
  32. val time: Long =(System.currentTimeMillis()-startTime)/100
  33. logger.info(s"elapsed time: $time")
  34. }
  35. }
  36. override def getPersonById(nconst: String): Future[Person] = {
  37. val source: Source[Map[String, String], _] = buildSource(inputFile = nameBasics)
  38. val startTime: Long = System.currentTimeMillis()
  39. val res = source
  40. .via(flow = filterByPersonIdFlow(nconst = nconst))
  41. .runWith(sink = buildPersonSink())
  42. res.andThen {
  43. case Failure(exception) => logger.info(s"error!!!!!!!!!!!!!$exception")
  44. case Success(value) => logger.info(s"$value")
  45. }
  46. res
  47. }
  48. override def getPersonByName(primaryName: String): Unit = {
  49. val source: Source[Map[String, String], _] = buildSource(inputFile = nameBasics)
  50. source
  51. .via(
  52. flow = filterByPersonNameFlow(personName = primaryName)
  53. )
  54. .runWith(sink = buildPersonSink())
  55. .andThen {
  56. case Failure(exception) => logger.info(s"$exception")
  57. case Success(value) => logger.info(s"$value: Successufully processing")
  58. }
  59. }
  60. override def getAllTvSeries(): Unit = {
  61. val source: Source[Map[String, String], _] = buildAndValidateSource(inputFile = titleBasics)
  62. val sink: Sink[TvSeries, Future[Done]] = buildAllTvSeriesSink(logger = logger)
  63. val startingTime: Long = System.currentTimeMillis()
  64. //graph sink->flow->sink
  65. source
  66. .via(flow = buildTvSerieFlow())
  67. .runWith(sink = sink)
  68. .andThen {
  69. case Success(value) =>
  70. val elapsedTime: Long = (System.currentTimeMillis() - startingTime) / 1000
  71. logger.info(s"$value: successfully processing file, elapsed time $titleBasics: $elapsedTime sec")
  72. case Failure(error: Error) => logger.error(s"$error")
  73. }
  74. }
  75. override def getByTvSeriePrimaryTitle(tvSeriePrimaryTitle: String):Future[IndexedSeq[TvSeries]] = {
  76. val tvSeriesSource: Source[Map[String, String], _] = buildAndValidateSource(inputFile = titleBasics)
  77. val filterByTvSerieTitle: Flow[Map[String, String], TvSeries, NotUsed] =
  78. filterByTvSeriePrimaryTitleFlow(tvSeriePrimaryTitle = tvSeriePrimaryTitle)
  79. val tvSrie :Future[IndexedSeq[TvSeries]] = tvSeriesSource
  80. .via(flow = filterByTvSerieTitle)
  81. .runWith(Sink.collection)
  82. tvSrie.andThen {
  83. case Failure(exception) => logger.info(s"$exception")
  84. case Success(value) => logger.info(s"$value")
  85. }
  86. }
  87. private def getTvSerieIdByPrimaryTitle(primaryTitle: String): String = {
  88. val source: Source[Map[String, String], _] = buildSource(inputFile = titleBasics)
  89. var tvSerie: TvSeries = null
  90. val res: Future[Option[String]] = source
  91. .filter((rowMap: Map[String, String]) => {
  92. rowMap.getOrElse(key = "primaryTitle", "") == primaryTitle
  93. })
  94. .map((rowMap: Map[String, String]) => {
  95. tvSerie = buildTvSerieModel(rowMap)
  96. rowMap.get("tconst")
  97. })
  98. .runWith(Sink.head)
  99. Await.result(res, 1 minutes)
  100. val tconst: String = res.value.get.get.get
  101. logger.info(s"TvSerie ID: $tconst, $tvSerie")
  102. tconst
  103. }
  104. private def getListOfPersonsIDByTvSerieID(tconst: String): List[Option[String]]={
  105. val source: Source[Map[String, String], _] = buildSource(inputFile = titlePrincipalsBasics)
  106. val startTime : Long = System.currentTimeMillis()
  107. val res: Future[IndexedSeq[Option[String]]] = source
  108. .filter((rowMaps: Map[String, String])=>{
  109. rowMaps.getOrElse(key = "tconst", default = "")==tconst
  110. })
  111. .map((rowMap: Map[String, String])=>{
  112. rowMap.get(key = "nconst")
  113. })
  114. .runWith(Sink.collection)
  115. .andThen {
  116. case Success(value) =>
  117. logger.info(s"$value")
  118. val elapsedTime : Long = (System.currentTimeMillis() - startTime)/1000
  119. logger.info(s"success, elapsed time: $elapsedTime")
  120. case Failure(error: Error) => logger.error(s"$error")
  121. }
  122. Await.result(res, 3 minutes)
  123. val listPersonsID: List[Option[String]] = res.value.get.get.toList
  124. listPersonsID
  125. }
  126. private def getListOfPersonsForTvSerie(nconstList: List[Option[String]]): List[Person] = {
  127. val source: Source[Map[String, String], _] = buildSource(inputFile = nameBasics)
  128. val startTime: Long = System.currentTimeMillis()
  129. val res : Future[IndexedSeq[Person]]=
  130. source
  131. .filter((rowMaps: Map[String, String])=>{
  132. nconstList.contains(rowMaps.get(key = "nconst"))
  133. })
  134. .map((rowMap: Map[String, String])=>{
  135. buildPersonModel(rowMap)
  136. })
  137. .runWith(Sink.collection)
  138. .andThen {
  139. case Failure(exception) => logger.error(s"$exception")
  140. case Success(value) =>
  141. val elapsedTime: Long = (System.currentTimeMillis()-startTime)/1000
  142. logger.info(s"END: Successfully, elapsed time: $elapsedTime")
  143. }
  144. Await.result(res, 4 minutes)
  145. val listPersons: List[Person] = res.value.get.get.toList
  146. listPersons
  147. }
  148. override def getPersonsTeamForTvSerie(tvSeriePrimaryTitle: String):Unit={
  149. val tvSerieID: String = getTvSerieIdByPrimaryTitle(primaryTitle = tvSeriePrimaryTitle)
  150. val listPersonIDs: List[Option[String]] = getListOfPersonsIDByTvSerieID(tconst = tvSerieID)
  151. val personsTeam: List[Person] = getListOfPersonsForTvSerie(nconstList = listPersonIDs)
  152. logger.info(s"Team size:${personsTeam.size}")
  153. personsTeam.foreach((person: Person)=>{
  154. logger.info(s"${person.toString}")
  155. })
  156. }
  157. }

Powered by TurnKey Linux.