Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Flattening a Scala Map in an RDD

I have an an RDD that looks like:

uidProcessedKeywords: org.apache.spark.rdd.RDD[(Long, Map[String,Double])]

How do I flatten the map in the RDD to get this:

org.apache.spark.rdd.RDD[(Long, String, Double)]
like image 798
Delip Avatar asked Dec 01 '25 06:12

Delip


1 Answers

val x = sc.parallelize(List((2, Map("a" -> 0.2, "b" -> 0.3))))
x.flatMap { 
    case (id, m) => m.map { case (k, v) => (id, k, v)}
  }
 .collect()
res1: Array[(Int, String, Double)] = Array((2,a,0.2), (2,b,0.3))
like image 195
Nikita Avatar answered Dec 03 '25 21:12

Nikita



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!