mapPartitions()

    leafsRDD = leafsRDD.mapPartitions(reducer, True) \
                           .coalesce(numPartitions) \
                           .cache()

mapPartitions():

Return a new DStream in which each RDD is generated by applying
mapPartitions() to each RDDs of this DStream.

rdd的mapPartitions是map的一个变种,它们都可进行分区的并行处理。

两者的主要区别是调用的粒度不一样:map的输入变换函数是应用于RDD中每个元素,而mapPartitions的输入函数是应用于每个分区。

假设一个rdd有10个元素,分成3个分区。如果使用map方法,map中的输入函数会被调用10次;而使用mapPartitions方法的话,其输入函数会只会被调用3次,每个分区调用1次。

coalesce函数可以控制是否shuffle,但当shuffle为false时,只能减小Partition数,无法增大。

SQL like coalesce.

coalesce(a, b) = {
    a if a is not NULL
    b otherwise
}

Examples
--------
>>> coalesce(1, 2)
1
>>> coalesce(1, None)
1
>>> coalesce(None, 2)
2
>>> coalesce(None, None) is None
True
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值