Support Questions

Find answers, ask questions, and share your expertise
Announcements
Celebrating as our community reaches 100,000 members! Thank you!

Spark sort by key with descending order

avatar
Super Collaborator

rdd.sortByKey() sorts in ascending order.

I want to sort in descending order.

I tried rdd.sortByKey("desc") but it did not work

1 ACCEPTED SOLUTION

avatar

Try using rdd.sortByKey(false)

This will sort in descending order

View solution in original post

2 REPLIES 2

avatar

Try using rdd.sortByKey(false)

This will sort in descending order

avatar
Contributor

Try this code

from pyspark import SparkConf, SparkContext
from pyspark.sql import SQLContext
conf1 = SparkConf().setAppName('sort_desc')
sc1 = SparkContext(conf=conf1)
sql_context = SQLContext(sc1)
csv_file_path = 'emp.csv'
employee_rdd = sc1.textFile(csv_file_path).map(lambda line: line.split(','))
print(type(employee_rdd))
employee_rdd_sorted = employee_rdd.sortByKey(ascending= False)
employee_df = employee_rdd.toDF(['dept','ctc'])
employee_df_sorted = employee_rdd_sorted.toDF(['dept','ctc'])