Spark学习摘记 —— RDD行动操作API归纳
阅读原文时间:2023年07月08日阅读:1

本文参考

参考《Spark快速大数据分析》动物书中的第三章"RDD编程",前一篇文章已经概述了转化操作相关的API,本文再介绍行动操作API

和转化操作API不同的是,行动操作API只能作用于一个RDD

RDD转化操作API归纳:https://www.cnblogs.com/kuluo/p/12545374.html

Pair RDD转化操作API归纳:https://www.cnblogs.com/kuluo/p/12558563.html

Pair RDD行动操作API归纳:https://www.cnblogs.com/kuluo/p/12567221.html

环境

idea + spark 2.4.5 + scala 2.11.12

RDD均通过SparkContext的parallelize()函数创建

collect()函数

目的:

返回RDD中的所有元素

代码:

val testList = List(1, 2, 3, 3)
val testRdd = sc.parallelize(testList)
testRdd.collect().foreach(ele => print(s"$ele "))

**输出:
**

1 2 3 3

**注意:
**

该函数会将所有元素放入驱动器进程中,只有当整个数据集能在单台机器的内存中放得下时,才能使用,不适宜用在大规模数据集,因此大多数情况下用于本地开发测试,以下还会碰到很多这样不适合大数据量访问的函数

count()函数

**目的:
**

返回RDD中的元素个数

**代码:
**

val testList = List(1, 2, 3, 3)
val testRdd = sc.parallelize(testList)
println(testRdd.count())

**输出:
**

4

countByValue()函数

**目的:
**

各元素在RDD中出现的次数

在经典的WordCount示例程序中我们一般先通过flatMap()函数进行压扁,再用map()函数转为pair RDD,最后用reduceByKey()函数进行计数,实际上我们可以通过countByValue()函数,将WordCount示例程序中的map()函数和reduceByKey()函数两步操作,合并为一步

**代码:
**

val testList = List(1, 2, 3, 3)
val testRdd = sc.parallelize(testList)
testRdd.countByValue().foreach(ele => print(s"$ele "))

**输出:
**

(1,1) (3,2) (2,1)

**和WordCount程序的对比:
**

val testList = List("a a a a b b c c", "a a c c c d d", "b b c c d", "a b c d")
val testRdd = sc.parallelize(testList)
testRdd.flatMap(line =>

line.split(" ")
).map(ele =>

  (ele, 1)
).reduceByKey{case (x, y) =>

  x + y
}.foreach(pair => print(s"$pair "))

使用countByValue后

val testList = List("a a a a b b c c", "a a c c c d d", "b b c c d", "a b c d")
val testRdd = sc.parallelize(testList)
testRdd.flatMap(line =>

  line.split(" ")
).countByValue().foreach(pair => print(s"$pair "))

两个示例程序输出结果相同:(d,4) (a,7) (b,5) (c,8)

**注意:
**

尽管使用该函数能够简化我们的编程,但是在源码中有如下注释:

This method should only be used if the resulting map is expected to be small, as the whole thing is loaded into the driver's memory.

To handle very large results, consider using rdd.map(x => (x, 1L)).reduceByKey(_ + _) , which returns an RDD[T, Long] instead of a map.

由此可见countByValue()函数也会将所有元素加载到驱动器进程,建议在数据集不大的情况下使用

另外一点要注意的是,countByValue()函数返回的RDD中的每个元素的类型是Map,但并不是我们常说的pair RDD,pair RDD相关的转化和行动操作不能应用在元素类型是Map的RDD上

take()函数

**目的:
**

从RDD中返回num个元素,将尽可能访问少的分区

**代码:
**

val testList = List(3, 4, 2, 1, 9, 6, 3, 6, 8, 9)
val testRdd = sc.parallelize(testList)
testRdd.take(5).foreach(pair => print(s"$pair "))

**输出:
**

3 4 2 1 9(不一定)

**注意:
**

This method should only be used if the resulting array is expected to be small, as all the data is loaded into the driver's memory.

take ()函数也会将所有元素加载到驱动器进程,建议在返回元素不多的情况下使用

takeSample()函数

**目的:
**

从RDD中返回任意一些元素

**代码:
**

val testList = List(0, 1, 2, 3, 4, 5, 6, 7, 8, 9)
val testRdd = sc.parallelize(testList)
testRdd.takeSample(true, 5).foreach(pair => print(s"$pair "))

**输出:
**

0 4 0 3 2(会出现重复元素)

**代码:
**

val testList = List(0, 1, 2, 3, 4, 5, 6, 7, 8, 9)
val testRdd = sc.parallelize(testList)
testRdd. takeSample(false, 5).foreach(pair => print(s"$pair "))

**输出:
**

0 3 8 2 7(不会出现重复元素)

**sample()函数、take()函数和takeSample()函数的区别:
**

sample()函数是转化操作API,withReplacement参数取false时,fraction参数取[0, 1]并表示RDD中每个元素被选中的可能性

take()函数是行动操作API,从RDD中抽取由num参数指定的数量的元素,首先访问RDD中的第一个分区,若第一个分区中的元素个数已经能够满足我们num参数指定的数量,将不再访问其它分区,即尽可能少地访问RDD分区,以下为take()函数源码注释

Take the first num elements of the RDD. It works by first scanning one partition, and use the results from that partition to estimate the number of additional partitions needed to satisfy the limit.

takeSample()函数是行动操作API,从withReplacement参数取false时,结果RDD中不会出现重复元素,num参数表示要取得的元素的个数

sample()函数takeSample()函数都有第三个参数seed用来设置随机数种子,保证每次抽取RDD元素的结果集不同

take()函数和takeSample()函数被明确指明各元素会被加载到驱动器进程,不适合大数据量的情况下使用

top()函数

**目的:
**

从RDD中返回最前面的num个元素,降序排序

内部实际上调用takeOrdered()函数

**代码:
**

val testList = List(3, 4, 2, 1, 9, 6, 3, 6, 8, 9)
val testRdd = sc.parallelize(testList)
testRdd.top(5).foreach(pair => print(s"$pair "))

等价写法

val testList = List(3, 4, 2, 1, 9, 6, 3, 6, 8, 9)
val testRdd = sc.parallelize(testList)
testRdd.takeOrdered(5)(Ordering[Int].reverse).foreach(pair => print(s"$pair "))

**输出:
**

9 9 8 6 6

takeOrdered()函数

**目的:
**

从RDD中按照提供的顺序返回最前面的num个元素,默认升序排序

**代码:
**

val testList = List(3, 4, 2, 1, 9, 6, 3, 6, 8, 9)
val testRdd = sc.parallelize(testList)
testRdd.takeOrdered(5).foreach(pair => print(s"$pair "))

**输出:
**

1 2 3 3 4

reduce()函数

**目的:
**

并行整合RDD中所有数据,操作两个相同元素类型的RDD数据返回一个同样类型的新元素

**代码:
**

val testList = List(0, 1, 2, 3, 4, 5, 6, 7, 8, 9)
val testRdd = sc.parallelize(testList)
println(testRdd.reduce(_ + _))

**输出:
**

45

fold()函数

**目的:
**

和reduce()函数一样,但需要提供初始值,提供的初始值应当是你提供的操作的单位元素,如加法运算的单位元素是0,乘法运算的单位元素是1

**代码:
**

val testList = List(0, 1, 2, 3, 4, 5, 6, 7, 8, 9)
val testRdd = sc.parallelize(testList)
println(testRdd.fold(0)(_ + _))

**输出:
**

45

aggregate()函数

**目的:
**

和reduce()函数相似,但返回值类型可以和操作的RDD类型不同,需要提供初始值,合并RDD内元素的函数以及将不同节点上的元素进行合并

**代码:
**

val testList = List(3, 4, 5, 3, 8, 6, 6, 7, 9, 1)
val testRdd = sc.parallelize(testList)
val result:(Int, Int)
= testRdd.aggregate((0, 0))((x, y)
=> {

  (x._1 + y, x._2 + 1)
}, (x, y) => {
  (x._1 + y._1, x._2 + y._2)
})
print(1.0 * result._1 / result._2)

计算平均值,在本例中aggregate()函数返回的是一个Tuple元组

**输出:
**

5.2

foreach()函数

**目的:
**

对RDD中的每个元素使用给定的函数,在前面的示例中均以出现,此处不再做演示

**更高效的操作:
**

val testList = List(3, 4, 5, 3, 8, 6, 6, 7, 9, 1)
val testRdd = sc.parallelize(testList)
testRdd.foreachPartition(partition => {

  partition.foreach(ele => {

print(s"$ele ")
  })
})

输出:3 4 5 3 8 6 6 7 9 1

手机扫一扫

移动阅读更方便

阿里云服务器
腾讯云服务器
七牛云服务器