Gibt es in Spark 1.6.0/Scala eine Möglichkeit, collect_list("colC")
oder collect_set("colC").over(Window.partitionBy("colA").orderBy("colB")
zu bekommen?
Vorausgesetzt, Sie haben dataframe
als
+----+----+----+
|colA|colB|colC|
+----+----+----+
|1 |1 |23 |
|1 |2 |63 |
|1 |3 |31 |
|2 |1 |32 |
|2 |2 |56 |
+----+----+----+
Sie können Window
Funktionen wie folgt ausführen
import org.Apache.spark.sql.functions._
import org.Apache.spark.sql.expressions._
df.withColumn("colD", collect_list("colC").over(Window.partitionBy("colA").orderBy("colB"))).show(false)
Ergebnis:
+----+----+----+------------+
|colA|colB|colC|colD |
+----+----+----+------------+
|1 |1 |23 |[23] |
|1 |2 |63 |[23, 63] |
|1 |3 |31 |[23, 63, 31]|
|2 |1 |32 |[32] |
|2 |2 |56 |[32, 56] |
+----+----+----+------------+
Ähnlich ist das Ergebnis für collect_set
auch. Die Reihenfolge der Elemente im letzten set
ist jedoch nicht wie bei collect_list
df.withColumn("colD", collect_set("colC").over(Window.partitionBy("colA").orderBy("colB"))).show(false)
+----+----+----+------------+
|colA|colB|colC|colD |
+----+----+----+------------+
|1 |1 |23 |[23] |
|1 |2 |63 |[63, 23] |
|1 |3 |31 |[63, 31, 23]|
|2 |1 |32 |[32] |
|2 |2 |56 |[56, 32] |
+----+----+----+------------+
Wenn Sie orderBy
wie folgt entfernen
df.withColumn("colD", collect_list("colC").over(Window.partitionBy("colA"))).show(false)
ergebnis wäre
+----+----+----+------------+
|colA|colB|colC|colD |
+----+----+----+------------+
|1 |1 |23 |[23, 63, 31]|
|1 |2 |63 |[23, 63, 31]|
|1 |3 |31 |[23, 63, 31]|
|2 |1 |32 |[32, 56] |
|2 |2 |56 |[32, 56] |
+----+----+----+------------+
Ich hoffe die Antwort ist hilfreich