Advertisement
Guest User

Untitled

a guest
Jun 29th, 2015
230
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Scala 1.54 KB | None | 0 0
  1. /* SimpleApp.scala */
  2. import org.apache.spark.SparkContext
  3. import org.apache.spark.SparkContext._
  4. import org.apache.spark.SparkConf
  5. import org.apache.spark.sql.DataFrame
  6.  
  7.  
  8.  
  9. case class Rank (pageURL:String, pageRank:Int, avgDuration:Int)
  10.  
  11. object SimpleApp {
  12.   def loadRankings(sc: SparkContext, sqlContext: org.apache.spark.sql.SQLContext, parts: Int, cached: Boolean=true): DataFrame = {
  13.     import sqlContext.implicits._
  14.  
  15.     val textFile = sc.textFile("/var/scratch/vdbogert/var/lib/bigDataBenchmark/5nodes.txt")
  16.     val textFileCoalesced = textFile.coalesce(parts, true)
  17.     val schemaRanks = (if(parts > 0) textFileCoalesced else textFile).map(_.split(",")).map(r=>Rank(r(0), r(1).toInt, r(2).toInt)).toDF()
  18.  
  19.     schemaRanks.registerTempTable("rankings")
  20.  
  21.     if (cached) {sqlContext.cacheTable("rankings"); println("Dataset being cached")} else println("Data not being cached")
  22.  
  23.     /* First one for caching */
  24.     sqlContext.sql("SELECT COUNT(1) FROM rankings").collect()
  25.  
  26.     return schemaRanks
  27.   }
  28.  
  29.   def main(args: Array[String]) {
  30.     /* args */
  31.     val parts = args(0).toInt
  32.     val reps = args(1).toInt
  33.     val query = args(2)
  34.     val cached = args(3).toBoolean
  35.  
  36.     val conf = new SparkConf().setAppName("Simple Application")
  37.     val sc = new SparkContext(conf)
  38.  
  39.     val sqlContext = new org.apache.spark.sql.SQLContext(sc)
  40.  
  41.     loadRankings(sc, sqlContext, parts, cached)
  42.  
  43.     System.out.println("Starting loop with reps")
  44.  
  45.     for(i <- 1 to reps) {
  46.       sqlContext.sql(query).collect()
  47.     }
  48.     sc.stop
  49.   }
  50. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement