scala 如何打乱 Spark 数据帧中的行?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/43637625/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-22 09:12:31  来源:igfitidea点击:

How to shuffle the rows in a Spark dataframe?

scalaapache-sparkdataframeapache-spark-sql

提问by Laure D

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.

我想在 Scala 中使用 Spark 对所有行进行洗牌。

How can I do this without going back to RDD?

我怎么能在不回到 RDD 的情况下做到这一点?

回答by prudenko

You need to use orderBymethod of the dataframe:

您需要使用orderBy数据框的方法:

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