Browse Source

first commit

placidenduwayo 1 year ago
parent
commit
b9afd85bba

+ 9
- 0
src/main/java/fr/natan/akkastreamfileprocessingapi/controller/MovieController.java View File

8
 import org.springframework.web.bind.annotation.RequestMethod;
8
 import org.springframework.web.bind.annotation.RequestMethod;
9
 import org.springframework.web.bind.annotation.RestController;
9
 import org.springframework.web.bind.annotation.RestController;
10
 
10
 
11
+import java.util.Collection;
12
+import java.util.List;
13
+
11
 @RestController
14
 @RestController
12
 public class MovieController {
15
 public class MovieController {
13
 
16
 
47
         akkaStreamFilesProcessing.getByTvSeriePrimaryTitle(movieTitle);
50
         akkaStreamFilesProcessing.getByTvSeriePrimaryTitle(movieTitle);
48
         return new ResponseEntity<>("is running", HttpStatus.OK);
51
         return new ResponseEntity<>("is running", HttpStatus.OK);
49
     }
52
     }
53
+
54
+    @RequestMapping(value = "/movies/id/{tvSerieID}", method = RequestMethod.GET)
55
+    private ResponseEntity<String> getListOfPersonsIDByTvSerieID(@PathVariable(name = "tvSerieID") String tvSerieID){
56
+       akkaStreamFilesProcessing.getListOfPersonsIDByTvSerieID(tvSerieID);
57
+       return new ResponseEntity<>("is running", HttpStatus.OK);
58
+    }
50
 }
59
 }

+ 1
- 1
src/main/scala/fr/natan/akkastreamfileprocessingapi/datasource/Datasource.scala View File

6
 
6
 
7
   val titleBasics: File = new File("../title.basics.tsv.gz")
7
   val titleBasics: File = new File("../title.basics.tsv.gz")
8
   val nameBasics: File = new File("../name.basics.tsv.gz")
8
   val nameBasics: File = new File("../name.basics.tsv.gz")
9
-  val titlePrincipalsBasics: File = new File("../title.basics.tsv.gz")
9
+  val titlePrincipalsBasics: File = new File("../title.principals.tsv.gz")
10
   val separator: String = "\t"
10
   val separator: String = "\t"
11
 }
11
 }

+ 2
- 1
src/main/scala/fr/natan/akkastreamfileprocessingapi/service/AkkaStreamComponents.scala View File

12
 
12
 
13
 import java.io.File
13
 import java.io.File
14
 import java.nio.file.Paths
14
 import java.nio.file.Paths
15
+import java.util
15
 import scala.concurrent.Future
16
 import scala.concurrent.Future
16
 
17
 
17
 object AkkaStreamComponents {
18
 object AkkaStreamComponents {
102
       }
103
       }
103
       )
104
       )
104
       .via(Compression.gunzip())
105
       .via(Compression.gunzip())
105
-      .via(CsvParsing.lineScanner(CsvParsing.Tab, CsvParsing.DoubleQuote))
106
+      .via(CsvParsing.lineScanner(CsvParsing.Tab, CsvParsing.DoubleQuote, CsvParsing.DoubleQuote))
106
       .via(CsvToMap.toMapAsStrings())
107
       .via(CsvToMap.toMapAsStrings())
107
 
108
 
108
     datasource
109
     datasource

+ 2
- 0
src/main/scala/fr/natan/akkastreamfileprocessingapi/service/AkkaStreamFileProcessing.scala View File

11
   def getAllTvSeries(): Unit
11
   def getAllTvSeries(): Unit
12
 
12
 
13
   def getByTvSeriePrimaryTitle(tvSerieTitle: String): Unit
13
   def getByTvSeriePrimaryTitle(tvSerieTitle: String): Unit
14
+
15
+  def getListOfPersonsIDByTvSerieID(tconst: String): List[Option[String]]
14
 }
16
 }

+ 48
- 11
src/main/scala/fr/natan/akkastreamfileprocessingapi/service/AkkaStreamFileProcessingImpl.scala View File

1
 package fr.natan.akkastreamfileprocessingapi.service
1
 package fr.natan.akkastreamfileprocessingapi.service
2
 
2
 
3
 import akka.actor.ActorSystem
3
 import akka.actor.ActorSystem
4
+import akka.stream.ActorAttributes.supervisionStrategy
5
+import akka.stream.Supervision.resumingDecider
4
 import akka.stream.scaladsl.{Flow, Sink, Source}
6
 import akka.stream.scaladsl.{Flow, Sink, Source}
5
 import akka.{Done, NotUsed}
7
 import akka.{Done, NotUsed}
6
 import com.typesafe.scalalogging.slf4j.Logger
8
 import com.typesafe.scalalogging.slf4j.Logger
7
-import fr.natan.akkastreamfileprocessingapi.datasource.Datasource.{nameBasics, titleBasics}
9
+import fr.natan.akkastreamfileprocessingapi.datasource.Datasource.{nameBasics, titleBasics, titlePrincipalsBasics}
8
 import fr.natan.akkastreamfileprocessingapi.models.Models.{Person, TvSeries}
10
 import fr.natan.akkastreamfileprocessingapi.models.Models.{Person, TvSeries}
9
 import fr.natan.akkastreamfileprocessingapi.models.ModelsBuilder.buildTvSerieModel
11
 import fr.natan.akkastreamfileprocessingapi.models.ModelsBuilder.buildTvSerieModel
10
 import fr.natan.akkastreamfileprocessingapi.service.AkkaStreamComponents.{buildAllPersonsSink, buildAndValidateSource, buildPersonFlow, buildPersonSink, buildSource, buildTvSerieFlow, buildTvSeriesSink, filterByPersonIdFlow, filterByPersonNameFlow, filterByTvSeriePrimaryTitleFlow}
12
 import fr.natan.akkastreamfileprocessingapi.service.AkkaStreamComponents.{buildAllPersonsSink, buildAndValidateSource, buildPersonFlow, buildPersonSink, buildSource, buildTvSerieFlow, buildTvSeriesSink, filterByPersonIdFlow, filterByPersonNameFlow, filterByTvSeriePrimaryTitleFlow}
14
 import scala.concurrent.ExecutionContext.Implicits.global
16
 import scala.concurrent.ExecutionContext.Implicits.global
15
 import scala.concurrent.duration.DurationInt
17
 import scala.concurrent.duration.DurationInt
16
 import scala.concurrent.{Await, Future}
18
 import scala.concurrent.{Await, Future}
19
+import scala.language.postfixOps
17
 import scala.util.{Failure, Success}
20
 import scala.util.{Failure, Success}
21
+import scala.collection.immutable.Iterable
18
 
22
 
19
 @Component
23
 @Component
20
 class AkkaStreamFileProcessingImpl extends AkkaStreamFileProcessing {
24
 class AkkaStreamFileProcessingImpl extends AkkaStreamFileProcessing {
49
      .andThen {
53
      .andThen {
50
         case Success(value) =>
54
         case Success(value) =>
51
           val elapsedTime: Long = (System.currentTimeMillis()-startTime)/1000
55
           val elapsedTime: Long = (System.currentTimeMillis()-startTime)/1000
52
-          logger.info(s"$value: Successfully processed, elapsed time: $elapsedTime")
56
+          logger.info(s"$value")
57
+          logger.info(s"Successfully processed, elapsed time: $elapsedTime")
53
         case Failure(exception) => logger.error(s"$exception: Fail")
58
         case Failure(exception) => logger.error(s"$exception: Fail")
54
       }
59
       }
55
 
60
 
111
       }
116
       }
112
 
117
 
113
     getTvSerieIdByPrimaryTitle(primaryTitle = tvSeriePrimaryTitle)
118
     getTvSerieIdByPrimaryTitle(primaryTitle = tvSeriePrimaryTitle)
119
+
114
   }
120
   }
115
 
121
 
116
-  private def getTvSerieIdByPrimaryTitle(primaryTitle: String): String ={
117
-    val source: Source[Map[String, String],_]= buildSource(inputFile = titleBasics)
122
+  private def getTvSerieIdByPrimaryTitle(primaryTitle: String): String = {
123
+    val source: Source[Map[String, String], _] = buildSource(inputFile = titleBasics)
118
 
124
 
119
-    var tvSerie: TvSeries =null
120
-    val res : Future[Option[String]]= source
121
-      .filter((rowMap: Map[String, String]) =>{
122
-        rowMap.getOrElse(key = "primaryTitle","")==primaryTitle
125
+    var tvSerie: TvSeries = null
126
+    val res: Future[Option[String]] = source
127
+      .filter((rowMap: Map[String, String]) => {
128
+        rowMap.getOrElse(key = "primaryTitle", "") == primaryTitle
123
       })
129
       })
124
-      .map((rowMap: Map[String, String])=>{
130
+      .map((rowMap: Map[String, String]) => {
125
         tvSerie = buildTvSerieModel(rowMap)
131
         tvSerie = buildTvSerieModel(rowMap)
126
         rowMap.get("tconst")
132
         rowMap.get("tconst")
127
       })
133
       })
128
       .runWith(Sink.head)
134
       .runWith(Sink.head)
129
 
135
 
130
-   Await.result(res,1 minutes)
131
-    val tconst:String = res.value.get.get.get
136
+    Await.result(res, 1 minutes)
137
+    val tconst: String = res.value.get.get.get
138
+    logger.info(s"TvSerie ID: $tconst, $tvSerie")
132
     tconst
139
     tconst
133
   }
140
   }
134
 
141
 
142
+  def getListOfPersonsIDByTvSerieID(tconst: String): List[Option[String]]={
143
+    val source: Source[Map[String, String], _] = buildSource(inputFile = titlePrincipalsBasics)
144
+    var tvSeries: Iterable[TvSeries]=null
145
+
146
+    val startTime : Long = System.currentTimeMillis()
147
+    val res: Future[IndexedSeq[Option[String]]] = source
148
+      .filter((rowMaps: Map[String, String])=>{
149
+        rowMaps.getOrElse(key = "tconst", default = "")==tconst
150
+      })
151
+      .map((rowMap: Map[String, String])=>{
152
+        rowMap.get(key = "nconst")
153
+      })
154
+      .withAttributes(supervisionStrategy(resumingDecider))
155
+      .runWith(Sink.collection)
156
+      .andThen {
157
+        case Success(value) =>
158
+          logger.info(s"$value")
159
+          val elapsedTime : Long = (System.currentTimeMillis() - startTime)/1000
160
+          logger.info(s"success, elapsed time: $elapsedTime")
161
+
162
+        case Failure(error: Error) => logger.error(s"$error")
163
+      }
164
+
165
+    Await.result(res,5 minutes)
166
+    val listPersonsID: List[Option[String]] = res.value.get.get.toList
167
+
168
+    listPersonsID
169
+
170
+  }
171
+
135
 
172
 
136
 }
173
 }
137
 
174
 

Powered by TurnKey Linux.