Spark RDD API全集

RDD是啥

Resilient Distributed Dataset (RDD),弹性分布式数据集,是对不可修改,分区的数据集合的抽象。

RDD is characterized by five main properties:

  • A list of partitions
  • A function for computing each split
  • A list of dependencies on other RDDs
  • Optionally, a Partitioner for key-value RDDs (e.g. to say that the RDD is hash-partitioned)
  • Optionally, a list of preferred locations to compute each split on (e.g. block locations for an HDFS file)

org.spark.rdd.RDD类方法

RDD是一个抽象类,定义如下

 
 
  1. abstract class RDD[T] extends Serializable with Logging

RDD类的public方法大约有80多个(包括不同参数重载的),均在下面列出。

值得注意的是,RDD类中并没有定义xxxByKey形式的方法,这类方法其实是在PairRDDFunctions中定义的,通过隐式转换,键值对形式的RDD(即RDD[(K, V))可以调用PairRDDFunctions中定义的方法。

键值转换操作

 
 
  1. filter(f: (T) Boolean): RDD[T]
  2. 过滤数据,仅留下使得f返回true的元素。
  3. map[U](f: (T) U)(implicit arg0: ClassTag[U]): RDD[U]
  4. 将一个RDD中的每个数据项,通过map中的函数映射变为一个新的元素。
  5. 输入分区与输出分区一对一,即:有多少个输入分区,就有多少个输出分区。
  6. flatMap[U](f: (T) TraversableOnce[U])(implicit arg0: ClassTag[U]): RDD[U]
  7. 第一步和map一样,最后将所有的输出分区合并成一个。
  8. 使用flatMap时候需要注意:
  9. flatMap会将字符串看成是一个字符数组。
  10. mapPartitions[U](f: (Iterator[T]) Iterator[U], preservesPartitioning: Boolean = false)(implicit arg0: ClassTag[U]): RDD[U]
  11. 该函数和map函数类似,只不过映射函数的参数由RDD中的每一个元素变成了RDD中每一个分区的迭代器。如果在映射的过程中需要频繁创建额外的对象,使用mapPartitions要比map高效的过。
  12. 比如,将RDD中的所有数据通过JDBC连接写入数据库,如果使用map函数,可能要为每一个元素都创建一个connection,这样开销很大,如果使用mapPartitions,那么只需要针对每一个分区建立一个connection
  13. 参数preservesPartitioning表示是否保留父RDDpartitioner分区信息。
  14. mapPartitionsWithIndex[U](f: (Int, Iterator[T]) => Iterator[U], preservesPartitioning: Boolean = false)(implicit arg0: ClassTag[U]): RDD[U]
  15. 函数作用同mapPartitions,不过提供了两个参数,第一个参数为分区的索引。
  16. keyBy[K](f: (T) K): RDD[(K, T)]
  17. 通过f函数为每个元素生成一个KEY
  18. sortBy[K](f: (T) K, ascending: Boolean = true, numPartitions: Int = this.partitions.length)(implicit ord: Ordering[K], ctag: ClassTag[K]): RDD[T]
  19. 通过给定的函数对元素排序
  20. zip[U](other: RDD[U])(implicit arg0: ClassTag[U]): RDD[(T, U)]
  21. 与另一个RDD组合成(k,v)对。
  22. zipPartitions[B, V](rdd2: RDD[B], preservesPartitioning: Boolean)(f: (Iterator[T], Iterator[B]) Iterator[V])(implicit arg0: ClassTag[B], arg1: ClassTag[V]): RDD[V]
  23. zipWithIndex(): RDD[(T, Long)]
  24. zipWithUniqueId(): RDD[(T, Long)]

聚合相关

 
 
  1. aggregate[U](zeroValue: U)(seqOp: (U, T) U, combOp: (U, U) U)(implicit arg0: ClassTag[U]): U
  2. aggregate用户聚合RDD中的元素,先使用seqOpRDD中每个分区中的T类型元素聚合成U类型,再使用combOp将之前每个分区聚合后的U类型聚合成U类型,特别注意seqOpcombOp都会使用zeroValue的值,zeroValue的类型为U
  3. treeAggregate[U](zeroValue: U)(seqOp: (U, T) U, combOp: (U, U) U, depth: Int = 2)(implicit arg0: ClassTag[U]): U
  4. 多层级聚合
  5. reduce(f: (T, T) T): T
  6. 根据映射函数f,对RDD中的元素进行二元计算,返回计算结果。
  7. treeReduce(f: (T, T) T, depth: Int = 2): T
  8. 多级reduce归并聚合
  9. fold(zeroValue: T)(op: (T, T) T): T
  10. foldaggregate的简化,将aggregate中的seqOpcombOp使用同一个函数op
  11. count(): Long
  12. count返回RDD中的元素数量。
  13. countApprox(timeout: Long, confidence: Double = 0.95): PartialResult[BoundedDouble]
  14. 近似count
  15. countApproxDistinct(relativeSD: Double = 0.05): Long
  16. countApproxDistinct(p: Int, sp: Int): Long
  17. 近似distinct count
  18. countByValue()(implicit ord: Ordering[T] = null): Map[T, Long]
  19. 计算每个值出现次数
  20. countByValueApprox(timeout: Long, confidence: Double = 0.95)(implicit ord: Ordering[T] = null):
  21. 计算每个值出现次数近似值
  22. distinct(): RDD[T]
  23. distinct(numPartitions: Int)(implicit ord: Ordering[T] = null): RDD[T]
  24. 返回元素去重后的RDD
  25. groupBy[K](f: (T) K)(implicit kt: ClassTag[K]): RDD[(K, Iterable[T])]
  26. groupBy[K](f: (T) K, numPartitions: Int)(implicit kt: ClassTag[K]): RDD[(K, Iterable[T])]
  27. groupBy[K](f: (T) K, p: Partitioner)(implicit kt: ClassTag[K], ord: Ordering[K] = null): RDD[(K, Iterable[T])]
  28. 按指定函数生成key,并按key分组。
  29. 注意:性能比较差,推荐用PairRDDFunctions.reduceByKey or PairRDDFunctions.aggregateByKey.
  30. 因为reduceByKey会先在分区内做聚合,再进行数据交换(shuffle)。
  31. glom(): RDD[Array[T]]
  32. 该函数是将RDD中每一个分区中类型为T的元素转换成Array[T],这样每一个分区就只有一个数组元素。
  33. max()(implicit ord: Ordering[T]): T
  34. 最大的元素
  35. min()(implicit ord: Ordering[T]): T
  36. 最小的元素

遍历元素

 
 
  1. foreach(f: (T) Unit): Unit
  2. foreach用于遍历RDD,将函数f应用于每一个元素。
  3. 但要注意,如果对RDD执行foreach,只会在Executor端有效,而并不是Driver端。
  4. 比如:rdd.foreach(println),只会在Executorstdout中打印出来,Driver端是看不到的。
  5. foreachPartition(f: (Iterator[T]) Unit): Unit
  6. foreachPartitionforeach类似,只不过是对每一个分区使用f

取元素相关

 
 
  1. collect(): Array[T]
  2. collect用于将一个RDD转换成数组。
  3. first(): T
  4. first返回RDD中的第一个元素,不排序。
  5. take(num: Int): Array[T]
  6. take用于获取RDD中从0num-1下标的元素,不排序。
  7. top(num: Int)(implicit ord: Ordering[T]): Array[T]
  8. top函数用于从RDD中,按照默认(降序)或者指定的排序规则,返回前num个元素。
  9. takeOrdered(num: Int)(implicit ord: Ordering[T]): Array[T]
  10. takeOrderedtop类似,只不过以和top相反的顺序返回元素
  11. takeSample(withReplacement: Boolean, num: Int, seed: Long = Utils.random.nextLong): Array[T]
  12. 取样本元素

集合间运算

 
 
  1. ++(other: RDD[T]): RDD[T]
  2. 与另一个RDD union
  3. intersection(other: RDD[T], partitioner: Partitioner)(implicit ord: Ordering[T] = null): RDD[T]
  4. intersection(other: RDD[T], numPartitions: Int): RDD[T]
  5. intersection(other: RDD[T]): RDD[T]
  6. 取交集
  7. subtract(other: RDD[T], p: Partitioner)(implicit ord: Ordering[T] = null): RDD[T]
  8. subtract(other: RDD[T], numPartitions: Int): RDD[T]
  9. subtract(other: RDD[T]): RDD[T]
  10. 求差集
  11. union(other: RDD[T]): RDD[T]
  12. 与另一个RDD合并,类似union all,不会去重。

其他

 
 
  1. persist(): RDD.this.type
  2. persist(newLevel: StorageLevel): RDD.this.type
  3. 缓存数据,可设置缓存级别(如果尚未设置过,才可以设置,本地checkpoint除外)
  4. unpersist(blocking: Boolean = true): RDD.this.type
  5. Mark the RDD as non-persistent, and remove all blocks for it from memory and disk.
  6. cache(): RDD.this.type
  7. MEMORY_ONLY级别缓存数据
  8. cartesian[U](other: RDD[U])(implicit arg0: ClassTag[U]): RDD[(T, U)]:
  9. 计算两个RDD的迪卡尔积
  10. checkpoint(): Unit
  11. 标记将该RDD进行checkpoint处理?
  12. coalesce(numPartitions: Int, shuffle: Boolean = false)(implicit ord: Ordering[T] = null): RDD[T]
  13. 分区合并(只能减少分区),使用HashPartitioner
  14. 第一个参数为重分区的数目,第二个为是否进行shuffle,默认为false;
  15. repartition(numPartitions: Int)(implicit ord: Ordering[T] = null): RDD[T]
  16. 调整分区数,会导致shuffle,如果是减少分区,可以使用coalesce,避免shuffle
  17. toDebugString: String
  18. 返回RDD依赖树/血统图
  19. getCheckpointFile: Option[String]
  20. 获取checkpoint文件夹名称
  21. localCheckpoint(): RDD.this.type
  22. 标记为使用本地checkpoint
  23. isEmpty(): Boolean
  24. 是否含0个元素
  25. iterator(split: Partition, context: TaskContext): Iterator[T]
  26. 返回迭代器,不应直接调用,而是给RDD的子类用的。
  27. toLocalIterator: Iterator[T]
  28. 返回元素的本地迭代器
  29. pipe(command: String): RDD[String]
  30. pipe(command: String, env: Map[String, String]): RDD[String]
  31. pipe(command: Seq[String], env: Map[String, String] = Map(), printPipeContext: ((String) Unit) Unit = null, printRDDElement: (T, (String) Unit) Unit = null, separateWorkingDir: Boolean = false, bufferSize: Int = 8192, encoding: String = Codec.defaultCharsetCodec.name): RDD[String]
  32. 调用外部进程处理RDD,如通过标准输入传给shell脚本。
  33. preferredLocations(split: Partition): Seq[String]
  34. Get the preferred locations of a partition, taking into account whether the RDD is checkpointed.
  35. randomSplit(weights: Array[Double], seed: Long = Utils.random.nextLong): Array[RDD[T]]
  36. 按权随机将元素分组
  37. sample(withReplacement: Boolean, fraction: Double, seed: Long = Utils.random.nextLong): RDD[T]
  38. 取样本/子集
  39. setName(_name: String): RDD.this.type
  40. 设置RDD名字

保存

 
 
  1. saveAsObjectFile(path: String): Unit
  2. 保存为SequenceFile
  3. saveAsTextFile(path: String, codec: Class[_ <: CompressionCodec]): Unit
  4. saveAsTextFile(path: String): Unit
  5. 保存为文本文件

变量

 
 
  1. context: SparkContext
  2. 创建RDDSparkContext
  3. sparkContext: SparkContext
  4. 创建RDDSparkContext
  5. dependencies: Seq[Dependency[_]]
  6. RDD的依赖列表
  7. getNumPartitions: Int
  8. 获取RDD的分区数
  9. getStorageLevel: StorageLevel
  10. 获取存储等级,如果设置为none,则返回StorageLevel.NONE
  11. id: Int
  12. RDDunique ID
  13. isCheckpointed: Boolean
  14. 是否checkpointed and materialized, either reliably or locally.
  15. name: String
  16. RDD的名字
  17. partitioner: Option[Partitioner]
  18. 分区器
  19. partitions: Array[Partition]
  20. 各个分区


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值