使用来自 Bluemix Apache Spark 服务的 JDBC 连接到 postgresql 数据库

Connecting to a postgresql db using JDBC from the Bluemix Apache Spark service

我在使用 Bluemix 上的 Apache Spark 服务连接到我的 postgresql 8.4 数据库时遇到问题。

我的代码是:

%AddJar https://jdbc.postgresql.org/download/postgresql-8.4-703.jdbc4.jar -f
val sqlContext = new org.apache.spark.sql.SQLContext(sc)

sqlContext.load("jdbc", Map("url" -> "jdbc:postgresql://<ip_address>:5432/postgres?
user=postgres&password=<password>", "dbtable" -> "table_name"))

我收到错误:

Name: java.sql.SQLException

Message: No suitable driver found for jdbc:postgresql://:5432/postgres?user=postgres&password=

我看了一圈,似乎我需要将 JDBC 驱动程序添加到 Spark class 路径。我不知道如何在 Bluemix Apache Spark 服务中执行此操作。

目前将 JDBC 驱动程序添加到 Bluemix Apache Spark 时存在问题。该团队正在努力解决它。您可以在此处关注进度: https://developer.ibm.com/answers/questions/248803/connecting-to-postgresql-db-using-jdbc-from-bluemi.html

可能会看看here? I believe the load() function is deprecated in Spark 1.4 [source]。

你可以试试这个

val url = "jdbc:postgresql://:5432/postgres"
val prop = new java.util.Properties
prop.setProperty("user","postgres")
prop.setProperty("password","xxxxxx")

val table = sqlContext.read.jdbc(url,"table_name",prop)

url 可能需要也可能不需要完整版本 - 即

jdbc:postgresql://:5432/postgres? user=postgres&password=password

这对我在 Bluemix 上有用

%AddJar https://jdbc.postgresql.org/download/postgresql-9.4.1208.jar -f

val sqlContext = new org.apache.spark.sql.SQLContext(sc);

val df = sqlContext.read.format("jdbc").options(地图("url" -> "jdbc:postgresql://:/", "user" -> "", "password" -> "","dbtable" -> "", "driver" -> "org.postgresql.Driver")).load()