Spark使用mapPartitions获取分区下的元素

mapPartitions解析

底层源码
  /**
   * Return a new RDD by applying a function to each partition of this RDD.
   *
   * `preservesPartitioning` indicates whether the input function preserves the partitioner, which
   * should be `false` unless this is a pair RDD and the input function doesn't modify the keys.
   */
  def mapPartitions[U: ClassTag](
      f: Iterator[T] => Iterator[U],
      preservesPartitioning: Boolean = false): RDD[U] = withScope {
    val cleanedF = sc.clean(f)
    new MapPartitionsRDD(
      this,
      (context: TaskContext, index: Int, iter: Iterator[T]) => cleanedF(iter),
      preservesPartitioning)
  }

从源码中我们看出需要传入的参数是一个迭代器,同时也需要返回一个迭代器类型,和一个布尔值.如果我们需要获取数据是属于哪一个分区的话,我们通过下面的方法获取:

    @Test
    def showData: Unit ={
        sc.parallelize(Seq(3, 8, 9, 4, 2), 2).mapPartitions(

            item => {
                val string: String = UUID.randomUUID().toString
//                println(string)
                item.foreach(it => println(s"${string}----${it}"))
                item
            }
        ).collect()
    }

得到结果:

01cb417e-08f5-4c21-b5c1-c83633fcf4a9----3
2969aa7e-fd92-4581-8375-6431b079e416----9
01cb417e-08f5-4c21-b5c1-c83633fcf4a9----8
2969aa7e-fd92-4581-8375-6431b079e416----4
2969aa7e-fd92-4581-8375-6431b079e416----2
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值