Explain the top() and takeOrdered() operation.

Viewing 1 reply thread
  • Author
    Posts
    • #5090
      DataFlair Team
      Moderator

      Explain the top() and takeOrdered() operation.

    • #5093
      DataFlair Team
      Moderator
        <li style=”list-style-type: none”>
      • Both top() and takeOrdered() are actions.
      • Both returns then elements of RDD based on default ordering or based on custom ordering provided by user.

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

      Returns the top k (largest) elements from this RDD as defined by the specified implicit Ordering[T] and maintains the ordering. This does the opposite of takeOrdered.

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

      Returns the first k (smallest) elements from this RDD as defined by the specified implicit Ordering[T] and maintains the ordering. This does the opposite of top.

      Example :

      val myrdd1 = sc.parallelize(List(5,7,9,13,51,89))
      myrdd1.top(3)
      myrdd1.takeOrdered(3)
      myrdd1.top(3)

      Output :

      Array[Int] = Array(89, 51, 13)
      Array[Int] = Array(5, 7, 9)
      Array[Int] = Array(89, 51, 13)

      See also: Operation in RDD.

Viewing 1 reply thread
  • You must be logged in to reply to this topic.