在 Spark SQL 中更改空值排序 [英] Changing Nulls Ordering in Spark SQL

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

问题描述

我需要能够按升序和降序对列进行排序,并允许空值排在第一位或空值排在最后.使用 RDD,我可以将 sortByKey 方法与自定义比较器一起使用.我想知道是否有使用 Dataset API 的相应方法.我看到了如何将 desc/asc 添加到列,但我对空值排序一无所知.

I need to be able to sort columns in ascending and descending order and also allow nulls to be first or nulls to be last. Using RDDs I could use the sortByKey method with a custom comparator. I was wondering if there is a corresponding approach using the Dataset API. I see how to to add desc/asc to columns but I have no clue on the nulls ordering.

推荐答案

您也可以使用数据集 API 来做到:

You can also do it with the dataset API:

scala>     val df = Seq("a", "b", null).toDF("x")
df: org.apache.spark.sql.DataFrame = [x: string]

scala> df.select('*).orderBy('x.asc_nulls_last).show
+----+
|   x|
+----+
|   a|
|   b|
|null|
+----+


scala> df.select('*).orderBy('x.asc_nulls_first).show
+----+
|   x|
+----+
|null|
|   a|
|   b|
+----+

同样的事情适用于 desc_nulls_lastdesc_nulls_first.

Same thing works with desc_nulls_last and desc_nulls_first.

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

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