如何在Spark SQL中按列降序排序? [英] How to sort by column in descending order in Spark SQL?

查看:34
本文介绍了如何在Spark SQL中按列降序排序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试了 df.orderBy("col1").show(10) 但它按升序排序.df.sort("col1").show(10) 也按升序排序.我查看了 stackoverflow,发现的答案都已过时或 提到 RDD.我想在 spark 中使用本机数据框.

I tried df.orderBy("col1").show(10) but it sorted in ascending order. df.sort("col1").show(10) also sorts in ascending order. I looked on stackoverflow and the answers I found were all outdated or referred to RDDs. I'd like to use the native dataframe in spark.

推荐答案

它在 org.apache.spark.sql.DataFrame 用于 sort 方法:

df.sort($"col1", $"col2".desc)

注意 $.desc 位于 sort 中的列,以便对结果进行排序.

Note $ and .desc inside sort for the column to sort the results by.

这篇关于如何在Spark SQL中按列降序排序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆