【发布时间】:2018-10-26 21:00:03
【问题描述】:
我有一个图表,它从多个 gzip 文件中读取行并将这些行写入另一组 gzip 文件,根据每行中的某个值进行映射。
它适用于小型数据集,但无法终止大型数据。 (这可能不是数据大小的问题,因为我没有运行足够的次数来确定——这需要一段时间)。
def files: Source[File, NotUsed] =
Source.fromIterator(
() =>
Files
.fileTraverser()
.breadthFirst(inDir)
.asScala
.filter(_.getName.endsWith(".gz"))
.toIterator)
def extract =
Flow[File]
.mapConcat[String](unzip)
.mapConcat(s =>
(JsonMethods.parse(s) \ "tk").extract[Array[String]].map(_ -> s).to[collection.immutable.Iterable])
.groupBy(1 << 16, _._1)
.groupedWithin(1000, 1.second)
.map { lines =>
val w = writer(lines.head._1)
w.println(lines.map(_._2).mkString("\n"))
w.close()
Done
}
.mergeSubstreams
def unzip(f: File) = {
scala.io.Source
.fromInputStream(new GZIPInputStream(new FileInputStream(f)))
.getLines
.toIterable
.to[collection.immutable.Iterable]
}
def writer(tk: String): PrintWriter =
new PrintWriter(
new OutputStreamWriter(
new GZIPOutputStream(
new FileOutputStream(new File(outDir, s"$tk.json.gz"), true)
))
)
val process = files.via(extract).toMat(Sink.ignore)(Keep.right).run()
Await.result(process, Duration.Inf)
线程转储显示进程是WAITINGAwait.result(process, Duration.Inf),没有其他任何事情发生。
OpenJDK v11 和 Akka v2.5.15
【问题讨论】:
标签: scala akka akka-stream