Javatpoint Logo
Javatpoint Logo

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.
Spark sortByKey Function
  • Create an RDD using the parallelized collection.

Now, we can read the generated result by using the following command.

Spark sortByKey Function

For ascending,

  • Apply sortByKey() function to ignore duplicate elements.
  • Now, we can read the generated result by using the following command.
Spark sortByKey Function

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.
Spark sortByKey Function

Here, we got the desired output.







Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA