How to shuffle the rows in a Spark dataframe?

2020-07-02 09:06发布

问题:

I have a dataframe like this:

+---+---+
|_c0|_c1|
+---+---+
|1.0|4.0|
|1.0|4.0|
|2.1|3.0|
|2.1|3.0|
|2.1|3.0|
|2.1|3.0|
|3.0|6.0|
|4.0|5.0|
|4.0|5.0|
|4.0|5.0|
+---+---+

and I would like to shuffle all the rows using Spark in Scala.

How can I do this without going back to RDD?

回答1:

You need to use orderBy method of the dataframe:

import org.apache.spark.sql.functions.rand
val shuffledDF = dataframe.orderBy(rand())