Spark (4) Actions (上)

1. collect:

收集所有 RDD 元素。

val a = sc.parallelize(List(1,2,2,3,3))
res0: Array[Int] = Array(1, 2, 2, 3, 3)

2. count:

計算 RDD 元素的個數。

a.count
res1: Long = 5

3. countByValue:

回傳以元素為 key,以個數為 value 的鍵值對。

a.countByValue
res2: scala.collection.Map[Int,Long] = Map(1 -> 1, 2 -> 2, 3 -> 2)

4. take:

從 RDD 中取出 N 個元素。

a.take(2)
res3: Array[Int] = Array(1, 2)

5. top:

回傳 top N 個元素。

a.top(2)
res4: Array[Int] = Array(3, 3)

6. foreach:

iterate 所有元素並將元素帶入給定函式。

a.foreach(println)

留言

熱門文章