Get the result of a stored procedure to a dataframe or Rdd?

点点圈 提交于 2020-12-05 12:04:13

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!