【发布时间】:2018-03-17 16:04:52
【问题描述】:
我有非常大的制表符分隔文件 (10GB-70GB),需要进行一些读取、数据操作和写入单独的文件。这些文件的范围可以从 100 到 10K 列,包含 200 万到 500 万行。
前 x 列是静态的,需要参考。示例文件格式:
#ProductName Brand Customer1 Customer2 Customer3
Corolla Toyota Y N Y
Accord Honda Y Y N
Civic Honda 0 1 1
我需要使用前 2 列来获取产品 ID,然后生成类似于以下内容的输出文件:
ProductID1 Customer1 Y
ProductID1 Customer2 N
ProductID1 Customer3 Y
ProductID2 Customer1 Y
ProductID2 Customer2 Y
ProductID2 Customer3 N
ProductID3 Customer1 N
ProductID3 Customer2 Y
ProductID3 Customer3 Y
当前示例代码:
val fileNameAbsPath = filePath + fileName
val outputFile = new PrintWriter(filePath+outputFileName)
var customerList = Array[String]()
for(line <- scala.io.Source.fromFile(fileNameAbsPath).getLines()) {
if(line.startsWith("#")) {
customerList = line.split("\t")
}
else {
val cols = line.split("\t")
val productid = getProductID(cols(0), cols(1))
for (i <- (2 until cols.length)) {
val rowOutput = productid + "\t" + customerList(i) + "\t" + parser(cols(i))
outputFile.println(rowOutput)
outputFile.flush()
}
}
}
outputFile.close()
我运行的一个测试花了大约 12 个小时来读取一个包含 300 万行和 2500 列的文件 (70GB)。最终的输出文件生成了 250GB,大约有 800+ 百万行。
我的问题是:除了我已经在做的事情之外,Scala 中还有什么可以提供更快的性能吗?
【问题讨论】:
-
这看起来很像一个作业,不适合作为一个问题
-
如果我误解了,我很抱歉,但我只是在寻找想法,而不是有人帮助我编码。我对 scala 相当陌生,想知道它是否会提供更好的性能。
-
我想将处理标题行的
if子句移出for循环。如果您知道标题行只会出现一次,则无需对每一行执行检查。其次,除非你真的想确保你不想错过任何写入,flush每次写入都会降低性能,我会先使用BufferedWriter和@987654328 @ 并让他们负责刷新脏位。