问题
How to create a data frame from the result of a stored proc?
val jdbcDf = sqlContext.read.format("jdbc").options(Map(
"driver" -> "com.microsoft.sqlserver.jdbc.SQLServerDriver",
"url" -> jdbcSqlConn,
"dbtable" -> "(exec aStoredProc) a" // Error
)).load()
回答1:
This is not logically possible since the stored procedure can return 0 or more result-sets.
If the no of rows generated by the procedure is small the query can be executed in the driver application and the resultset can be converted into Dataframe/Dataset. For example the following code snippet generates the Dataframe from rw ResultSet
val conn = DriverManager.getConnection("jdbc:mysql://database/schema?user=username&password=pass")
val rs = conn.createStatement.executeQuery("exec stored_procedure()")
val data = Iterator.continually((rs.next(), rs)).takeWhile(_._1).map({case (_,rs) => rs.getString("col1") -> rs.getString("col2")}).toList // get the necassary columns (here I am getting col1,col2)
sc.parallelize(data).toDF()
Alternatively the stored procedure can be modified to write the resultset into a table and the table can be read to create a dataframe.
来源:https://stackoverflow.com/questions/43989894/get-the-result-of-a-stored-procedure-to-a-dataframe-or-rdd