1

I have a case class like that:

case class ResultDays (name: String, number: Double, values: Double*)

and I want to save it into a .csv file

resultRDD.toDF()
  .coalesce(1)
  .write.format("com.databricks.spark.csv")
  .option("header", "true")
  .save("res/output/result.csv")

Unfortunately I have this error:

java.lang.UnsupportedOperationException: CSV data source does not support array<double> data type.

So, how can I insert a variable number of values and save it into a .csv?

4
  • CSV, as a format, does not support a variable number of values, in the sense that all records must have the same columns. Do you know anything about the number of values expected? Perhaps the maximum number of values the values member might have? Commented Feb 13, 2017 at 10:28
  • I've to write the same number of values for every row, but I don't know how many values I have before run. Commented Feb 13, 2017 at 10:32
  • OK - but once you have resultRDD, you can assume all records have the same number of values? Commented Feb 13, 2017 at 11:00
  • Yes, but using ResultDays class it contains Double* and it seems I cannot use that Commented Feb 13, 2017 at 11:03

1 Answer 1

1

If you can assume all records in resultRDD have the same number of columns in values - you can read the first() record, use it to determine the number of values in the arrays, and convert these arrays into separate columns:

// determine number of "extra" columns:
val extraCols = resultRDD.first().values.size

// create a sequence of desired columns:
val columns = Seq($"name", $"number") ++ (1 to extraCols).map(i => $"values"(i - 1) as s"col$i")

// select the above columns before saving:
resultRDD.toDF()
  .select(columns: _*)
  .coalesce(1)
  .write.format("com.databricks.spark.csv")
  .option("header", "true")
  .save("res/output/result.csv")

Example CSV result would be something like:

name,number,col1,col2
a,0.1,0.01,0.001
b,0.2,0.02,0.002
c,0.3,0.03,0.003
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.