Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to sort an RDD and limit in Spark?

I have RDD of Foo class : class Foo( name : String, createDate : Date ). I want an other RDD with 10 percent older Foo. My first idea was to sort by createDate and limit by 0.1*count, but there is no limit function.

Have you an idea?

like image 465
etig Avatar asked Oct 05 '15 11:10

etig


1 Answers

Assuming Foo is a case class like this:

import java.sql.Date
case class Foo(name: String, createDate: java.sql.Date)
  1. Using plain RDDs:

    import org.apache.spark.rdd.RDD
    import scala.math.Ordering
    
    val rdd: RDD[Foo] = sc
      .parallelize(Seq(
        ("a", "2015-01-03"), ("b", "2014-11-04"), ("a", "2016-08-10"),
        ("a", "2013-11-11"), ("a", "2015-06-19"), ("a", "2009-11-23")))
      .toDF("name", "createDate")
      .withColumn("createDate", $"createDate".cast("date"))
      .as[Foo].rdd
    
    rdd.cache()
    val  n = scala.math.ceil(0.1 * rdd.count).toInt
    
    • data fits into driver memory:

      • and fraction you want is relatively small

        rdd.takeOrdered(n)(Ordering.by[Foo, Long](_.createDate.getTime))
        // Array[Foo] = Array(Foo(a,2009-11-23))
        
      • fraction you want is relatively large:

        rdd.sortBy(_.createDate.getTime).take(n)
        
    • otherwise

      rdd
        .sortBy(_.createDate.getTime)
        .zipWithIndex
        .filter{case (_, idx) => idx < n}
        .keys
      
  2. Using DataFrame (note - this is actually not optimal performance wise due to limit behavior).

    import org.apache.spark.sql.Row
    
    val topN = rdd.toDF.orderBy($"createDate").limit(n)
    topN.show
    
    // +----+----------+
    // |name|createDate|
    // +----+----------+
    // |   a|2009-11-23|
    // +----+----------+
    
    
    // Optionally recreate RDD[Foo]
    topN.map{case Row(name: String, date: Date) => Foo(name, date)} 
    
like image 193
zero323 Avatar answered Sep 27 '22 20:09

zero323