Scala - How to get an object from an RDD after filtering

1626 views scala

I have filtered an RDD of Sale objects to get a single Sale by id. But how do I get a Sale object? I've tried mapping to Sale but it also returns an array. As well as .take(1)

As far as I understand map transformation just returns another RDD and it is expected behavior. Since RDD is an immutable data structure.

val sales: RDD[Sale] = oldSales.filter(sale => sale.ID == "123-0567").map(s => s)

val filteredSale: Sale = ??

answered question

1 Answer


Try this

val filteredSale = sales.collect.head.getAs[Sale](0)

posted this

Have an answer?


Please login first before posting an answer.