January 2019
Beginner to intermediate
154 pages
4h 31m
English
If you want to get N element based on an ordering, you can use a takeOrdered() action. You can also make use of sortBy() transformation, followed by a take() action. Both approaches trigger a data shuffle. In the following example, we take out 3 elements from the RDD, containing numbers from 0 to 9, by providing our own sorting criteria:
#Pythonspark.sparkContext.parallelize(range(10)).takeOrdered(3, key = lambda x: -x)Out[3]: [9, 8, 7]
Here, we took the first 3 elements in decreasing order.
Read now
Unlock full access