我问这个问题是因为我必须在RDD [key:Int,Array(Double)]中找到一个特定元素,其中键是唯一的。因此,在整个RDD上使用过滤器会很昂贵,而我只需要一个知道密钥的元素。
val wantedkey = 94
val res = rdd.filter( x => x._1 == wantedkey )
感谢您的建议
答案 0 :(得分:1)
所有transformations都是懒惰的,只有在您调用action时才会计算它们。所以你可以写:
val wantedkey = 94
val res = rdd.filter( x => x._1 == wantedkey ).first()
答案 1 :(得分:1)
查看PairRDDFunctions.scala上的查找功能。
def lookup(key: K): Seq[V]
Return the list of values in the RDD for key key. This operation is
done efficiently if the RDD has a known partitioner by only searching
the partition that the key maps to.
实施例
val a = sc.parallelize(List("dog", "tiger", "lion", "cat", "panther", "eagle"), 2)
val b = a.keyBy(x => (_.length)
b.lookup(5)
res0: Seq[String] = WrappedArray(tiger, eagle)