Home >Database >Mysql Tutorial >How to Simulate SQL's `ROW_NUMBER()` Function in Spark RDD?
SQL Row Number Equivalent in Spark RDD
In Spark, obtaining a row number equivalent to SQL's row_number() over (partition by ... order by ...) for an RDD can be achieved using Spark 1.4's enhanced functionality.
Solution:
val sample_data = Seq(((3, 4), 5, 5, 5), ((3, 4), 5, 5, 9), ((3, 4), 7, 5, 5), ((1, 2), 1, 2, 3), ((1, 2), 1, 4, 7), ((1, 2), 2, 2, 3)) val temp1 = sc.parallelize(sample_data)
Utilize the rowNumber() function introduced in Spark 1.4 to create a partitioned window:
import org.apache.spark.sql.expressions.Window val partitionedRdd = temp1 .map(x => (x._1, x._2._1, x._2._2, x._2._3)) .groupBy(_._1) .mapGroups((_, entries) => entries.toList .sortBy(x => (x._2, -x._3, x._4)) .zipWithIndex .map(x => (x._1._1, x._1._2, x._1._3, x._1._4, x._2 + 1)) )
partitionedRdd.foreach(println) // Example output: // ((1,2),1,4,7,1) // ((1,2),1,2,3,2) // ((1,2),2,2,3,3) // ((3,4),5,5,5,4) // ((3,4),5,5,9,5) // ((3,4),7,5,5,6)
The above is the detailed content of How to Simulate SQL's `ROW_NUMBER()` Function in Spark RDD?. For more information, please follow other related articles on the PHP Chinese website!