spark-Action算子

count

Return the number of elements in the RDD.

这个算子就是来算一下所有分区有多少条数据,因为底层调用了runJob方法,所以是一个Action方法

package com.doit.spark.day05

import org.apache.spark.{SparkConf, SparkContext}

object Count {
  def main(args: Array[String]): Unit = {
    val conf = new SparkConf().setAppName("MapPartitionsDemo").setMaster("local[*]")
    val sc = new SparkContext(conf)
    val nums = sc.parallelize(List(1, 1, 2, 2, 2, 3, 4, 5, 4, 3, 2, 4, 2, 5), 4)
    //rdd调用count方法,因为有四个分区  结果为3+4+3+4=14
    val c: Long = nums.count()
    //手写源码方法
    val ints: Array[Int] = sc.runJob(nums, (it: Iterator[Int]) => {
    var i = 0
      while (it.hasNext) {
        it.next()
        i += 1
      }
      i
    })
    print(ints.sum)    
  }
}

top

这个方法是来计算出所有数据中排序前两个

object TopN {
  def main(args: Array[String]): Unit = {
    val conf = new SparkConf().setAppName("DistinctDemo").setMaster("local[*]")
    val sc = new SparkContext(conf)

    val nums: RDD[Int] = sc.parallelize(List(1,8,3,6, 2,9,5,4,7), 2)
    val r: Array[Int] = nums.top(2)
  }
}

源码

可以自定义排序方法,是将每个分区中的前N名拿出来,通过new BoundedPriorityQueue[T](num)(ord.reverse)

mapRDDs.reduce { (queue1, queue2) =>
  queue1 ++= queue2
  queue1
}.toArray.sorted(ord)

再来和并

def top(num: Int)(implicit ord: Ordering[T]): Array[T] = withScope {
    takeOrdered(num)(ord.reverse)
  }


def takeOrdered(num: Int)(implicit ord: Ordering[T]): Array[T] = withScope {
    if (num == 0) {
      Array.empty
    } else {
      val mapRDDs = mapPartitions { items =>
        // Priority keeps the largest elements, so let's reverse the ordering.
        val queue = new BoundedPriorityQueue[T](num)(ord.reverse)
        queue ++= collectionUtils.takeOrdered(items, num)(ord)
        Iterator.single(queue)
      }
      if (mapRDDs.partitions.length == 0) {
        Array.empty
      } else {
        mapRDDs.reduce { (queue1, queue2) =>
          queue1 ++= queue2
          queue1
        }.toArray.sorted(ord)
      }
    }
  }

take

就是拿出所有数据中的前n个数据,从0分区开始拿,需要拿的数据在多少个分区就action几次

max和min

取出数据中最大的值和最小的值

底层调用reduce方法

 val i: Int = nums.reduce((a, b) => Math.max(a, b))

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值