In Scala, I need to use the same connection to run several queries and write the output to HDFS using spark context. It has to be the same connection because some of the queries create volatile tables, if the connection is closed, the volatile tables will be gone.
I am aware of the following function:
val jdbcDF = sqlContext.read.format("jdbc").options(
Map("url" -> "jdbc:postgresql:dbserver",
"dbtable" -> "schema.tablename")).load()
But it will require creating a connection each time I run a query. Is there any other alternative? I can get a result set from a Connection Object, but how give the rs to sqlcontext to write the data to HDFS?