|
@@ -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
|
+}
|