开发者社区> 问答> 正文

Apache Spark 2.0:Expression-string到orderBy()/ sort()列的降序

from pyspark.sql import functions as sFn
# Note: I import Spark functions this way to avoid name collisions w/ Python.
# Usage below: sFn.expr(), sFn.col(), etc.

col0 = [0, 1, 2, 3]
col1 = [4, 5, 6, 7]

myDF = spark.createDataFrame(zip(col0, col1),

                             schema=['col0', 'col1'])

print(myDF)
myDF.show()
myDF.orderBy(sFn.expr('col0 desc')).show() # <--- Problem line. Doesn't descend.
现在这本书的例子声称最后一个语句会以col0递减的方式排序,但它没有:

DataFrame[col0: bigint, col1: bigint]

col0 col1
0 4
1 5
2 6
3 7
col0 col1
0 4
1 5
2 6
3 7

但是,这种语法变体一直对我有用:

myDF.orderBy(sFn.col("col0").desc()).show()
问题变异是否超过拼写错误或勘误表?如果它是拼写错误或勘误表,那么需要进行哪些调整才能使其正常工作?

展开
收起
社区小助手 2018-12-21 11:52:43 2405 0
1 条回答
写回答
取消 提交回答
  • 社区小助手是spark中国社区的管理员,我会定期更新直播回顾等资料和文章干货,还整合了大家在钉群提出的有关spark的问题及回答。

    In sFn.expr('col0 desc'),desc被翻译为别名而不是a order by modifier,正如您在控制台中输入的那样:

    sFn.expr('col0 desc')

    Columndesc>

    以下是您可以根据需要选择的其他几个选项:

    myDF.orderBy('col0', ascending=0).show()
    col0 col1
    3 7
    2 6
    1 5
    0 4
    myDF.orderBy(sFn.desc('col0')).show()
    col0 col1
    3 7
    2 6
    1 5
    0 4
    myDF.orderBy(myDF.col0.desc()).show()
    col0 col1
    3 7
    2 6
    1 5
    0 4
    2019-07-17 23:23:21
    赞同 展开评论 打赏
问答排行榜
最热
最新

相关电子书

更多
Hybrid Cloud and Apache Spark 立即下载
Scalable Deep Learning on Spark 立即下载
Comparison of Spark SQL with Hive 立即下载

相关实验场景

更多

相关镜像