Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does Spark supports melt and dcast [duplicate]

We use melt and dcast to convert data from wide->long and long->wide format. Refer http://seananderson.ca/2013/10/19/reshape.html for more details.

Either scala or SparkR is fine.

I've gone through this blog and scala functions and R API. I don't see functions which does similar job.

Is there any equivalent function in Spark? If not, is there any other way to do it in Spark?

like image 626
sag Avatar asked Dec 25 '22 07:12

sag


1 Answers

Reshaping Data with Pivot in Spark gives support for reshaping with pivot. I understood melt is roughly the reverse of pivot also called unpivot. I'm relatively new to Spark. With my knowledge i tried to implement melt operation.

    def melt(df: DataFrame, columns: List[String]): DataFrame ={

    val restOfTheColumns =  df.columns.filterNot(columns.contains(_))
    val baseDF = df.select(columns.head, columns.tail: _*)
    val newStructure =StructType(baseDF.schema.fields ++ List(StructField("variable", StringType, true), StructField("value", StringType, true)))
    var newdf  = sqlContext.createDataFrame(sqlContext.sparkContext.emptyRDD[Row], newStructure)

    for(variableCol <- restOfTheColumns){
      val colValues = df.select(variableCol).map(r=> r(0).toString)
      val colRdd=baseDF.rdd.zip(colValues).map(tuple => Row.fromSeq(tuple._1.toSeq.:+(variableCol).:+(tuple._2.toString)))
      var colDF =sqlContext.createDataFrame(colRdd, newStructure)
      newdf =newdf.unionAll(colDF)
    }
    newdf
  }

It does the work. But i am not very sure about the efficiency.

+-----+---+---+----------+------+
| name|sex|age|    street|weight|
+-----+---+---+----------+------+
|Alice|  f| 34| somewhere|    70|
|  Bob|  m| 63|   nowhere|   -70|
|Alice|  f|612|nextstreet|    23|
|  Bob|  m|612|      moon|     8|
+-----+---+---+----------+------+

Can be used as

melt(df, List("name", "sex"))

The result is as below:

+-----+---+--------+----------+
| name|sex|variable|     value|
+-----+---+--------+----------+
|Alice|  f|     age|        34|
|  Bob|  m|     age|        63|
|Alice|  f|     age|       612|
|  Bob|  m|     age|       612|
|Alice|  f|  street| somewhere|
|  Bob|  m|  street|   nowhere|
|Alice|  f|  street|nextstreet|
|  Bob|  m|  street|      moon|
|Alice|  f|  weight|        70|
|  Bob|  m|  weight|       -70|
|Alice|  f|  weight|        23|
|  Bob|  m|  weight|         8|
+-----+---+--------+----------+

I hope it is useful and appreciate your comments if there is room for improvements.

like image 152
NehaM Avatar answered Jan 05 '23 12:01

NehaM