并行排序工作节点上的pysparkDataframe

dwthyt8l  于 2021-05-29  发布在  Spark
关注(0)|答案(1)|浏览(268)

我在主节点上有一个分布式sparkDataframe列表,我想在单独的工作节点(我的spark集群conatins 4节点)上并行地对它们进行排序。可以使用map()或flatmap()吗?或者别的什么。谢谢。

mpbci0fu

mpbci0fu1#

你可以用 sortWithinPartitions 为每个分区并行排序数据的方法。每个工作者都有自己的分区数据。
根据我对1000000个随机号码的经验。 sortWithinPartitions 性能提升2倍。

//Create a list with size 1000000 of random nos.
      spark.sparkContext.parallelize(for (i <- 1 to 1000000) yield  r.nextInt(1000000) )
        .toDF // Convert to dataframe with single column 'value'
        .sortWithinPartitions(col("value"))  // Sort the partition in parallel i.e. similair to sortin data on each worker
        .sort(col("value"))                  // Final sort on complete data set
        .map(r=>r.getInt(0)).collectAsList().toSeq // Collecting the result in list

相关问题