从Spark连接到Postgres时出现NullpointerException - 为什么?

问题描述 投票:1回答:1
object App {
  def main(args: Array[String]) {
    val conf = new spark.SparkConf().setMaster("local[2]").setAppName("mySparkApp")
    val sc = new spark.SparkContext(conf)
    val sqlContext = new SQLContext(sc)

    val jdbcUrl = "1.2.34.567" 
    val jdbcUser = "someUser"
    val jdbcPassword = "xxxxxxxxxxxxxxxxxxxx"
    val tableName = "myTable"
    val driver = "org.postgresql.Driver"
    Class.forName(driver)

    val df = sqlContext
            .read
            .format("jdbc")
            .option("driver", driver)
            .option("url", jdbcUrl)
            .option("userName", jdbcUser)
            .option("password", jdbcPassword)
            .option("dbtable", tableName) // NullPointerException occurs here
            .load()
  }
}

我想从Spark连接到我的局域网上的Postgres数据库。在运行时,发生以下错误:

Exception in thread "main" java.lang.NullPointerException
    at org.apache.spark.sql.execution.datasources.jdbc.JDBCRDD$.resolveTable(JDBCRDD.scala:71)
    at org.apache.spark.sql.execution.datasources.jdbc.JDBCRelation$.getSchema(JDBCRelation.scala:210)
    at org.apache.spark.sql.execution.datasources.jdbc.JdbcRelationProvider.createRelation(JdbcRelationProvider.scala:35)
    at org.apache.spark.sql.execution.datasources.DataSource.resolveRelation(DataSource.scala:318)
    at org.apache.spark.sql.DataFrameReader.loadV1Source(DataFrameReader.scala:223)
    at org.apache.spark.sql.DataFrameReader.load(DataFrameReader.scala:211)
    at org.apache.spark.sql.DataFrameReader.load(DataFrameReader.scala:167)
    at <redacted>?.main(App.scala:42)
    at <redacted>.App.main(App.scala)

是否有一个明显的原因,为什么在option("dbtable", tableName)线上有一个nullpointer异常?我正在使用带有Scala 2.11.12的spark-2.3.1-bin-hadoop2.7。对于postgres依赖,我使用的是这个版本:

        <dependency>
            <groupId>org.postgresql</groupId>
            <artifactId>postgresql</artifactId>
            <version>9.4-1200-jdbc41</version>
        </dependency>
postgresql scala apache-spark
1个回答
2
投票

错误消息(对故障排除不是很有帮助)可能不是针对选项dbtable,而是选项url

看起来你的jdbcUrl缺少URL协议jdbc:postgresql://作为它的前缀。这是一个链接:Spark's JDBC data sources

© www.soinside.com 2019 - 2024. All rights reserved.