Apache spark 如何在spark流媒体应用程序中保持SQLContext实例的活动状态';生命周期?

Apache spark 如何在spark流媒体应用程序中保持SQLContext实例的活动状态';生命周期?,apache-spark,spark-streaming,apache-spark-sql,Apache Spark,Spark Streaming,Apache Spark Sql,我在spark流媒体应用程序中使用了SQLContext,如下所示: case class topic_name (f1: Int, f2: Int) val sqlContext = new SQLContext(sc) @transient val ssc = new StreamingContext(sc, new Duration(5 * 1000)) ssc.checkpoint(".") val theDStream = KafkaUtils.createDirectStream[

我在spark流媒体应用程序中使用了SQLContext,如下所示:

case class topic_name (f1: Int, f2: Int)

val sqlContext = new SQLContext(sc)
@transient val ssc = new StreamingContext(sc, new Duration(5 * 1000))
ssc.checkpoint(".")
val theDStream = KafkaUtils.createDirectStream[String, String, StringDecoder, StringDecoder](ssc, kafkaParams, Set("topic_name"))

theDStream.map(x => x._2).foreach { rdd =>
  sqlContext.jsonRDD(newsIdRDD).registerTempTable("topic_name")
  sqlContext.sql("select count(*) from topic_name").foreach { x => 
    WriteToFile("file_path", x(0).toString)
  }
}

ssc.start()
ssc.awaitTermination()
我发现我只能获得每5秒的消息计数,因为“这个临时表的生存期与用于创建这个数据帧的SQLContext相关联”,我猜每5秒,就会创建一个新的SQLContext,临时表只能生存5秒,我想让sqlContext和临时表在整个流媒体应用程序的生命周期中都处于活动状态,如何做到这一点


谢谢~

你说得对。SQLContext只记住在该对象的生存期内注册的表。因此,您应该使用saveAsTable命令,而不是使用registerTempTable来使用持久性存储,比如Hive