简体   繁体   English

尝试通过 scala/spark 应用程序连接到 postgres 数据库时出现 ClassNotFoundException

[英]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.

相关问题 如何将scala play应用程序连接到postgres数据库 - How can I connect a scala play application to postgres database 尝试与mysql dbms连接时出现ClassNotFoundException? - ClassNotFoundException when trying to connect with mysql dbms? 使用spark-submit运行Spark应用程序时出现ClassNotFoundException - ClassNotFoundException when running Spark application with spark-submit 尝试将Java应用程序连接到Redshift数据库时服务器关闭连接 - Server closes connection when trying to connect Java application to Redshift database 当我尝试连接 jdbc、odbc 时出现 ClassNotFoundException - ClassNotFoundException when I'm trying to connect jdbc, odbc 尝试运行以前正确运行的应用程序时出现 ClassNotFoundException - ClassNotFoundException when trying to run a previously correctly running application SpringBoot 2.1.0 在尝试集成数据库访问时抛出 ClassNotFoundException - SpringBoot 2.1.0 throws ClassNotFoundException when trying to integrate database access 将Scala应用提交到独立Spark集群时出现java.lang.ClassNotFoundException - java.lang.ClassNotFoundException when submitting scala app to standalone spark cluster '致命:数据库“demodb”不存在'当尝试将 spring 引导应用程序连接到 postgresql 数据库时 - 'FATAL: database "demodb" does not exist' When trying to connect spring boot application to postgresql database 无法通过Java应用程序连接到数据库 - Not able to connect to database through java application
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM