我有RDD像(a,b,数组(p1,p2,p3),c),我想在数组里面的数值应用flatmap来得到下面的输出。 (A,B,P1,c)中 (A,B,P2,c)中 (A,B,P3,c)中
答案 0 :(得分:6)
代码:
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: (a1,b1,1)|(a1,b1,2)|(a1,b1,3)|(a1,b1,1)|(a1,b1,2)|(a1,b1,3)