How do I read a large CSV file with Scala Stream class?

Jan Willem Tulp picture Jan Willem Tulp · Nov 23, 2010 · Viewed 26.5k times · Source

How do I read a large CSV file (> 1 Gb) with a Scala Stream? Do you have a code example? Or would you use a different way to read a large CSV file without loading it into memory first?

Answer

Kevin Wright picture Kevin Wright · Nov 23, 2010

Just use Source.fromFile(...).getLines as you already stated.

That returns an Iterator, which is already lazy (You'd use stream as a lazy collection where you wanted previously retrieved values to be memoized, so you can read them again)

If you're getting memory problems, then the problem will lie in what you're doing after getLines. Any operation like toList, which forces a strict collection, will cause the problem.