4

I'm using Spark shell (1.3.1) which is a Scala shell. The simplified situation that needs iteration on Row is something like this:

import org.apache.commons.lang.StringEscapeUtils

var result = sqlContext.sql("....")
var rows = result.collect() // Array[org.apache.spark.sql.Row]
var row = rows(0) // org.apache.spark.sql.Row
var line = row.map(cell => StringEscapeUtils.escapeCsv(cell)).mkString(",")
// error: value map is not a member of org.apache.spark.sql.Row
println(line)

My problem is that Row does not have map and - as far as I know - it cannot be converted to Array or List, so I cannot escape each cell using this style. I could write a loop using an index variable but it would be inconvenient. I would like to iterate on the cells in a situation like this:

result.collect().map(row => row.map(cell => StringEscapeUtils.escapeCsv(cell)).mkString(",")).mkString("\n")

(These are typically not large results, they can fit into the client memory many times.)

Is there any way to iterate on the cells of a Row? Is there any syntax for putting an index based loop at the place of row.map(...) in the last snippet?

1 Answer 1

1

You can use toSeq() on Row which has map. toSeq will be in the same order as the rows

Sign up to request clarification or add additional context in comments.

3 Comments

I always doubt collect and then iterate again on it, so if you do not mind can you explain why you can not distribute your operation (ie need to bring everything in single m/c)? TO answer your question, you can map on the RDD and convert it back from Row to tuple and then collect.
Unspecified value parameter idx
@ayanguha It does not matter. The problem is the same, we want to iterate on each row. Remotely or locally: it does not matter. The result set - as I said - is small.

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.