Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spark select top values in RDD

The original dataset is:

# (numbersofrating,title,avg_rating)
newRDD =[(3,'monster',4),(4,'minions 3D',5),....] 

I want to select top N avg_ratings in newRDD.I use the following code,it has an error.

selectnewRDD = (newRDD.map(x, key =lambda x: x[2]).sortBy(......))

TypeError: map() takes no keyword arguments

The expected data should be:

# (numbersofrating,title,avg_rating)
selectnewRDD =[(4,'minions 3D',5),(3,'monster',4)....] 
like image 617
user3849475 Avatar asked Aug 07 '15 16:08

user3849475


People also ask

What is Take () in PySpark?

take (num: int) → List[T][source] Take the first num elements of the RDD. It works by first scanning one partition, and use the results from that partition to estimate the number of additional partitions needed to satisfy the limit.

How do you get the latest PySpark record?

Use tail() action to get the Last N rows from a DataFrame, this returns a list of class Row for PySpark and Array[Row] for Spark with Scala.

How do you use PySpark reduce?

reduce() is similar to fold() except reduce takes a 'Zero value' as an initial value for each partition. reduce() is similar to aggregate() with a difference; reduce return type should be the same as this RDD element type whereas aggregation can return any type.


2 Answers

You can use either top or takeOrdered with key argument:

newRDD.top(2, key=lambda x: x[2])

or

newRDD.takeOrdered(2, key=lambda x: -x[2])

Note that top is taking elements in descending order and takeOrdered in ascending so key function is different in both cases.

like image 149
zero323 Avatar answered Nov 15 '22 23:11

zero323


Have you tried using top? Given that you want the top avg ratings (and it is the third item in the tuple), you'll need to assign it to the key using a lambda function.

# items = (number_of_ratings, title, avg_rating)
newRDD = sc.parallelize([(3, 'monster', 4), (4, 'minions 3D', 5)])
top_n = 10
>>> newRDD.top(top_n, key=lambda items: items[2])
[(4, 'minions 3D', 5), (3, 'monster', 4)]
like image 40
Alexander Avatar answered Nov 15 '22 22:11

Alexander