I have RDD like (a,b,Array(p1,p2,p3),c), I want to apply flatmap on Array inside the value to get the below output like. (a,b,p1,c) (a,b,p2,c) (a,b,p3,c)
Asked
Active
Viewed 2,693 times
0
-
1So, where are you stuck? – The Archetypal Paul Feb 10 '16 at 21:28
-
From java back ground, i'm very new to scala looking for some code snippet to apply flatmap on array which in the part of value.. – prakash Feb 10 '16 at 21:52
1 Answers
6
Code:
val arr = Array(("a1", "b1", Array("1", "2", "3")),("a1", "b1", Array("1", "2", "3")))
val rdd: RDD[(String, String, Array[String])] = sc.parallelize(arr)
val result = rdd.flatMap {
case (first, second, third) => {
third.map(x => (first, second, x))
}}
println("Results: " + result.collect.mkString("|"))
Results:
Results: (a1,b1,1)|(a1,b1,2)|(a1,b1,3)|(a1,b1,1)|(a1,b1,2)|(a1,b1,3)

Bryan
- 71
- 2