0

I am using Spark SQL for extracting some information from a JSON file. The question is I want to save the result from the SQL analysis into a CSV file for plotting it with Plateau or with d3.js and I'm not able to add a row which contains the "identifiers" of my columns. For example, if I execute the code which is below, I obtain something like this:

val languages = sqlContext.sql(""""<QUERY>"""")
val result = idiomas.map(row => row(0) + "," + row(1))
result.saveAsTextFile(outputDirectory + "/lang")
result.collect.foreach(println)

A,395
B,275
C,106
D,60

And what I want is to add an identifier row and obtain this:

letter,number
A,395
B,275
C,106
D,60

How can I do it? Thanks!

Lars Kotthoff
  • 107,425
  • 16
  • 204
  • 204
lds
  • 41
  • 1
  • 4
  • Just `println("letter, number")` before the `result.collect` line? Or are you looking for something more complex? – The Archetypal Paul Oct 20 '15 at 16:23
  • I'd take a look at https://github.com/databricks/spark-csv – Reactormonk Oct 20 '15 at 16:26
  • The important thing for my is saving it, not printing it – lds Oct 20 '15 at 16:27
  • I don't understand very well how to use https://github.com/databricks/spark-csv so I need another explanation of how to use it – lds Oct 20 '15 at 16:28
  • Possible duplicate of [How to write to a file in Scala?](http://stackoverflow.com/questions/4604237/how-to-write-to-a-file-in-scala) – childofsoong Oct 20 '15 at 17:52
  • See http://stackoverflow.com/questions/4604237/how-to-write-to-a-file-in-scala for how to write to a file in Scala. Once you get that part down, it's just a question of writing your data and properly interspersing comma and newline literals (if you're not aware, a newline literal is written as `"\n"`, exactly the same as Java) – childofsoong Oct 20 '15 at 17:54

0 Answers0