[英]ClassNotFoundException when trying to connect to a postgres database through a scala/spark application
I need to connect to a postgres database through a scala/spark app.我需要通过 scala/spark 应用程序连接到 postgres 数据库。 It works perfectly when I run it in my IDE however, I am getting the following log message when I try to run the packaged executable jar with this command:
当我在 IDE 中运行它时,它可以完美运行,但是,当我尝试使用以下命令运行打包的可执行文件 jar 时,我收到以下日志消息:
Triggering the executable jar with:使用以下命令触发可执行文件 jar:
java -cp HighestPerformingCampaign-assembly-1.0.jar com.scala.Executor
Exeception thrown:抛出异常:
Exception in thread "main" java.lang.ClassNotFoundException: Failed to find data source: jdbc. Please find packages at http://spark.apache.org/third-party-projects.html
at org.apache.spark.sql.execution.datasources.DataSource$.lookupDataSource(DataSource.scala:689)
at org.apache.spark.sql.execution.datasources.DataSource$.lookupDataSourceV2(DataSource.scala:743)
at org.apache.spark.sql.DataFrameReader.load(DataFrameReader.scala:266)
at org.apache.spark.sql.DataFrameReader.load(DataFrameReader.scala:226)
at com.scala.Executor$.findHighestCampaign(Executor.scala:31)
at com.scala.Executor$.main(Executor.scala:15)
at com.scala.Executor.main(Executor.scala)
Caused by: java.lang.ClassNotFoundException: jdbc.DefaultSource
at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:602)
at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:178)
at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:522)
at org.apache.spark.sql.execution.datasources.DataSource$.$anonfun$lookupDataSource$5(DataSource.scala:663)
at scala.util.Try$.apply(Try.scala:213)
at org.apache.spark.sql.execution.datasources.DataSo`enter code here`urce$.$anonfun$lookupDataSource$4(DataSource.scala:663)
at scala.util.Failure.orElse(Try.scala:224)
at org.apache.spark.sql.execution.datasources.DataSource$.lookupDataSource(DataSource.scala:663)
... 6 more
My build.sbt file is set up as follows:我的 build.sbt 文件设置如下:
name := "HighestPerformingCampaign"
version := "1.0"
crossScalaVersions := Seq("2.11.12", "2.12.12")
libraryDependencies += "org.apache.spark" %% "spark-core" % "3.1.1"
libraryDependencies += "org.apache.spark" %% "spark-sql" % "3.1.1"
libraryDependencies += "org.postgresql" % "postgresql" % "9.3-1102-jdbc41"
mainClass := Some("com.scala.Executor")
assemblyJarName in assembly := "HighestPerformingCampaign-assembly-1.0.jar"
and I am using the sbt-assembly plugin, which is stored under the project folder, to generate the jar:我正在使用存储在项目文件夹下的 sbt-assembly 插件来生成 jar:
addSbtPlugin("com.eed3si9n" % "sbt-assembly" % "0.15.0")
Is there something that I'm missing here which is preventing the driver from getting added to the packaged jar?有什么我在这里遗漏的东西阻止了驱动程序被添加到打包的 jar 中吗? My connection details are specified as follows also:
我的连接详细信息也指定如下:
val df = spark
.sqlContext
.read
.format("jdbc")
.option("url", "jdbc:postgresql:postgres")
.option("user", "postgres")
.option("password", "postgres")
.option("query", query)
.load()
You can check your jar if it has the required classes using jar -tf HighestPerformingCampaign-assembly-1.0.jar
.您可以使用
jar -tf HighestPerformingCampaign-assembly-1.0.jar
检查您的 jar 是否具有所需的类。 If it does not contain the required class jdbc.DefaultSource
(which should be the case), it means the fat / packaged jar is not getting built as expected.如果它不包含所需的 class
jdbc.DefaultSource
(应该是这种情况),则意味着胖/打包的 jar 未按预期构建。 I would suggest instead of creating this fat jar, you can create Artifacts
from the IDE (in Intellij it's at Project Settings -> Artifacts
), which is basically creates and puts all the dependent jars under some Artifacts root directory, and then provide the path of this directory to the java command like java -cp HighestPerformingCampaign-assembly-1.0.jar:<absolute path to artifact root> com.scala.Executor
I would suggest instead of creating this fat jar, you can create
Artifacts
from the IDE (in Intellij it's at Project Settings -> Artifacts
), which is basically creates and puts all the dependent jars under some Artifacts root directory, and then provide the path of this directory to the java command like java -cp HighestPerformingCampaign-assembly-1.0.jar:<absolute path to artifact root> com.scala.Executor
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.