在Spark SQL中更改Null顺序 [英] Changing Nulls Ordering in Spark SQL

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

问题描述

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

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_last desc_nulls_first 相同.

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

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