简体   繁体   English

Spark dataframe orderby using many columns in scala

[英]Spark dataframe orderby using many columns in scala

In Spark 1.6, Basically I would like to apply partition by and then do order by using two columns so that I can apply rank logic for each partition在 Spark 1.6 中,基本上我想应用分区,然后使用两列进行排序,以便我可以为每个分区应用排名逻辑

 val str = "insertdatetime,a_load_dt"
val orderByList = str.split(",")
val ptr = "memberidnum"
val partitionsColumnsList = ptr.split(",").toList


val  landingDF = hc.sql("""select memberidnum,insertdatetime,'2019-09-26' as a_load_dt from landing_omega.omegamaster""")
val  stagingDF = hc.sql("""select memberidnum,insertdatetime,a_load_dt from staging_omega.omegamaster where recordstatus ='current'""")
val unionedDF = landingDF.unionAll(stagingDF)
unionedDF.registerTempTable("temp_table")
val windowFunction = Window.partitionBy(partitionsColumnsList.map(elem => col(elem)):_*).orderBy(unionedDF(orderByList(0),orderByList(1)).desc)

But it throws the below error但它会引发以下错误

 scala> val windowFunction = Window.partitionBy(partitionsColumnsList.map(elem => col(elem)):_*).orderBy(unionedDF(orderByList(0),orderByList(1)).desc)
<console>:56: error: too many arguments for method apply: (colName: String)org.apache.spark.sql.Column in class DataFrame
     val windowFunction = Window.partitionBy(partitionsColumnsList.map(elem => col(elem)):_*).orderBy(unionedDF(orderByList(0),orderByList(1)).desc)

How do I fix this issue.我该如何解决这个问题。 I want to apply order by on two columns desc order我想在两列 desc order 上应用 order by

Please help ^请帮忙^

You can simply do the below change:您可以简单地进行以下更改:

val windowFunction = Window.partitionBy(partitionsColumnsList.head, partitionsColumnsList.tail:_*).orderBy(unionedDF(orderByList(0),orderByList(1)).desc)

You can use the below snippet:您可以使用以下代码段:

import org.apache.spark.sql.functions.col
import org.apache.spark.sql.expressions.Window

Window.partitionBy(partitionsColumnsList.map(col): _*)
.orderBy(array_union(orderByList.map(col): _*).desc)

If this did not work.如果这不起作用。 Please let me know.请告诉我。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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