Spark算子:RDD行動Action操做(2)–take、top、takeOrdered

take

def take(num: Int): Array[T]es6

take用於獲取RDD中從0到num-1下標的元素,不排序。apache

 
  1. scala> var rdd1 = sc.makeRDD(Seq(10, 4, 2, 12, 3))
  2. rdd1: org.apache.spark.rdd.RDD[Int] = ParallelCollectionRDD[40] at makeRDD at :21
  3.  
  4. scala> rdd1.take(1)
  5. res0: Array[Int] = Array(10)
  6.  
  7. scala> rdd1.take(2)
  8. res1: Array[Int] = Array(10, 4)
  9.  

top

def top(num: Int)(implicit ord: Ordering[T]): Array[T]函數

top函數用於從RDD中,按照默認(降序)或者指定的排序規則,返回前num個元素。es5

 
  1. scala> var rdd1 = sc.makeRDD(Seq(10, 4, 2, 12, 3))
  2. rdd1: org.apache.spark.rdd.RDD[Int] = ParallelCollectionRDD[40] at makeRDD at :21
  3.  
  4. scala> rdd1.top(1)
  5. res2: Array[Int] = Array(12)
  6.  
  7. scala> rdd1.top(2)
  8. res3: Array[Int] = Array(12, 10)
  9.  
  10. //指定排序規則
  11. scala> implicit val myOrd = implicitly[Ordering[Int]].reverse
  12. myOrd: scala.math.Ordering[Int] = scala.math.Ordering$$anon$4@767499ef
  13.  
  14. scala> rdd1.top(1)
  15. res4: Array[Int] = Array(2)
  16.  
  17. scala> rdd1.top(2)
  18. res5: Array[Int] = Array(2, 3)
  19.  

takeOrdered

def takeOrdered(num: Int)(implicit ord: Ordering[T]): Array[T]spa

takeOrdered和top相似,只不過以和top相反的順序返回元素。scala

 
  1. scala> var rdd1 = sc.makeRDD(Seq(10, 4, 2, 12, 3))
  2. rdd1: org.apache.spark.rdd.RDD[Int] = ParallelCollectionRDD[40] at makeRDD at :21
  3.  
  4. scala> rdd1.top(1)
  5. res4: Array[Int] = Array(2)
  6.  
  7. scala> rdd1.top(2)
  8. res5: Array[Int] = Array(2, 3)
  9.  
  10. scala> rdd1.takeOrdered(1)
  11. res6: Array[Int] = Array(12)
  12.  
  13. scala> rdd1.takeOrdered(2)
  14. res7: Array[Int] = Array(12, 10)
相關文章
相關標籤/搜索