placidenduwayo преди 2 години
родител
ревизия
3fb8efc08d

+ 1
- 0
src/main/scala/fr/natan/akkastreamfileprocessingapi/datasource/Datasource.scala Целия файл

@@ -7,4 +7,5 @@ object Datasource {
7 7
   val titleBasics: File = new File("../title.basics.tsv.gz")
8 8
   val nameBasics: File = new File("../name.basics.tsv.gz")
9 9
   val titlePrincipalsBasics: File = new File("../title.basics.tsv.gz")
10
+  val separator: String = "\t"
10 11
 }

+ 17
- 7
src/main/scala/fr/natan/akkastreamfileprocessingapi/models/Person.scala Целия файл

@@ -1,9 +1,19 @@
1 1
 package fr.natan.akkastreamfileprocessingapi.models
2 2
 
3
-final case class Person(nconst: String,
4
-                  primaryName: String,
5
-                  birthYear: String,
6
-                  dearthYear: String,
7
-                  primaryProfession: String,
8
-                  knownForTitles: List[String]
9
-                 )
3
+final case class Person(
4
+                         nconst: String,
5
+                         primaryName: String,
6
+                         birthYear: String,
7
+                         dearthYear: String,
8
+                         primaryProfession: List[String],
9
+                         knownForTitles: List[String]
10
+                       ){
11
+  override def toString: String = {
12
+    "Person[ nconst:"+nconst+
13
+      ", primaryName:"+primaryName+
14
+    ", birthYear:"+birthYear+
15
+    ", dearthYear:"+dearthYear+
16
+    ", primaryProfession:"+primaryProfession+
17
+    ", knownForTitles:"+knownForTitles
18
+  }
19
+}

+ 9
- 3
src/main/scala/fr/natan/akkastreamfileprocessingapi/models/TvSeries.scala Целия файл

@@ -13,8 +13,14 @@ final case class TvSeries(
13 13
                    ) {
14 14
 
15 15
   override def toString: String = {
16
-    "TvSerie[tvSerieID : " + tconst +", title-type:"+titleType+", primary-title:"+primaryTitle+"" +
17
-      ", riginal-title:"+originalTitle+", is adult (no=0, yes=1):"+isAdult+", start year:"+startYear+
18
-    ", end year:"+endYear+", runtime minutes:"+runtimeMinutes+", genre:"+genre+"]"
16
+    "TvSerie[tvSerieID : " + tconst +
17
+      ", title-type:"+titleType+
18
+      ", primary-title:"+primaryTitle+"" +
19
+      ", riginal-title:"+originalTitle+
20
+      ", is adult (no=0, yes=1):"+isAdult+
21
+      ", start year:"+startYear+
22
+    ", end year:"+endYear+
23
+      ", runtime minutes:"+runtimeMinutes+
24
+      ", genre:"+genre+"]"
19 25
   }
20 26
 }

+ 44
- 11
src/main/scala/fr/natan/akkastreamfileprocessingapi/service/AkkaStreamComponents.scala Целия файл

@@ -7,7 +7,8 @@ import akka.util.ByteString
7 7
 import akka.{Done, NotUsed}
8 8
 import com.typesafe.scalalogging.slf4j.Logger
9 9
 import fr.natan.akkastreamfileprocessingapi.businessexceptions.FileNotFoundException
10
-import fr.natan.akkastreamfileprocessingapi.models.TvSeries
10
+import fr.natan.akkastreamfileprocessingapi.datasource.Datasource.separator
11
+import fr.natan.akkastreamfileprocessingapi.models.{Person, TvSeries}
11 12
 import fr.natan.akkastreamfileprocessingapi.valitator.Validators.fileExists
12 13
 
13 14
 import java.io.File
@@ -18,7 +19,7 @@ object AkkaStreamComponents {
18 19
 
19 20
   implicit val actor: ActorSystem = ActorSystem("AkkaStreamActor")
20 21
 
21
-  private def convertToClassMovie(array: Array[String]): TvSeries = {
22
+  private def convertToTvSerie(array: Array[String]): TvSeries = {
22 23
     val tvSerie: TvSeries = TvSeries(
23 24
       array(0),
24 25
       array(1),
@@ -33,19 +34,42 @@ object AkkaStreamComponents {
33 34
     tvSerie
34 35
   }
35 36
 
37
+  private def convertToPerson(array: Array[String]): Person ={
38
+    Person(
39
+      array(0),
40
+      array(1),
41
+      array(2),
42
+      array(3),
43
+      array(4).split(",").toList,
44
+      array(5).split(",").toList,
45
+    )
46
+  }
47
+
36 48
   //flows
37 49
 
38
-  def buildMovieFlow(movieID: String): Flow[String, TvSeries, NotUsed] = {
50
+  def buildTvSerieFlow(movieID: String): Flow[String, TvSeries, NotUsed] = {
39 51
 
40
-    val flow: Flow[String, TvSeries, NotUsed] =
52
+    val tvFlow: Flow[String, TvSeries, NotUsed] =
41 53
       Flow[String].filter(rows => !rows.contains(movieID))
42 54
         .map(row => {
43
-          val movie: Array[String] = row.split("\t")
44
-          convertToClassMovie(movie)
55
+          val movie: Array[String] = row.split(separator)
56
+          convertToTvSerie(movie)
45 57
         })
46
-    flow
58
+    tvFlow
47 59
   }
48 60
 
61
+  def buildPersonFlow(personID: String): Flow[String, Person, NotUsed]={
62
+    val personFlow: Flow[String, Person, NotUsed] =
63
+      Flow[String]
64
+        .filterNot((rows: String)=>{
65
+          rows.contains(personID)
66
+        })
67
+        .map((row: String)=>{
68
+          convertToPerson(row.split(separator))
69
+        })
70
+
71
+    personFlow
72
+  }
49 73
   def buildfilterByMoviePrimaryTitleFlow(moviePrimaryTitle: String): Flow[String, TvSeries, NotUsed] = {
50 74
     val filterFlow: Flow[String, TvSeries, NotUsed] =
51 75
       Flow[String]
@@ -54,7 +78,7 @@ object AkkaStreamComponents {
54 78
         })
55 79
         .map(row => {
56 80
           val movie: Array[String] = row.split("\t")
57
-          convertToClassMovie(movie)
81
+          convertToTvSerie(movie)
58 82
         })
59 83
 
60 84
     filterFlow
@@ -93,12 +117,21 @@ object AkkaStreamComponents {
93 117
   }
94 118
 
95 119
   //sink
96
-  def buildSink(logger: Logger): Sink[TvSeries, Future[Done]] = {
97
-    val sink = Sink.foreach[TvSeries](
120
+  def buildTvSeriesSink(logger: Logger): Sink[TvSeries, Future[Done]] = {
121
+    val tvSeriesSink : Sink[TvSeries, Future[Done]]= Sink.foreach[TvSeries](
98 122
       (movie: TvSeries) => {
99 123
         logger.info(s"${movie.toString}")
100 124
       }
101 125
     )
102
-    sink
126
+    tvSeriesSink
127
+  }
128
+
129
+  def buildPersonSink(logger: Logger): Sink[Person, Future[Done]] ={
130
+    val personSink :Sink[Person, Future[Done]] = Sink
131
+      .foreach[Person]((person: Person)=>{
132
+        logger.info(s"${person.toString}")
133
+      })
134
+
135
+    personSink
103 136
   }
104 137
 }

+ 2
- 0
src/main/scala/fr/natan/akkastreamfileprocessingapi/service/AkkaStreamFileProcessing.scala Целия файл

@@ -4,4 +4,6 @@ trait AkkaStreamFileProcessing {
4 4
 
5 5
   def getAllMovies()
6 6
   def getMoviesByTitle (movieTitle: String)
7
+
8
+  def getAllPersons()
7 9
 }

+ 8
- 4
src/main/scala/fr/natan/akkastreamfileprocessingapi/service/AkkaStreamFileProcessingImpl.scala Целия файл

@@ -6,7 +6,7 @@ import akka.{Done, NotUsed}
6 6
 import com.typesafe.scalalogging.slf4j.Logger
7 7
 import fr.natan.akkastreamfileprocessingapi.datasource.Datasource.titleBasics
8 8
 import fr.natan.akkastreamfileprocessingapi.models.TvSeries
9
-import fr.natan.akkastreamfileprocessingapi.service.AkkaStreamComponents.{buildAndValidateSource, buildMovieFlow, buildSink, buildfilterByMoviePrimaryTitleFlow}
9
+import fr.natan.akkastreamfileprocessingapi.service.AkkaStreamComponents.{buildAndValidateSource, buildTvSerieFlow, buildTvSeriesSink, buildfilterByMoviePrimaryTitleFlow}
10 10
 import org.slf4j.LoggerFactory
11 11
 import org.springframework.stereotype.Component
12 12
 
@@ -23,14 +23,14 @@ class AkkaStreamFileProcessingImpl extends AkkaStreamFileProcessing {
23 23
   override def getAllMovies(): Unit = {
24 24
 
25 25
     val source: Source[String, NotUsed] = buildAndValidateSource(inputFile = titleBasics)
26
-    val sink: Sink[TvSeries, Future[Done]] = buildSink(logger = logger)
26
+    val sink: Sink[TvSeries, Future[Done]] = buildTvSeriesSink(logger = logger)
27 27
 
28 28
 
29 29
     val startingTime: Long = System.currentTimeMillis()
30 30
 
31 31
     //graph sink->flow->sink
32 32
     source
33
-      .via(flow = buildMovieFlow(movieID = "tconst"))
33
+      .via(flow = buildTvSerieFlow(movieID = "tconst"))
34 34
       .runWith(sink = sink)
35 35
       .andThen {
36 36
       case Success(value) =>
@@ -44,7 +44,7 @@ class AkkaStreamFileProcessingImpl extends AkkaStreamFileProcessing {
44 44
   override def getMoviesByTitle(moviePrimaryTitle: String): Unit = {
45 45
 
46 46
     val source: Source[String, NotUsed] = buildAndValidateSource(inputFile = titleBasics)
47
-    val sink: Sink[TvSeries, Future[Done]] = buildSink(logger = logger)
47
+    val sink: Sink[TvSeries, Future[Done]] = buildTvSeriesSink(logger = logger)
48 48
 
49 49
     val filterByMovieTitleFlow: Flow[String, TvSeries, NotUsed] =
50 50
       buildfilterByMoviePrimaryTitleFlow(moviePrimaryTitle = moviePrimaryTitle)
@@ -59,6 +59,10 @@ class AkkaStreamFileProcessingImpl extends AkkaStreamFileProcessing {
59 59
       case Failure(error: Error) => logger.error(s"$error")
60 60
     }
61 61
   }
62
+
63
+  override def getAllPersons(): Unit = {
64
+    ???
65
+  }
62 66
 }
63 67
 
64 68
 

Powered by TurnKey Linux.