Using Scala to Cut a Large CSV File

What is the best way to make an io file in Scala 2.8?

All I want to do is cut a massive CSV file into many small ones, with 1000 lines of data per file and each file that saves a header.

+3
source share
2 answers

For such simple tasks I would use scala.io.Source. An example would look like this:

val input = io.Source.fromFile("input.csv").getLines()

if (input.hasNext) {
  // assuming one header line
  val header = List(input.next())

  for ((i, lines) <- Iterator.from(1) zip input.grouped(linesPerFile)) {
    val out = createWriter(i) // Create a file for index i
    (header.iterator ++ lines.iterator).foreach(out.println)
    out.close
  }
}
+12
source

, CSV . CSV-, , : , , .

CSV, . kantan.csv ( ), , product-collections opencsv.

0

Source: https://habr.com/ru/post/1762803/


All Articles