Sreenath Chothar
Sreenath Chothar

Reputation: 173

Find first element in RDD satisfying the given predicate

How to find the first element in a normal RDD( Because in PairRDD, we can use lookup(key) API ) which satisfy a predicate? And after finding the first element, it should exit the RDD traversal. Looking for a solution without using legacy for loops.

Upvotes: 2

Views: 10352

Answers (2)

Jai Prakash
Jai Prakash

Reputation: 2799

The above solutions stated are perfectly correct. Here is another method to achieve the same goal

rdd.filter(p).first

Upvotes: 1

Harald Gliebe
Harald Gliebe

Reputation: 7564

How about

rdd.filter(p).top(1)

or if you don't have an order on the RDD

rdd.filter(p).take(1)

Upvotes: 7

Related Questions