88
Spark sortByKey Function
In Spark, the sortByKey function maintains the order of elements. It receives key-value pairs (K, V) as an input, sorts the elements in ascending or descending order and generates a dataset in an order.
Example of sortByKey Function
In this example, we arrange the elements of dataset in ascending and descending order.
- To open the Spark in Scala mode, follow the below command.
- Create an RDD using the parallelized collection.
Now, we can read the generated result by using the following command.
For ascending,
- Apply sortByKey() function to ignore duplicate elements.
- Now, we can read the generated result by using the following command.
Here, we got the desired output.
For descending,
- Apply sortByKey() function and pass Boolean type as parameter.
- Now, we can read the generated result by using the following command.
Here, we got the desired output.
Next TopicSpark groupByKey Function