title: SQL over Spark Streaming
body: <p>This is the code to run simple SQL queries over Spark Streaming.</p>
  
  <pre class="lang-scala prettyprint-override"><code>import org.apache.spark.streaming.{Seconds, StreamingContext}
  import org.apache.spark.streaming.StreamingContext._
  import org.apache.spark.sql.SQLContext
  import org.apache.spark.streaming.Duration
  
  object StreamingSQL {
  
    case class Persons(name: String, age: Int)
  
    def main(args: Array[String]) {
  
      val sparkConf = new SparkConf().setMaster("local").setAppName("HdfsWordCount")
      val sc = new SparkContext(sparkConf)
      // Create the context
      val ssc = new StreamingContext(sc, Seconds(2))
  
      val lines = ssc.textFileStream("C:/Users/pravesh.jain/Desktop/people/")
      lines.foreachRDD(rdd=&gt;rdd.foreach(println))
  
      val sqc = new SQLContext(sc);
      import sqc.createSchemaRDD
  
      // Create the FileInputDStream on the directory and use the
      // stream to count words in new files created
  
      lines.foreachRDD(rdd=&gt;{
        rdd.map(_.split(",")).map(p =&gt; Persons(p(0), p(1).trim.toInt)).registerAsTable("data")
        val teenagers = sqc.sql("SELECT name FROM data WHERE age &gt;= 13 AND age &lt;= 19")
        teenagers.foreach(println)
      })
  
      ssc.start()
      ssc.awaitTermination()
    }
  }
  </code></pre>
  
  <p>As you can see, to run SQL over streaming, the query has to be made inside the foreachRDD method.
  I want to run SQL join over data received from two different streams. Is there any way that can be done?</p>