Spark and SparkSQL: How to imitate window function?

前端 未结 3 1760
野趣味
野趣味 2020-12-06 02:36

Description

Given a dataframe df

id |       date
---------------
 1 | 2015-09-01
 2 | 2015-09-01
 1 | 2015-09-03
 1 | 2015-09-04
 2 |          


        
3条回答
  •  野趣味
    野趣味 (楼主)
    2020-12-06 03:07

    You can do this with RDDs. Personally I find the API for RDDs makes a lot more sense - I don't always want my data to be 'flat' like a dataframe.

    val df = sqlContext.sql("select 1, '2015-09-01'"
        ).unionAll(sqlContext.sql("select 2, '2015-09-01'")
        ).unionAll(sqlContext.sql("select 1, '2015-09-03'")
        ).unionAll(sqlContext.sql("select 1, '2015-09-04'")
        ).unionAll(sqlContext.sql("select 2, '2015-09-04'"))
    
    // dataframe as an RDD (of Row objects)
    df.rdd 
      // grouping by the first column of the row
      .groupBy(r => r(0)) 
      // map each group - an Iterable[Row] - to a list and sort by the second column
      .map(g => g._2.toList.sortBy(row => row(1).toString))     
      .collect()
    

    The above gives a result like the following:

    Array[List[org.apache.spark.sql.Row]] = 
    Array(
      List([1,2015-09-01], [1,2015-09-03], [1,2015-09-04]), 
      List([2,2015-09-01], [2,2015-09-04]))
    

    If you want the position within the 'group' as well, you can use zipWithIndex.

    df.rdd.groupBy(r => r(0)).map(g => 
        g._2.toList.sortBy(row => row(1).toString).zipWithIndex).collect()
    
    Array[List[(org.apache.spark.sql.Row, Int)]] = Array(
      List(([1,2015-09-01],0), ([1,2015-09-03],1), ([1,2015-09-04],2)),
      List(([2,2015-09-01],0), ([2,2015-09-04],1)))
    

    You could flatten this back to a simple List/Array of Row objects using FlatMap, but if you need to perform anything on the 'group' that won't be a great idea.

    The downside to using RDD like this is that it's tedious to convert from DataFrame to RDD and back again.

提交回复
热议问题