Scala 将dataframe透视到固定的spark sql列数

Scala 将dataframe透视到固定的spark sql列数,scala,apache-spark,spark-shell,Scala,Apache Spark,Spark Shell,我有一个数据帧 val df = spark.sqlContext.createDataFrame(Seq( ("100","3","sceince","A"), ("100","3","maths","B"), ("100","3","maths","F"), ("100","3","computesrs","null"), ("101","2","maths","E"), ("101","2","computesrs","C"), ("102","2","maths","null"), ("

我有一个数据帧

val df = spark.sqlContext.createDataFrame(Seq( ("100","3","sceince","A"), ("100","3","maths","B"), ("100","3","maths","F"), ("100","3","computesrs","null"), ("101","2","maths","E"), ("101","2","computesrs","C"), ("102","2","maths","null"), ("102","2","computesrs","C"),  ("100","2","maths","D"), ("100","2","computesrs","C") )).toDF("Rid","class","subject","teacher")

scala> df.show
+---+-------+----------+-------+
|Rid|class  |   subject|teacher|
+---+-------+----------+-------+
|100|      3|   sceince|      A|
|100|      3|     maths|      B|
|100|      3|     maths|      F|
|100|      3|computesrs|   null|
|101|      2|     maths|      E|
|101|      2|computesrs|      C|
|102|      2|     maths|   null|
|102|      2|computesrs|      C|
|100|      2|     maths|      D|
|100|      2|computesrs|      C|
+---+-------+----------+-------+
我必须将此数据帧透视到一些(5)固定列中,这些列按
Rid
class
分组。这里,
subject
列可能没有n个不同的值,但是根据
Rid
class
我们必须生成
subject
teacher
列作为键值对

预期数据帧:

    +-------+-------+-----------+---------------+---------------+---------------+-----------+---------------+---------------+---------------+--------+--------------+
    |Rid    |class  |period1    |periodteacher1 |period2        |periodteacher2 |period3    |periodteacher3 |period4        |periodteacher4 |period5 |periodteacher5|
    +-------+-------+-----------+---------------+---------------+---------------+-----------+---------------+---------------+---------------+--------+--------------+
    |100    |3      |sceince    |A              |maths          |B              |maths      |F              |computesrs     |               |        |              |
    |100    |2      |maths      |D              |computesrs     |C              |           |               |               |               |        |              |
    |101    |2      |maths      |E              |computesrs     |C              |           |               |               |               |        |              |
    |102    |2      |maths      |               |computesrs     |C              |           |               |               |               |        |              |
    +-------+-------+-----------+---------------+---------------+---------------+-----------+---------------+---------------+---------------+--------+--------------+

有什么建议吗?

您需要排名,然后应用pivot。请参阅下面的代码

scala> import org.apache.spark.sql.expressions.Window
import org.apache.spark.sql.expressions.Window

scala> import org.apache.spark.sql.functions._
import org.apache.spark.sql.functions._
scala> val df = Seq((100,3,"sceince","A"), (100,3,"maths","B"), (100,3,"maths","F"), (100,3,"computesrs",null), (101,2,"maths","E"), (101,2,"computesrs","C"), (102,2,"maths",null), (102,2,"computesrs","C"), (100,2,"maths","D"), (100,2,"computesrs","C")).toDF("Rid", "class", "subject", "teacher")
df: org.apache.spark.sql.DataFrame = [Rid: int, class: int ... 2 more fields]

scala> df.show
+---+-----+----------+-------+
|Rid|class|   subject|teacher|
+---+-----+----------+-------+
|100|    3|   sceince|      A|
|100|    3|     maths|      B|
|100|    3|     maths|      F|
|100|    3|computesrs|   null|
|101|    2|     maths|      E|
|101|    2|computesrs|      C|
|102|    2|     maths|   null|
|102|    2|computesrs|      C|
|100|    2|     maths|      D|
|100|    2|computesrs|      C|
+---+-----+----------+-------+
创建用于排名的窗口函数

scala> val wind2 = Window.partitionBy("Rid", "class").orderBy("subject")
wind2: org.apache.spark.sql.expressions.WindowSpec = org.apache.spark.sql.expressions.WindowSpec@6e13e2fc

scala> val resDF = df.withColumn("rank", row_number().over(wind2))
resDF: org.apache.spark.sql.DataFrame = [Rid: int, class: int ... 3 more fields]

scala> resDF.show
+---+-----+----------+-------+----+
|Rid|class|   subject|teacher|rank|
+---+-----+----------+-------+----+
|102|    2|computesrs|      C|   1|
|102|    2|     maths|   null|   2|
|101|    2|computesrs|      C|   1|
|101|    2|     maths|      E|   2|
|100|    2|computesrs|      C|   1|
|100|    2|     maths|      D|   2|
|100|    3|computesrs|   null|   1|
|100|    3|     maths|      B|   2|
|100|    3|     maths|      F|   3|
|100|    3|   sceince|      A|   4|
+---+-----+----------+-------+----+
立即应用透视图

scala> resDF.groupBy("Rid", "class").pivot("rank", Seq("1", "2", "3", "4", "5")).agg(first($"subject"), first($"teacher")).show
+---+-----+-----------------------+-----------------------+-----------------------+-----------------------+-----------------------+-----------------------+-----------------------+-----------------------+-----------------------+-----------------------+
|Rid|class|1_first(subject, false)|1_first(teacher, false)|2_first(subject, false)|2_first(teacher, false)|3_first(subject, false)|3_first(teacher, false)|4_first(subject, false)|4_first(teacher, false)|5_first(subject, false)|5_first(teacher, false)|
+---+-----+-----------------------+-----------------------+-----------------------+-----------------------+-----------------------+-----------------------+-----------------------+-----------------------+-----------------------+-----------------------+
|102|    2|                  maths|                   null|             computesrs|                      C|                   null|                   null|                   null|                   null|                   null|                   null|
|101|    2|                  maths|                      E|             computesrs|                      C|                   null|                   null|                   null|                   null|                   null|                   null|
|100|    2|                  maths|                      D|             computesrs|                      C|                   null|                   null|                   null|                   null|                   null|                   null|
|100|    3|                sceince|                      A|                  maths|                      B|                  maths|                      F|             computesrs|                   null|                   null|                   null|
+---+-----+-----------------------+-----------------------+-----------------------+-----------------------+-----------------------+-----------------------+-----------------------+-----------------------+-----------------------+-----------------------+
使用
重命名列。WithColumnRename(,)