1
votes

I am working on a project with Apache spark. I was pretty much aware of transformations and actions in spark, however all in a sudden I am confused with sortByKey() function in spark.

How does that work, does it considers all partitions(i.e all RDDs) for sorting or data is sorted within a single RDD?.

My requirement is to get data sorted globally just like we sort in any programming language(Collections.sort() in java).

Consider the below code:(xyz is rdd after some series of operations)

JavaPairRDD<Float,Object>sorted = xyz.transformToPair(rdd->rdd.sortByKey());

Does the above statement gives result which is globally sorted?

If no, then how to get data sorted globally i.e considering all the data among all the partitions?

Thanks in advance.

1

1 Answers

0
votes

It is globally sorted (OrderedRDDFunctions):

Sort the RDD by key, so that each partition contains a sorted range of the elements. Calling collect or save on the resulting RDD will return or output an ordered list of records (in the save case, they will be written to multiple part-X files in the filesystem, in order of the keys).