本文介绍了scala-引发数据框的结果集的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在查询mysql表

i am querying mysql table

val url = "jdbc:mysql://XXX-XX-XXX-XX-XX.compute-1.amazonaws.com:3306/pg_partner"
val driver = "com.mysql.jdbc.Driver"
val username = "XXX"
val password = "XXX"
var connection:Connection = DriverManager.getConnection(url, username, password)
val statement = connection.createStatement()
val patnerName = statement.executeQuery("SELECT id,name FROM partner")

我确实在patnerName中得到了结果,但是我需要转换为Dataframe.

i do get my result in patnerName but i need to be converted to Dataframe.

我可以通过以下代码打印数据:

i am able to print data by below code:

while (patnerName.next) {
  val id = patnerName.getString("id")
  val name = patnerName.getString("name")
  println("id = %s, name = %s".format(id,name))
}

现在如何将patnerName转换为DataFrame?

Now How do i convert patnerName to a DataFrame?

推荐答案

直接使用Spark功能怎么样?

What about directly using the Spark functionality?

val jdbcDF = spark.read
  .format("jdbc")
  .option("url", "jdbc:mysql://XXX-XX-XXX-XX-XX.compute-1.amazonaws.com:3306/")
  .option("dbtable", "pg_partner")
  .option("user", "XXX")
  .option("password", "XXX")
  .load()

该代码取自这里.

这篇关于scala-引发数据框的结果集的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-11 08:07