gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 12:07:20 24 4
gpt4 key购买 nike

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) 行出现空指针异常是否有明显的原因?我在 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>

最佳答案

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

看起来您的 jdbcUrl 缺少 URL 协议(protocol) jdbc:postgresql:// 作为其前缀。这是一个链接:Spark's JDBC data sources .

关于postgresql - 从 Spark 连接到 Postgres 时出现 NullpointerException——为什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55616608/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com