阶火花阵列映射(scala-spark Array mapping)

2019-10-23 00:49发布

I have a question about mapping Array in scala. I have the following Array:

Array[(scala.collection.immutable.Set[String], com.trends.City, com.trends.State)]

Basically, I want to map the Array such that each String in the Set will have com.trends.City and State attached to it. The result should look something like:

Array[(String, com.trends.City, com.trends.State)] 

Which is like flatMap, but I want the com.trends to be in there.

I could also convert the Array into a RDD if needed and use flatMapValues, but I am concerned for the efficiency, could someone tell me what is the best way to go?

Answer 1:

您可以斯卡拉阵列像这样上使用flatMap:

class City
class State
val array: Array[(scala.collection.immutable.Set[String], City, State)] = Array()
array.flatMap(p => p._1.map(q => (q, p._2, p._3)))


文章来源: scala-spark Array mapping