Antoine DORIAN 2 years ago
parent
commit
43163ba5f7

+ 5
- 0
pom.xml View File

@@ -88,6 +88,11 @@
88 88
             <groupId>com.typesafe.akka</groupId>
89 89
             <artifactId>akka-stream_${scala.binary.version}</artifactId>
90 90
         </dependency>
91
+        <dependency>
92
+            <groupId>com.lightbend.akka</groupId>
93
+            <artifactId>akka-stream-alpakka-csv_${scala.binary.version}</artifactId>
94
+            <version>4.0.0</version>
95
+        </dependency>
91 96
 
92 97
         <!-- alpakka -->
93 98
         <dependency>

+ 17
- 0
src/main/resources/test.tsv View File

@@ -0,0 +1,17 @@
1
+tconst	titleType	primaryTitle	originalTitle	isAdult	startYear	endYear	runtimeMinutes	genres
2
+tt0000001	short	Carmencita	Carmencita	0	1894	\N	1	Documentary,Short
3
+tt0000002	short	Le clown et ses chiens	Le clown et ses chiens	0	1892	\N	5	Animation,Short
4
+tt0000003	short	Pauvre Pierrot	Pauvre Pierrot	0	1892	\N	4	Animation,Comedy,Romance
5
+tt0000004	short	Un bon bock	Un bon bock	0	1892	\N	12	Animation,Short
6
+tt0000005	short	Blacksmith Scene	Blacksmith Scene	0	1893	\N	1	Comedy,Short
7
+tt0000006	short	Chinese Opium Den	Chinese Opium Den	0	1894	\N	1	Short
8
+tt0000007	short	Corbett and Courtney Before the Kinetograph	Corbett and Courtney Before the Kinetograph	0	1894	\N	1	Short,Sport
9
+tt0000008	short	Edison Kinetoscopic Record of a Sneeze	Edison Kinetoscopic Record of a Sneeze	0	1894	\N	1	Documentary,Short
10
+tt0000009	movie	Miss Jerry	Miss Jerry	0	1894	\N	45	Romance
11
+tt0000010	short	Leaving the Factory	La sortie de l'usine Lumi?¿re ?á Lyon	0	1895	\N	1	Documentary,Short
12
+tt0000011	short	Akrobatisches Potpourri	Akrobatisches Potpourri	0	1895	\N	1	Documentary,Short
13
+tt0000012	short	The Arrival of a Train	L'arriv?®e d'un train ?á La Ciotat	0	1896	\N	1	Documentary,Short
14
+tt0000013	short	The Photographical Congress Arrives in Lyon	Le d?®barquement du congr?¿s de photographie ?á Lyon	0	1895	\N	1	Documentary,Short
15
+tt0000014	short	The Waterer Watered	L'arroseur arros?®	0	1895	\N	1	Comedy,Short
16
+tt0000015	short	Autour d'une cabine	Autour d'une cabine	0	1894	\N	2	Animation,Short
17
+tt0000016	short	Boat Leaving the Port	Barque sortant du port	0	1895	\N	1	Documentary,Short

+ 2
- 0
src/main/resources/test2.tsv View File

@@ -0,0 +1,2 @@
1
+tconst	titleType	name	originalTitle	isAdult	startYear	endYear	runtimeMinutes	ge
2
+tt0000015	short	Antoine	Autour d'une cabine	0	1900	\N	2	Animation,Short

+ 79
- 0
src/main/scala/com/mediahub/FileReader.scala View File

@@ -0,0 +1,79 @@
1
+package com.mediahub
2
+
3
+import akka.{Done, NotUsed}
4
+import org.springframework.stereotype.Component
5
+
6
+import java.io.File
7
+import akka.actor.ActorSystem
8
+import akka.stream.alpakka.csv.scaladsl.{CsvParsing, CsvToMap}
9
+import akka.stream.javadsl.FileIO
10
+import akka.stream.scaladsl.{Compression, Flow, Framing, Keep, Sink, Source}
11
+import akka.util.ByteString
12
+
13
+import java.nio.file.Paths
14
+import java.util.stream.Collectors
15
+import scala.concurrent.ExecutionContext.Implicits.global
16
+import scala.concurrent.{ExecutionContext, Future}
17
+
18
+
19
+@Component
20
+class FileReader {
21
+
22
+  implicit val system: ActorSystem = ActorSystem()
23
+
24
+  val list: List[File] = getListOfFiles
25
+  val source: Source[File, NotUsed] = Source(list)
26
+
27
+  val result = source
28
+    .flatMapConcat(f => FileIO.fromPath(Paths.get(f.getPath), 1 * 1024 * 1024))
29
+    .via(CsvParsing.lineScanner(CsvParsing.Tab))
30
+    .via(CsvToMap.toMapAsStringsCombineAll(headerPlaceholder = Option.empty))
31
+    .filter(row => row.getOrElse("primaryTitle", "") == "Autour d'une cabine")
32
+    .map(a => a.get("tconst"))
33
+    .toMat(Sink.foreach(println))(Keep.both)
34
+   // .runForeach(h => println("source 1 ", h))
35
+
36
+  //.runWith(Sink.seq)
37
+
38
+  source
39
+    .flatMapConcat(f => FileIO.fromPath(Paths.get(f.getPath), 1 * 1024 * 1024))
40
+    .via(CsvParsing.lineScanner(CsvParsing.Tab))
41
+    .via(CsvToMap.toMapAsStringsCombineAll(headerPlaceholder = Option.empty))
42
+    .runForeach(h => println("source 2 ", h))
43
+   /* .filter(row => row.getOrElse("tconst", "name") == (result.value, "Antoine"))
44
+    .runForeach(println)*/
45
+
46
+
47
+
48
+
49
+  // readAsync()
50
+
51
+
52
+  /* def readAsync(): Source = {
53
+     Source(list)
54
+       .mapAsync(4) { value =>
55
+         implicit val ec: ExecutionContext = ActorSystem().dispatcher
56
+         Future {
57
+           source
58
+             .flatMapConcat(f => FileIO.fromPath(Paths.get(f.getPath), 1 * 1024 * 1024))
59
+             .via(CsvParsing.lineScanner(CsvParsing.Tab))
60
+             .via(CsvToMap.toMapAsStringsCombineAll(headerPlaceholder = Option.empty))
61
+             .filter(row => row.getOrElse("primaryTitle", "") == "Autour d'une cabine")
62
+             .runForeach(println)
63
+           println(s"Process in Thread:${Thread.currentThread().getName}")
64
+           value
65
+         }
66
+       }
67
+       .runWith(Sink.foreach(value => println(s"Item emitted:$value in Thread:${Thread.currentThread().getName}")))
68
+   }*/
69
+
70
+  def getListOfFiles: List[File] = {
71
+    val d = new File("src/main/resources")
72
+    if (d.exists && d.isDirectory) {
73
+      d.listFiles.filter(_.isFile).toList
74
+    } else {
75
+      List[File]()
76
+    }
77
+  }
78
+
79
+}

+ 0
- 17
src/main/scala/com/mediahub/HelloWorld.scala View File

@@ -1,17 +0,0 @@
1
-package com.mediahub
2
-
3
-import org.springframework.stereotype.Component
4
-
5
-@Component
6
-class HelloWorld {
7
-  println("Hello from default constructor")
8
-  greet()
9
-
10
-
11
-
12
-  def greet() = {
13
-    val greeting = "Hello from SCALA!!!!"
14
-    println(greeting)
15
-  }
16
-
17
-}

+ 27
- 0
src/main/scala/com/mediahub/MovieQueryService.scala View File

@@ -0,0 +1,27 @@
1
+package com.mediahub
2
+
3
+import akka.stream.scaladsl.Source
4
+
5
+class MovieQueryService {
6
+
7
+  final case class Principal(
8
+                              name: String,
9
+                              birthYear: Int,
10
+                              deathYear: Option[Int],
11
+                              profession: List[String])
12
+
13
+  final case class TvSeries(
14
+                             original: String,
15
+                             startYear: Int,
16
+                             endYear: Option[Int],
17
+                             genres: List[String])
18
+
19
+  trait MovieService {
20
+    def principalsForMovieName(name: String): Source[Principal, _]
21
+
22
+    def tvSeriesWithGreatestNumberOfEpisodes(): Source[TvSeries, _]
23
+  }
24
+
25
+}
26
+
27
+

Powered by TurnKey Linux.