Проверьте код ниже.
scala> val df = Seq(100,149,177,102,133,149,16,19,19).toDF("id")
scala> df.show(false)
+---+
|id |
+---+
|100|
|149|
|177|
|102|
|133|
|149|
|16 |
|19 |
|19 |
+---+
scala> val n = 3
n: Int = 3
scala> val cutOff = df.count.toLong/n.toLong
cutOff: Long = 3
scala> val rowExpr = ntile(n.toInt).over(Window.orderBy(lit(1)))
scala> val columns = struct(
(0L until cutOff)
.map(i => col("data")(i).as(s"data_${i}"))
.reverse:_*
)
scala> df
.withColumn("row_id",rowExpr)
.groupBy($"row_id")
.agg(collect_list($"id").as("data"))
.withColumn("data",columns)
.orderBy($"row_id".asc)
.select($"data.*")
.show(false)
+------+------+------+
|data_2|data_1|data_0|
+------+------+------+
|177 |149 |100 |
|149 |133 |102 |
|19 |19 |16 |
+------+------+------+