Skip to content

Instantly share code, notes, and snippets.

@mlehman
Created August 2, 2014 15:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mlehman/69dc9eaa1e254080833c to your computer and use it in GitHub Desktop.
Save mlehman/69dc9eaa1e254080833c to your computer and use it in GitHub Desktop.
Save Case Class as TSV On Spark
implicit class ProductRDD[T <: Product](rdd: RDD[T]) {
/* Saves a RDD of Tuples into a TSV.
* Ex: Employee(emp_id = 123, Name(first="Bob",last="Smith")) => "123\tBob\tSmith"
*/
def saveAsTsv(path: String) {
rdd.map(p => p.productIterator.flatMap {
case a: Product => a.productIterator //flattens nested case classes
case b => Seq(b)
}.mkString("\t"))
.saveAsTextFile(path)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment