Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Converting various Seqs to CSV?

Tags:

csv

scala

We have a number of Scala classes returning Map[String,String] (key,value) results for storage in a NoSQL database. Some of the results are actually Map[String, List] or Map[String, ArrayBuffer], so we are using .toString on those objects to convert. This gives us output that looks like:

"ArrayBuffer(1,2,3,4)"

or

"List(1,2,4)"

Rather than include the object type, we'd like to have these written out as straight CSV, quotes and escaped characters as necessary. Is there a good CSV library that works well with Scala?

like image 900
Joshua Avatar asked Sep 18 '25 00:09

Joshua


1 Answers

If you just want to serialize a single list or array without correctly escaping quotes etc:

scala> List(1,2,3,4).mkString(",")
res39: String = 1,2,3,4

If you are looking to serialize slightly more complex data structures: product-collections will serialize a collection of tuples or case classes (any Product) to csv and correctly escape the quotes.

scala> List ((1,"Jan"),
     | (2,"Feb"),
     | (3,"Mar","Extra column")).csvIterator.mkString("\n")
res41: String =
1,"Jan"
2,"Feb"
3,"Mar","Extra column"

product-collections will also write directly to a java.io.Writer. It has a collection CollSeq specialized on homogenous tuples that would not allow the "extra column" above.

And to convert your original data into a format that can be handled by product-collections:

scala> CollSeq(scala.collection.mutable.ArrayBuffer("a","b","quoted \"stuff\""):_*)
res52: com.github.marklister.collections.immutable.CollSeq1[String] =
CollSeq((a),
        (b),
        (quoted "stuff"))

scala> res52.csvIterator.mkString("\n")
res53: String =
"a"
"b"
"quoted ""stuff"""
like image 131
Mark Lister Avatar answered Sep 19 '25 19:09

Mark Lister