调用dataframe.distinct()会导致内容转移到驱动程序以作最后的区分吗?

问题描述:

我有下面的代码,它试图读取一些jsons,区分它们并将输出写入单个json文件。我的问题是,如果我应.collect().distinct()或它会发生在幕后呢?调用dataframe.distinct()会导致内容转移到驱动程序以作最后的区分吗?

val manyJsons = sqlContext.read.json(someJsonDirectory) 
val distinctJsons = manyJsons.distinct() 
distinctJsons.coalesce(1).write.json(jsonDirectoryWithOneFile) 

如果你正在写在磁盘上的文件,你不需要.collect()

.distinct()shuffle数据发现duplicatesremove duplicates

.coalesce(1)代码在写入文件之前将所有的partitions移动到一个节点。这等于.collect()。唯一的区别是.collect()会将所有分区移动到driver node,但.coalesce可能会或可能不会将所有分区移动到driver node.coalesce(1)用于创建一个分区,以便输出文件只有一个。