简体   繁体   中英

How to sort spark dataframe on the combination of columns in Java?

I have a spark data frame in Java, something like below:

在此处输入图片说明

I want it to be sorted based on "Col3" but all the values of Col1 and Col2 should be in a group. The result should be something like below:

在此处输入图片说明

The groupBy() function is used during aggregation while your requirement just requires orderBy()

Assuming dataframe df with 3 columns Col1, Col2, Col3, you can do the below in Spark

val sortedDf = df.orderBy(col("Col1").desc,col("Col2").desc,col("Col3").asc)

POC for the same is available here SQLFIDDLE

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM