繁体   English   中英

如何在没有本地迭代器的情况下返回Spark RDD分区值?

[英]How do I return Spark RDD partition values without a local iterator?

我正在学习Spark及其与RDD分区分布有关的并行性。 我有一台4 CPU的计算机,因此我有4个并行单元。 要返回分区索引“ 0”的成员,我找不到不强制RDD使用localIterator的方法来返回此分区。

我习惯火花很简洁。 是否有更简洁的方法来按分区过滤RDD? 以下两种方法有效,但看起来很笨拙。

scala> val data = 1 to 20
data: scala.collection.immutable.Range.Inclusive = Range(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20)

scala> val distData = sc.parallelize(data)
distData: org.apache.spark.rdd.RDD[Int] = ParallelCollectionRDD[75] at parallelize at <console>:26

scala> distData.mapPartitionsWithIndex{
   (index,it) => {
      it.toList.map(x => if (index == 0) (x)).iterator
   }
}.toLocalIterator.toList.filterNot(
   _.isInstanceOf[Unit]
)
res107: List[AnyVal] = List(1, 2, 3, 4, 5)

scala> distData.mapPartitionsWithIndex{
   (index,it) => {
      it.toList.map(x => if (index == 0) (x)).iterator
   }
}.toLocalIterator.toList.filter(
   _ match{
      case x: Unit => false
      case x => true
   }
)
res108: List[AnyVal] = List(1, 2, 3, 4, 5)
distData.mapPartitionsWithIndex{ (index, it) => 
      if (index == 0) it else Array[Int]().iterator
}

您可以返回一个空的迭代器,它将正常工作。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM