How to sort RDD

I have scoreTriplets - this is RDD [ARRAY [String]], which I sort as follows.

var ScoreTripletsArray = scoreTriplets.collect()
  if (ScoreTripletsArray.size > 0) {        
    /*Sort the ScoreTripletsArray descending by score field*/        
    scala.util.Sorting.stableSort(ScoreTripletsArray, (e1: Array[String], e2: Array[String]) => e1(3).toInt > e2(3).toInt)
}

But collect () will be heavy if there are elements that are not there.

So I need to sort RDD using scorewithout using the collect () function.
scoreTriples - RDD [ARRAY [String]], each RDD line will store an array of the following variables.
EdgeId sourceID destID scoresourceNAme destNAme distance

Please give me any link or hint.

+4
source share
2 answers

- , sortBy:

import scala.util.Random

val data = Seq.fill(10)(Array.fill(3)("") :+ Random.nextInt.toString)
val rdd  = sc.parallelize(data)

val sorted = rdd.sortBy(_.apply(3).toInt)
sorted.take(3)
// Array[Array[String]] = Array(
//   Array("", "", "", -1660860558),
//   Array("", "", "", -1643214719),
//   Array("", "", "", -1206834289))

, top takeOrdered.

import scala.math.Ordering

rdd.takeOrdered(2)(Ordering.by[Array[String], Int](_.apply(3).toInt))
// Array[Array[String]] = 
//   Array(Array("", "", "", -1660860558), Array("", "", "", -1643214719))

rdd.top(2)(Ordering.by[Array[String], Int](_.apply(3).toInt))
// Array[Array[String]] = 
//   Array(Array("", "", "", 1920955686), Array("", "", "", 1597012602))
+4

RDD sortBy (. doc). -

scoreTriplets.sortBy( _(3).toInt )
+2

Source: https://habr.com/ru/post/1616328/


All Articles