0

I want to convert an array created like:

case class Student(name: String, age: Int)
val dataFrame: DataFrame = sql.createDataFrame(sql.sparkContext.parallelize(List(Student("Torcuato", 27), Student("Rosalinda", 34))))

When I collect the results from the DataFrame, the resulting array is an Array[org.apache.spark.sql.Row] = Array([Torcuato,27], [Rosalinda,34])

I'm looking into converting the DataFrame in an RDD[Map] e.g:

Map("name" -> nameOFFirst, "age" -> ageOfFirst)
Map("name" -> nameOFsecond, "age" -> ageOfsecond)

I tried to use map via: x._1 but that does not seem to work for Array [spark.sql.row] How can I anyway perform the transformation?

3
  • The outer map doesn't have a key- value structure? Commented Apr 14, 2016 at 9:16
  • The context is I want to use spark-jobserver but have some problems regarding serialization of job results. Apparently only a map of string key / values works. The result returned will be an aggregation of several spark queries. So the outer map would kind-of contain further keys. groups.google.com/forum/#!topic/spark-jobserver/V4finry_RoM Commented Apr 14, 2016 at 9:19
  • 2
    This is a very bad question, with misleading title, bad practice, low quality description. You'll need to work on these stuff when you post questions here Commented Apr 14, 2016 at 9:44

2 Answers 2

6

You can use map function with pattern matching to do the job here

import org.apache.spark.sql.Row

dataFrame
  .map { case Row(name, age) => Map("name" -> name, "age" -> age) }

This will result in RDD[Map[String, Any]]

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

1 Comment

not working for me I am getting scala.Any error --> Exception in thread "main" java.lang.ClassNotFoundException: scala.Any
0

In other words, you could transform row of dataframe to map, and below works!

def dfToMapOfRdd(df: DataFrame): RDD[Map[String, Any]] = {
    val result: RDD[Map[String, Any]] = df.rdd.map(row => {
        row.getValuesMap[Any](row.schema.fieldNames)
    })
    result
}

refs: https://stackoverflow.com/a/46156025/6494418

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.