Spark无法查找窗口功能

时间:2015-10-02 10:42:00

标签: sql scala apache-spark apache-spark-sql window-functions

使用https://stackoverflow.com/a/32407543/5379015中提供的解决方案 我尝试重新创建相同的查询,但使用编程语法代替Dataframe API,如下所示:

import org.apache.spark.{SparkContext, SparkConf}
import org.apache.spark.sql.hive.HiveContext
import org.apache.spark.sql.expressions.Window
import org.apache.spark.sql.functions._

object HiveContextTest {
  def main(args: Array[String]) {
    val conf = new SparkConf().setAppName("HiveContextTest")
    val sc = new SparkContext(conf)
    val sqlContext = new HiveContext(sc)
    import sqlContext.implicits._

    val df = sc.parallelize(
      ("foo", 1) :: ("foo", 2) :: ("bar", 1) :: ("bar", 2) :: Nil
    ).toDF("k", "v")


    // using dataframe api works fine

    val w = Window.partitionBy($"k").orderBy($"v")
    df.select($"k",$"v", rowNumber().over(w).alias("rn")).show


    //using programmatic syntax doesn't work

    df.registerTempTable("df")
    val w2 = sqlContext.sql("select k,v,rowNumber() over (partition by k order by v) as rn from df")
    w2.show()

  }
}

第一个df.select($"k",$"v", rowNumber().over(w).alias("rn")).show工作正常但w2.show()导致

Exception in thread "main" org.apache.spark.sql.AnalysisException: Couldn't find window function rowNumber;

有没有人对如何使用程序语法进行此操作有任何想法?非常感谢提前。

1 个答案:

答案 0 :(得分:1)

等同于rowNumber的SQL是row_number

SELECT k, v, row_number() OVER (PARTITION BY k ORDER BY v) AS rn FROM df