Spark函数讲解:collect

来源:互联网 发布:熟练使用办公软件 编辑:程序博客网 时间:2024/06/02 02:29
将RDD转成Scala数组,并返回。

函数原型

def collect(): Array[T]def collect[U: ClassTag](f: PartialFunction[T, U]): RDD[U]
collect函数的定义有两种,我们最常用的是第一个。第二个函数需要我们提供一个标准的偏函数,然后保存符合的元素到MappedRDD中。

实例

scala> val one: PartialFunction[Int, String] = { case 1 => "one"; case _ => "other"}one: PartialFunction[Int,String] = <function1>scala> val data = sc.parallelize(List(2,3,1))data: org.apache.spark.rdd.RDD[Int] =     ParallelCollectionRDD[11] at parallelize at <console>:12scala> data.collect(one).collectres4: Array[String] = Array(other, other, one)

注意

  如果数据量比较大的时候,尽量不要使用collect函数,因为这可能导致Driver端内存溢出问题。

0 0