I use the Spark shell (1.3.1), which is the Scala shell. A simplified situation requiring iteration on Rowlooks something like this:
import org.apache.commons.lang.StringEscapeUtils
var result = sqlContext.sql("....")
var rows = result.collect()
var row = rows(0)
var line = row.map(cell => StringEscapeUtils.escapeCsv(cell)).mkString(",")
println(line)
My problem is that it Rowdoes not mapand - as far as I know - it cannot be converted to Arrayor List, therefore, I cannot avoid every cell using this style. I could write a loop using an index variable, but that would be inconvenient. I would like to iterate over cells in a situation like this:
result.collect().map(row => row.map(cell => StringEscapeUtils.escapeCsv(cell)).mkString(",")).mkString("\n")
(As a rule, these are not very large results; they can enter the client memory many times.)
Is there a way to iterate in cells Row? Is there any syntax to place an index based loop in place row.map(...)in the last snippet?