如何以随机顺序将数据帧写入hdfs csv?

2024-10-04 01:30:53 发布

您现在位置:Python中文网/ 问答频道 /正文

我想写一个数据帧到hdfs。但是随机排序非常慢,尽管只有1700万个数据行。在

df = df.withColumn('random_index',rand())
df = df.orderBy('random_index')

df.write.csv('hdfs:///user/yananc/yanan_gbdt_dnn', sep=',')

有没有什么快速有效的方法来实现这一点?在


Tags: csv数据dfindex排序randomhdfswrite
1条回答
网友
1楼 · 发布于 2024-10-04 01:30:53

使用orderBy将跨数据帧的所有分区对行进行排序。因为您只想对行进行无序处理,所以可以考虑改用sortWithinPartitions。此方法不需要对数据进行洗牌,因此速度更快:

df.sortWithinPartitions(rand())

当然,根据数据帧的不同,这不会对数据给出完全随机的顺序。这取决于行在分区中的分布方式。在

相关问题 更多 >