是否存在一个过滤器函数,当它找到与谓词对应的第 n 个第一个元素时停止



我问这个问题是因为我必须在RDD[key:Int,Array(Double)]上找到一个特定的元素,其中键是唯一的。因此,在整个RDD上使用过滤器的成本很高,而我只需要一个知道键的元素。

val wantedkey = 94
val res = rdd.filter( x => x._1 == wantedkey )

谢谢你的建议

在 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)

所有转换都是延迟的,仅当您对它们调用操作时才会计算它们。所以你可以写:

val wantedkey = 94
val res = rdd.filter( x => x._1 == wantedkey ).first()

最新更新